Calling Chai plugin in Intern returns error

你说的曾经没有我的故事 提交于 2019-12-24 06:42:47

问题


I was trying to use the sinon-chai plugin within Intern but it gave me:

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

I had installed the plugin via npm and here's my test file:

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

  ...

});

What might go wrong?


回答1:


The node loader requires Node.js, so it can't be used in the browser. You'll need to load the sinon-chai library directly, as shown below (assuming the relative path from your test to node_modules is ../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);
  ...
});

You could simplify the test include by defining a sinon-chai package in your intern config:

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

Then you could get by with just:

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


来源:https://stackoverflow.com/questions/25995984/calling-chai-plugin-in-intern-returns-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!