33

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?

softshipper
  • 32,463
  • 51
  • 192
  • 400

5 Answers5

79

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

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

Seth
  • 1,545
  • 1
  • 16
  • 30
Henry Tao
  • 1,104
  • 10
  • 14
31

You can use should directly

should.not.exist(err);
agebrock
  • 459
  • 5
  • 7
9

That's a known problem with the should library: As it extends object, of course it only works when you have a concrete object. As null, by definition, means that you don't have an object, you can not call any methods on it or access any of its properties.

Hence, should is not available in this case.

Basically, you have two options of how to deal with this:

  1. You could exchange actual and expected. This way, as long as you do not expect null, you have an object in the beginning and hence can access its should property. But, this is not nice, as it changes semantics, and does not work in all cases.
  2. You could exchange the should library by another assertion library that does not have this problem.

Personally, I'd go with option 2, and my highly subjective personal favorite for this is node-assertthat (which has been written by me, hence it's my favorite).

Anyway, there are lots of other options, such as expect. Feel free to use any of these assertion libraries that best suits your style of writing tests.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 22
    You can use shuold to test for null objects with should.exists(object) – Mbrevda Aug 29 '13 at 21:38
  • 2
    good call, @zaboco. ```should.exist(object)``` is the correct method to call. I can't actually edit my comment thought :( – Mbrevda Jun 06 '14 at 13:10
  • 1
    Don't you mean `should.not.exist(possibleNullVariable)` since you are testing for null? – Chloe Nov 08 '19 at 21:43
7

One more option that I don't see mentioned here:

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

In you example that would would be:

should(accountExists).be.null
Mbrevda
  • 2,888
  • 2
  • 27
  • 35
  • 1
    Uncaught TypeError: Property 'should' of object # is not a function – FelikZ Nov 11 '14 at 13:41
  • @FelikZ you would have to show some code to give that error some context. Did you remember to require() should? – Mbrevda Nov 11 '14 at 17:59
  • yes should required. That error comes only with null and undefined variables. Other shoulds works as expected. Working solution for me is the post: http://stackoverflow.com/a/19994851/734493 – FelikZ Nov 12 '14 at 08:52
1

I always find expect works better with null and undefined as follows:

expect(err).be.null

bigbounty
  • 16,526
  • 5
  • 37
  • 65
Jeff C
  • 11
  • 3