I'm using chai-as-promised to test some promises. My issue is I'm not sure how to have multiple expect statements in a single test. In order for the expect().to.be.fulfilled
to work properly, I need to return it, like this:
it('test', () => {
return expect(promise).to.be.fulfilled
}
... or to use notify
, like this:
it('test', (done) => {
expect(promise).to.be.fulfilled.notify(done)
}
The issue comes when I have another thing I need to check, such as that a certain function gets called, like this:
it('test', (done) => {
var promise = doSomething()
expect(sinon_function_spy.callCount).to.equal(1)
expect(promise).to.be.fulfilled.notify(done)
})
The problem here is that, because doSomething()
is asynchronous, the call to sinon_function_spy
may not have occurred yet when I call that expect
, making this test flaky. If I use a then
, like this:
it('test', (done) => {
var promise = doSomething()
promise.then(() => {
expect(sinon_function_spy.callCount).to.equal(1)
})
expect(promise).to.be.fulfilled.notify(done)
})
Then the test technically passes and fails as expected, but it will fail because the promise gets rejected, due to the thrown exception in the then
call. Similarly, if I have a case where the promise is expected to reject:
it('test', (done) => {
var promise = doSomething()
promise.then(() => {
expect(sinon_function_spy.callCount).to.equal(1)
})
expect(promise).to.be.rejected.notify(done)
})
Then the check on the sinon_function_spy
never gets called, since the promise was rejected and doesn't call then
.
How can I get both expect
statements to reliably execute and return the correct values?