From the Chai as Promised docs
Notice: either return or notify(done) must be used with promise assertions. This can be a slight departure from the existing format of assertions being used on a project or by a team. Those other assertions are likely synchronous and thus do not require special handling.
The most powerful extension provided by Chai as Promised is the eventually property. With it, you can transform any existing Chai assertion into one that acts on a promise
You can use async / await
or .then(() => {})
to include multiple promises in a test.
These four tests will fail:
const { describe, it } = require('mocha');
const chai = require('chai');
const { expect } = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
describe('test', () => {
it('must be rejected (1)', () => {
return expect(Promise.resolve('success')).to.eventually.be.rejected;
});
it('must be rejected (2)', (done) => {
expect(Promise.resolve('success')).to.eventually.be.rejected.notify(done);
});
it('must be rejected (3)', async () => {
await expect(Promise.resolve('success1')).to.eventually.be.fulfilled;
return expect(Promise.resolve('success2')).to.eventually.be.rejected;
});
it('must be rejected (4)', () => {
return expect(Promise.resolve('success1')).to.eventually.be.fulfilled.then(
() => expect(Promise.resolve('success2')).to.eventually.be.rejected
);
});
});
Live example: https://stackblitz.com/edit/node-a7t3tx?file=index.js