为什么茉莉花节点不拿起我的帮手脚本?

这个问题很可能是因为我缺乏以前使用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 

我敢肯定这个答案很简单,但是我没有find任何超级简单的介绍,或者在github上的任何明显的例子。 任何意见或帮助将不胜感激!

谢谢!

在节点中,所有东西都被命名为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()./更改为正确的path),也可以运行jasmine-node *.spec.js

如果您将jasmineconfiguration为:您不一定需要将您的帮助脚本包含在spec(testing)文件中:

 { "spec_dir": "spec", "spec_files": [ "**/*[sS]pec.js" ], "helpers": [ "helpers/**/*.js" ], "stopSpecOnExpectationFailure": false, "random": false } 

帮助器/文件夹中的所有内容都将在Spec文件之前运行。 在助手文件有这样的东西,包括你的function。

 beforeAll(function(){ this.helper_func = function() { console.log("IN HELPER FUNC"); }; }); 

你将能够在你的spec文件中引用它