Mocha UnhandledPromiseRejectionWarning: AssertionError

Viewed 1169

I'm beginning start writing some Mocha tests for an application I've been working on.

When running onn of the tests fails I'm getting the following error:

(node:12988) UnhandledPromiseRejectionWarning: AssertionError: Failure reason: expected 'Fail' to include 'Success'

Whilst I understand the failure reason, I'm really struggling to resolve the UnhandledPromiseRejection warning and have gone round in circles with the issue.

Any ideas?

describe('Test', function() {


    it('This should complete succesfully', function() {


        class Obj { 
            getResult() { 
                return new Promise(function(resolve, reject) {  
                    resolve('Fail');
                });
            }
        }

        var obj = new Obj();
        obj.getResult().then(function(res) {
            expect(res).to.include('Success');
        }); 


    });

    });
1 Answers

The test is asynchronous and should be treated as such. Modern testing frameworks support promises, including Mocha. If there's a promise, it should be returned from test function:

return obj.getResult().then(function(res) {
    expect(res).to.include('Success');
}); 
Related