Should js Cannot read property 'should' of null

softshipper picture softshipper · Aug 7, 2013 · Viewed 26.4k times · Source

i try to use the testing tool mocha in node. Consider the following test scenario

var requirejs = require('requirejs');

requirejs.config({
    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require
});


describe('Testing controller', function () {

    it('Should be pass', function (done) {
            (4).should.equal(4);
            done();
    });

    it('Should avoid name king', function (done) {
        requirejs(['../server/libs/validate_name'], function (validateName) {
            var err_test, accountExists_test, notAllow_test, available_test;
            validateName('anu', function (err, accountExists, notAllow, available) {
                accountExists.should.not.be.true;
                done();
            });

        });
    });

});  

as a testing result i have got:

$ make test
./node_modules/.bin/mocha \
                --reporter list

  . Testing controller Should be pass: 0ms
  1) Testing controller Should avoid name anu

  1 passing (560 ms)
  1 failing

  1) Testing controller Should avoid name anu:
     Uncaught TypeError: Cannot read property 'should' of null
      at d:\townspeech\test\test.spec.js:23:30
      at d:\townspeech\server\libs\validate_name.js:31:20
      at d:\townspeech\test\test.spec.js:22:13
      at Object.context.execCb (d:\townspeech\node_modules\requirejs\bin\r.js:1869:33)
      at Object.Module.check (d:\townspeech\node_modules\requirejs\bin\r.js:1105:51)
      at Object.Module.enable (d:\townspeech\node_modules\requirejs\bin\r.js:1376:22)
      at Object.Module.init (d:\townspeech\node_modules\requirejs\bin\r.js:1013:26)
      at null._onTimeout (d:\townspeech\node_modules\requirejs\bin\r.js:1646:36)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)



make: *** [test] Error 1

The first pass without any complication but the second, it seems like, that could not attach module shouldjs. Why?

Answer

Henry Tao picture Henry Tao · Nov 15, 2013

I had the same problem. I solved it by using:

(err === null).should.be.true;