最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my mochachai Error throwing test failing? - Stack Overflow

programmeradmin3浏览0评论

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
        // /
        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?

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?

Share Improve this question asked Sep 27, 2014 at 7:28 KhrobKhrob 1,2692 gold badges12 silver badges12 bronze badges 5
  • 1 If you look at the call stack, your test seems to be failing because of an error at line 10: var err = new ReferenceError('This is a bad function.'); Seems like your runtime environment doesn't recognize ReferenceError. What browser/environment are you using to run this test? – Ateş Göral Commented Sep 27, 2014 at 7:34
  • @AtesGoral: It doesn't matter what environment he's running in. You've found the bug: ReferenceError is undefined. Make it an answer. It's up to the OP to figure out why it's not defined. – slebetman Commented Sep 27, 2014 at 7:43
  • Should be a vanilla node environment, but I'll dig into the lack of ReferenceError. Thanks for the insight – Khrob Commented Sep 27, 2014 at 16:42
  • For the breadcrumb trail, console.log(err, typeof err, ReferenceError); on line 11 results in: [ReferenceError: This is a bad function.] 'object' [Function: ReferenceError]. Investigations continue... – Khrob Commented Sep 27, 2014 at 19:08
  • It is possible that the environment doesn't allow you to programmatically instantiate a ReferenceError because it's an intrinsic exception type. – Ateş Göral Commented Sep 27, 2014 at 19:26
Add a comment  | 

2 Answers 2

Reset to default 19

This should fix your problem:

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

Notice that the expect function is not being invoked.

Tracked it down!

Up the top

expect = require('chai').expect(),

wasn't giving me anything useful. Changing it to:

chai = require('chai'),

then calling the test as

chai.expect(fn).to.throw(ReferenceError);

does exactly what I expected.

Thanks for the help.

发布评论

评论列表(0)

  1. 暂无评论