【问题标题】:Calling Chai plugin in Intern returns error在 Intern 中调用 Chai 插件返回错误
【发布时间】:2014-09-23 13:08:57
【问题描述】:

我试图在 Intern 中使用 sinon-chai plugin,但它给了我:

Error {stack: (...), message: "Cannot find the Node.js require"} 

我已经通过 npm 安装了插件,这是我的测试文件:

define([
  'intern!bdd',
  'intern/chai!expect',
  'app/functions',
  'intern/chai',
  'intern/dojo/node!sinon-chai'
], function (bdd, expect, myapp, chai, sinonChai) {
  chai.use(sinonChai);

  ...

});

可能出了什么问题?

【问题讨论】:

  • 你浏览过它吗?

标签: node.js dojo requirejs intern


【解决方案1】:

节点加载器需要Node.js,所以不能在浏览器中使用。您需要直接加载 sinon-chai 库,如下所示(假设您的测试到 node_modules 的相对路径是 ../node_modules):

define([
  'intern!bdd',
  'intern/chai!expect',
  'app/functions',
  'intern/chai',
  '../node_modules/sinon-chai/lib/sinon-chai'
], function (bdd, expect, myapp, chai, sinonChai) {
  chai.use(sinonChai);
  ...
});

您可以通过在实习生配置中定义一个 sinon-chai 包来简化测试包含:

...
loader: {
    { name: 'sinon-chai', location: 'node_modules/sinon-chai/lib' },
    ...
}
...

那么你可以只用:

define([
    ...
    'sinon-chai/sinon-chai'
], function (bed, expect, myapp, chai, sinonChai) {
...
});

【讨论】:

  • 这就是我为实习生设置集成chai-spies 库的方式。需要注意的一点是,我必须在我的 package.json 中单独安装 chai lib,在我的测试文件中,我从 node_modules 文件夹中包含了 chai lib,以便使用 chai.use() 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多