vendredi 25 mars 2016

How to test for thrown error with Chai.should

I'm using Chai.should and I need to test for an exception, but whatever I try, I cannot get it to work. The docs only explain expect :(

I have this Singleton class which throws an error if you try

new MySingleton();

Here is the constructor that throws the error

constructor(enforcer) {
    if(enforcer !== singletonEnforcer) throw 'Cannot construct singleton';
    ...

Now I would like to check that this happens

 it('should not be possible to create a new instance', () => {
    (function () {
        new MySingleton();
    })().should.throw(Error, /Cannot construct singleton/);
 });

or

new MySingleton().should.throw(Error('Cannot construct singleton');

None of these work. How is this done ? Any suggestions ?

Aucun commentaire:

Enregistrer un commentaire