Chai.should()使摩卡爆炸

时间:2016-02-02 13:24:31

标签: node.js mocha bdd chai

我尝试使用Chai应该使用样式断言,但下面的语句会让Mocha爆炸。这是我的完整需求块:

/**                                         
 * Module dependencies.                     
 */                                         
var mongoose = require('mongoose'),         
  User = mongoose.model('User'),            
  moment = require('moment'),               
  SSEvent = mongoose.model('Event'),        
  chai = require('chai');                   

chai.use(require('chai-datetime'));         
var should = chai.should();                   

错误讯息:

Running "mochaTest:src" (mochaTest) task
>> Mocha exploded!
>> TypeError: chai.should is not a function
>>     at Object.<anonymous> (/Users/sebastianweikart/Desktop/dev/conftool-nga-mean/modules/events/tests/server/events.server.model.tests.js:13:19)
>>     at Module._compile (module.js:413:34)
>>     at Object.Module._extensions..js (module.js:422:10)
>>     at Module.load (module.js:357:32)
>>     at Function.Module._load (module.js:314:12)
>>     at Module.require (module.js:367:17)
>>     at require (internal/module.js:16:19)
>>     at /Users/sebastianweikart/Desktop/dev/conftool-nga-mean/node_modules/mocha/lib/mocha.js:219:27

可能是什么问题? 我用     "chai": "^3.5.0""mocha": "~2.4.5"应该是最新的稳定版本。

更新

我现在添加了以下简单的剥离测试 - 它仍然会爆炸:

'use strict';

/**
 * Module dependencies.
 */
var chai = require('chai');
var should = chai.should();



/**
 * Unit tests
 */
describe('Chai Should Test', function () {

  describe('Chai Should() should work', function () {
    it('Chai Should() must work', function (done) {
      var spartacus = 'spartacus';
      should.exist(spartacus);
      done();
    });

  });
});



Running "mochaTest:src" (mochaTest) task
>> Mocha exploded!
>> TypeError: chai.should is not a function
>>     at Object.<anonymous> (/Users/sebastianweikart/Desktop/dev/conftool-nga-mean/modules/events/tests/server/chai.should.test.js:7:19)

5 个答案:

答案 0 :(得分:5)

确保在项目中卸载了should.js.当should.js和chai.should一起使用时会发生冲突

答案 1 :(得分:4)

确保您不需要should包裹 - &gt; https://www.npmjs.com/package/should

答案 2 :(得分:0)

如果您正确导入chai,则似乎没有问题。

chai docs表示:

  

如果需要,则执行该功能。

因此,如果您有一个有效的chai对象,它将附加shoudld()个函数。您可以testing it live on tonic dev验证这一点。

您还应该尝试删除chai.use(require('chai-datetime'));语句 - 这可能会覆盖chais原始属性并删除should()函数。

如果您仍然遇到问题,可能是由其他原因引起的 - 在这种情况下,我们需要查看/了解您的项目。

答案 3 :(得分:0)

我有同样的问题。

但是我刚导入的应该是const should = chai.should;而不是const should = chai.should();

答案 4 :(得分:-1)

我用过

chai.Should()

而不是

chai.should()

作为临时修复,因为我正在处理的项目目前需要一个需要包装的包,这会干扰chai。 Chai同时使用.should.Should导出,它们引用了我们正在调用的相同函数loadShould()