我有 2 个文件 mycode.js
和 mycode.test.js
。我想在 Node 控制台 (repl) -(无浏览器)中以 karma 风格测试它。
mycode.test.js
var expect = require('expect');
var mycode = require("./mycode");
describe("mycode", () => {
it("should properly run tests", () => {
expect(1).toBe(1);
});
});
实现此目标的最简单设置是什么?
最佳答案
我不知道如何在 Node repl 中实现它,因为对 require() 的调用假定您在加载要测试的文件之前单独加载该函数。我在 repl 中看到的测试代码的唯一方法是将您用来测试的函数包含在加载的文件中。例如,要测试函数覆盖的工作原理,您可以创建 function_override.js
然后将其加载到 repl --
$ .load path/to/file/function_override.js
-- 并有一个 assert() 函数,用于测试该文件内其他函数的输出。例如:
function assert(value, text) {
if (value === true) {
console.log(text);
} else {
console.log ("that is false");
}
}
var fun = 3;
function FuncType1(){
assert(typeof fun === "function", "We access the function");
}
function FuncType2(){
var fun = 3;
assert(typeof fun === "number", "Now we access the number");
}
function FuncType3(){
assert(typeof fun === "number", "Still a number");
}
function fun(){};
FuncType1(); // returns "We access the function"
FuncType2(); // returns "Now we access the number"
FuncType3(); // returns "that is false"
关于javascript - 如何在 Node repl(无浏览器)中测试 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694178/