我已经从 npm 安装了 node-qunit(稳定版),但似乎无法进行任何测试。我的源文件似乎没有包含在范围内。
./source/myscript.js:
var myObj = {
a : true
}
./test/tests.js:
test("that a is true", function () {
ok(myObj.a);
});
./test/runner.js:
var runner = require('qunit');
runner.run({
code : './source/myscript.js',
tests : './test/tests.js'
});
./生成文件:
test :
<tab>node ./test/testrunner.js
.PHONY: install test
如果我运行 make test
,我会得到一个 'ReferenceError: myObj is not defined'
错误。源文件确实运行,因为它可能会抛出错误。它似乎没有像应该的那样包含在全局范围内。如果我按照 node-qunit readme 中的说明从命令行执行它,则它不起作用.有人知道如何让它工作吗?
最佳答案
您没有导出任何内容。在幕后,node-qunit 使用 require
加载指定的模块。要在模块被 require
时公开变量,您必须将它们添加到 exports
中对象(或将您自己的对象分配给 exports
变量)
(对象字面量中也有语法错误——;
)
这对我有用:
./source/myscript.js:
exports.myObj = {
a: true
}
./test/tests.js:
QUnit.module('tests')
test("that a is true", function () {
ok(myObj.a)
})
./test/runner.js:
var runner = require('qunit')
runner.run({
code : './source/myscript.js'
, tests : './test/tests.js'
})
关于javascript - node-qunit 不在范围内包含我的源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8863068/