这个问题可能是因为我以前缺乏使用 node.js 的经验,但我希望 jasmine-node 能让我从命令行运行我的 jasmine 规范。
TestHelper.js:
var helper_func = function() {
console.log("IN HELPER FUNC");
};
my_test.spec.js:
describe ('Example Test', function() {
it ('should use the helper function', function() {
helper_func();
expect(true).toBe(true);
});
});
这是目录中仅有的两个文件。然后,当我这样做时:
jasmine-node .
我明白了
ReferenceError: helper_func is not defined
我确信这个问题的答案很简单,但我没有在 github 上找到任何 super 简单的介绍或任何明显的内容。任何建议或帮助将不胜感激!
谢谢!
最佳答案
在 Node 中,所有内容都被命名为它的 js 文件。要使该函数可以被其他文件调用,请将 TestHelper.js 更改为如下所示:
var helper_func = function() {
console.log("IN HELPER FUNC");
};
// exports is the "magic" variable that other files can read
exports.helper_func = helper_func;
然后将您的 my_test.spec.js 更改为如下所示:
// include the helpers and get a reference to it's exports variable
var helpers = require('./TestHelpers');
describe ('Example Test', function() {
it ('should use the helper function', function() {
helpers.helper_func(); // note the change here too
expect(true).toBe(true);
});
});
最后,我相信 jasmine-node .
将按顺序运行目录中的每个文件 - 但您不需要运行帮助程序。相反,您可以将它们移动到不同的目录(并将 require()
中的 ./
更改为正确的路径),或者您可以只运行 jasmine- Node *.spec.js
.
关于javascript - 为什么 jasmine-node 没有选择我的帮助脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081882/