In my pursuit to test a simple javascript package, I encountered an issue. Despite wanting to verify if an Error is thrown during the test, it bizarrely gets marked as a failure when executed.
Here's the snippet of code in question:
var should = require('chai').should(),
expect = require('chai').expect();
describe('#myTestSuite', function () {
it ('should check for TypeErrors', function () {
// Taken directly from the 'throw' section in
// http://chaijs.com/api/bdd/
var err = new ReferenceError('This is a bad function.');
var fn = function () { throw err; }
expect(fn).to.throw(ReferenceError);
})
})
Upon running the above code, the output received is as follows:
kh:testthing khrob$ npm test
> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c38293f38382425222b0c7c627d627c">[email protected]</a> 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've tried various approaches including wrapping the function inside anonymous functions as suggested in different answers but still no success in passing the test. It seems like there might be some configuration error or perhaps my understanding of pass/fail criteria for the test is off.
Any insights on this matter?