繁体   English   中英

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

[英]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.

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