sebbulon
sebbulon

Reputation: 623

Chai.should() makes Mocha explode

I try to use Chai Should style assertions, but the following statement makes Mocha explode. Here is my complete Require block:

/**                                         
 * 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();                   

Error message:

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

What could possibly be the problem? I use "chai": "^3.5.0" and "mocha": "~2.4.5" which should be the latest stable versions..

Update:

I now added the following simple stripped down test - and it still explodes:

'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)

Upvotes: 1

Views: 2764

Answers (5)

Артур Гудиев
Артур Гудиев

Reputation: 1144

I had the same problem.

But I just imported should in this way const should = chai.should; instead of const should = chai.should();

Upvotes: 0

Aaron Martins
Aaron Martins

Reputation: 374

I used

chai.Should()

instead of

chai.should()

as a temporary fix, since the project I'm working on currently requires a package that requires the should package, which interferes with chai. Chai exports with both .should and .Should, and they refer to the same function, loadShould() which is what we're invoking.

Upvotes: -1

Asif Saeed
Asif Saeed

Reputation: 2045

make sure should.js is uninstalled in the project. Conflict occurs when should.js and chai.should is used together

Upvotes: 5

Michael
Michael

Reputation: 1197

Make sure you are not also requiring the should package -> https://www.npmjs.com/package/should.

Upvotes: 4

duncanhall
duncanhall

Reputation: 11431

If you are correctly importing chai it doesn't seem as if there's a problem.

The chai docs state:

With the should require, the function is [...] executed.

So if you have a valid chai object, it will have a shoudld() function attached. You can verify this by testing it live on tonic dev.

You should also try to remove the chai.use(require('chai-datetime')); statement - this could be overriding chais original properties and removing the should() function.

If you are still having problems it is likely being caused by something else - in which case we'd need to see / know more about your project.

Upvotes: 0

Related Questions