I use mocha, chai and promised chai. The test should fail but doesn't, I don't know what's going wrong, any suggestions?
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', async () => { expect(Promise.resolve('success')).to.rejected }) })
I'm trying to test a promise that should be rejected, the test should fail, but the test succeeds
P粉5150665182024-04-03 00:15:38
Excerpted fromChai as Promised document
You can use async /await
or .then(() => {})
to include multiple Promises in your 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 ); }); });
Example:https://stackblitz.com/edit/node- a7t3tx?file=index.js