Chai: How Should I Throw?

Created on 2 Jun 2012  路  10Comments  路  Source: chaijs/chai

I tried to write should.js style throw statements like so (new Bill(input)).should.throw(Error); but its not working. Where can I find resources wherein I can checkout documentation on should assertion style?

Most helpful comment

Try this...

(function () {
  new Bill(input);
}).should.throw(Error);

All 10 comments

Our website BDD documentation is likely the best place to start: http://chaijs.com/api/bdd/ - Just select "throw" from the scrollable list on the left or Throw is the fourth from the bottom.

Alternatively, if you are looking to dive into some actual code, our test suite is rather complete. You can find the .should.throw() tests here: https://github.com/chaijs/chai/blob/master/test/should.js#L426

If you feel you Chai is not performing correctly, please provide more information, starting with what environment (node or browser), and what test runner you are using, leading into what you expected to happen vs. what actually happen.

Let me know how it goes!

My Testing environment is browser google chrome 18.x, I think there is still a problem. Check the following code

Test.js

(new Bill(input)).should.throw(Error);

Try this...

(function () {
  new Bill(input);
}).should.throw(Error);

Yes, its working, thanx

throw uses try/catch so it needs a function to execute.

Let me know if you have any more questions. Good luck!

I have read above posts and tested several times. Then got following

  • Error should throw like following
function throwme() {
  throw new Error('I am exception');
}
  • Test throw like following
  it('It throw exception', function() {
    (function() {
      throwme();
    }).should.Throw('I am exception');
  });

Any better idea?

Hi can any one help? it's wired that for a same function, both throw and not throw pass the test

https://jsfiddle.net/8t5bf261/

class Person {
  constructor(age) {
    if (Object.prototype.toString.call(age) !== '[object Number]') throw 'NOT A NUMBER'
    this.age = age;
  }
  howold() {
    console.log(this.age);
  }
}

var should = chai.should();
mocha.setup('bdd');

describe('Person', function() {
  it('should throw if input is not a number', function() {
    (function() {
      var p1 = new Person('sadf');
    }).should.not.throw;

    (function(){
    var p2= new Person('sdfa');
    }).should.throw;
  })
})

mocha.run();

@sabrinaluo Try using .should.throw() and .should.not.throw() (with parenthesis).

You might also want to consider using throw Error('NOT A NUMBER') or even throw TypeError('NOT A NUMBER') in your class so that the stack is captured, though I don't think this is directly related to your issue.

@meeber thanks, it works for me!

Here's two examples with cleaner syntax that work fine for me:

it('should pass validation', function() {
      should.not.throw(() => { model.fromJson(validJson) });
});

it('should fail validation', function() {
      should.throw(() => { mode.fromJson(invalidJson) }); 
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jockster picture jockster  路  4Comments

huaguzheng picture huaguzheng  路  3Comments

domenic picture domenic  路  4Comments

liborbus picture liborbus  路  4Comments

meeber picture meeber  路  3Comments