Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my mocha/chai Error throwing test failing?

I have a simple javascript package I'm trying to test. I want to check for an Error being thrown, but when my test is run, and the error is thrown, the test is marked as failing.

Here's the code:

var should = require('chai').should(),
    expect = require('chai').expect();

describe('#myTestSuite', function () {

    it ('should check for TypeErrors', function () {

        // Pulled straight from the 'throw' section of
        // http://chaijs.com/api/bdd/
        var err = new ReferenceError('This is a bad function.');
        var fn = function () { throw err; }
        expect(fn).to.throw(ReferenceError);

    })

})

Which, when run gives me the following output:

kh:testthing khrob$ npm test

> [email protected] test /Users/khrob/testthing
> mocha



  #myTestSuite
    1) should check for TypeErrors


  0 passing (5ms)   1 failing

  1) #myTestSuite should check for TypeErrors:
     TypeError: object is not a function
      at Context.<anonymous> (/Users/khrob/testthing/test/index.js:10:3)
      at callFn (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:249:21)
      at Test.Runnable.run (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:242:7)
      at Runner.runTest (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:373:10)
      at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:451:12
      at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:298:14)
      at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:308:7
      at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:246:23)
      at Object._onImmediate (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:275:5)
      at processImmediate [as _immediateCallback] (timers.js:336:15)



npm ERR! Test failed.  See above for more details. 
npm ERR! not ok code 0

I know there's dozens of answers on here about what you pass to expect() being a function not the result of a function, and I've tried every permutation of anonymous functionizing I can think of, but I always get the failed test result.

I'm thinking it must be something to do with my config, given that I'm basically just running the example from the documentation, or my expectation for what is a pass or fail on the test is not calibrated properly.

Any clues?

like image 515
Khrob Avatar asked Sep 27 '14 07:09

Khrob


People also ask

How do you skip the Mocha test?

This inclusive ability is available in Mocha by appending . skip() to the suite or to specific test cases. The skipped tests will be marked as "pending" in the test results.

How do I run a single test file in Mocha chai?

If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case. If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe.


1 Answers

This should fix your problem:

var expect = require('chai').expect;

Notice that the expect function is not being invoked.

like image 88
Steven Vachon Avatar answered Nov 16 '22 00:11

Steven Vachon