Search code examples
es6-promisechaichai-as-promised

chai as promised test rejection timeout


How should I test the reject:

return new Promise(function(resolve, reject){
            models.users.find({
                where: {
                    email: email
                }
            }).then(function(result){
                if(!result)
                    throw 'Invalid password'
            }).catch(function(err){
                reject(err);
            });
        });

in my test:

it('should be rejected', function(){
            let data= { req: {
                email: '[email protected]',
            }};

            return User.authUser(data).should.be.rejected;
        });

It should be rejected whit error 'Invalid password', but i get error:

 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure
it resolves.

Solution

  • You could throw an error and test the content of the error message. You needn't to reject the promise because an exception reject the promise and you shouldn't wrap it in another promise:

       return models.users.find({
                where: {
                    email: email
                }
            }).then(function(result){
                if(!result)
                    throw new Error('Invalid password');
            });
    

    and in your test :

    return User.authUser(data).should.be.rejectedWith(Error).and.eventually.have.property("message").equal('Invalid password');