简体   繁体   English

为什么 jasmine-node 没有获取我的帮助脚本?

[英]why is jasmine-node not picking up my helper script?

This question is likely based in my lack of previous experience with node.js, but I was hoping jasmine-node would just let me run my jasmine specs from the command line.这个问题可能是因为我以前缺乏使用 node.js 的经验,但我希望 jasmine-node 能让我从命令行运行我的 jasmine 规范。

TestHelper.js:测试助手.js:

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

my_test.spec.js:我的测试.spec.js:

describe ('Example Test', function() {
  it ('should use the helper function', function() {
    helper_func();
    expect(true).toBe(true);
  }); 
});

Those are the only two files in the directory.这是目录中仅有的两个文件。 Then, when I do:然后,当我这样做时:

jasmine-node .

I get我得到

ReferenceError: helper_func is not defined

I'm sure the answer to this is easy, but I didn't find any super-simple intros, or anything obvious on github. Any advice or help would be greatly appreciated!我确信这个问题的答案很简单,但我没有在 github 上找到任何超级简单的介绍或任何明显的内容。任何建议或帮助将不胜感激!

Thanks!谢谢!

In node, everything is namespaced to it's js file.在节点中,所有内容都命名为它的 js 文件。 To make the function callable by other files, change TestHelper.js to look like this:要使 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;

And then change your my_test.spec.js to look like this:然后将您的 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);
  }); 
});

and, lastly, I believe jasmine-node.最后,我相信jasmine-node. will run every file in the directory sequentially - but you don't need to run the helpers.将按顺序运行目录中的每个文件 - 但您不需要运行助手。 Instead you could move them to a different directory (and change the ./ in the require() to the correct path), or you could just run jasmine-node *.spec.js .相反,您可以将它们移动到不同的目录(并将require()中的./更改为正确的路径),或者您可以只运行jasmine-node *.spec.js

you do not necessarily need to include your helper script in the spec (testing) file if you have the jasmine config as:如果您有 jasmine 配置,则不一定需要在规范(测试)文件中包含您的帮助程序脚本:

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

Everything in the helpers/ folder will run before the Spec files. helpers/ 文件夹中的所有内容都将在 Spec 文件之前运行。 In the helpers files have something like this to include your function.在帮助文件中有类似这样的内容来包含您的 function。

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

you will then be able to make references to it in your spec files然后你就可以在你的规范文件中引用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM