javascript - 为什么 jasmine-node 没有选择我的帮助脚本?

标签 javascript unit-testing testing node.js jasmine

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

相关文章:

unit-testing - 无法找到按钮来模拟点击 - 使用 Mocha、Chai、Enzyme 进行 React 单元测试

python - 在第三方 Egg 上运行 plon 单元测试

javascript - 首先在 NodeUnit 中运行 init - 单独的文件

java - 如何在sencha中调用javascript中的java方法?

javascript - 使 NbAlertComponent 的关闭按钮起作用的正确方法是什么(关闭警报)

java - 在 Robolectric 3.1.2 中设置 FakeHttp.setDefaultHttpResponse 没有给我响应

ruby-on-rails - 为什么我的 Cucumber 测试在使用 Selenium 时会失败?

unit-testing - Go - 测试函数相等性

javascript - Angular 网格上的批量编辑

javascript - 计算div在div中的位置