I use mocha, chai, and chai-as-promised.
The test should fail, but it doesn’t, I don’t know what’s 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 tried to test a promise that should be rejected and the test should fail, but the test was successful
2
Answers
"Promise.resolve() resolves a promise, which is not the same as fulfilling or rejecting the promise." (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)
If you want to write a ‘dummy test’ just too see if you can catch that a promise is rejected do something like:
From the Chai as Promised docs
You can use
async / await
or.then(() => {})
to include multiple promises in a test.These four tests will fail:
Live example: https://stackblitz.com/edit/node-ooc3jh?file=index.js