[英]why is jasmine-node not picking up my helper script?
這個問題可能是因為我以前缺乏使用 node.js 的經驗,但我希望 jasmine-node 能讓我從命令行運行我的 jasmine 規范。
測試助手.js:
var helper_func = function() {
console.log("IN HELPER FUNC");
};
我的測試.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 上找到任何超級簡單的介紹或任何明顯的內容。任何建議或幫助將不勝感激!
謝謝!
在節點中,所有內容都命名為它的 js 文件。 要使 function 可由其他文件調用,請將 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
。
如果您有 jasmine 配置,則不一定需要在規范(測試)文件中包含您的幫助程序腳本:
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
helpers/ 文件夾中的所有內容都將在 Spec 文件之前運行。 在幫助文件中有類似這樣的內容來包含您的 function。
beforeAll(function(){
this.helper_func = function() {
console.log("IN HELPER FUNC");
};
});
然后你就可以在你的規范文件中引用它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.