如何在节点 repl(无浏览器)中测试 javascript?
How to test javascript in node repl (no browser)?
我有 2 个文件 mycode.js
和 mycode.test.js
。我想在 Node 控制台(repl)-(无浏览器)中以业力风格测试它。
mycode.test.js
var expect = require('expect');
var mycode = require("./mycode");
describe("mycode", () => {
it("should properly run tests", () => {
expect(1).toBe(1);
});
});
实现此目标的最简单设置是什么?
我不知道如何在节点 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"
我有 2 个文件 mycode.js
和 mycode.test.js
。我想在 Node 控制台(repl)-(无浏览器)中以业力风格测试它。
mycode.test.js
var expect = require('expect');
var mycode = require("./mycode");
describe("mycode", () => {
it("should properly run tests", () => {
expect(1).toBe(1);
});
});
实现此目标的最简单设置是什么?
我不知道如何在节点 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"