chaijs / chai-as-promised

Extends Chai with assertions about promises.
MIT License
1.42k stars 112 forks source link

Asserting a rejcted promise to not be rejected succeeds #278

Closed meta72 closed 4 months ago

meta72 commented 1 year ago

Setup

Test Suite

describe("assertion that does not work as expected", function(): void {
    it("should fail expecting rejected Promise not to be rejected", function(): PromiseLike<void> {
        // this test case actually succeeds
        return expect(Promise.reject(new Error())).to.eventually.not.be.rejected;
    });
});
Similar assertions that work as expected ```typescript describe("assertions that work as expected", function(): void { it("should succeed expecting rejected Promise to be rejected", function(): PromiseLike { return expect(Promise.reject(new Error("hiss"))).to.eventually.be.rejected; }); it("should succeed expecting resolved Promise not to be rejected", function(): PromiseLike { return expect(Promise.resolve()).to.eventually.not.be.rejected; }); it("should succeed expecting resolved Promise to be fulfilled", function(): PromiseLike { return expect(Promise.resolve()).to.eventually.be.fulfilled; }); it("should fail expecting resolved Promise to be rejected", function(): PromiseLike { return expect(Promise.resolve()).to.eventually.be.rejected; }); it("should fail expecting rejected Promise to be fulfilled", function(): PromiseLike { // this can be used as I would have `.to.eventually.not.be.rejected` expected to behave return expect(Promise.reject(new Error())).to.eventually.be.fulfilled; }); }); ```

I would have expected that the first test case, i.e. asserting that a rejected promise will not be rejected, should fail. Is this a bug, or did I miss something with negating assertions with eventually?

43081j commented 4 months ago

as far as i can tell, i think this is expected behaviour

basically, when you want to assert against the eventual value, you would use eventually.

when you want to assert that it resolved or rejected, you would not. similarly, if you want to assert that it rejected with a particular value, you wouldn't use eventually.

examples:

// for all of these, we're asserting on the state of the promise.
// not on the eventual value
expect(val).to.be.rejected;
expect(val).not.to.be.rejected;
expect(val).to.be.rejectedWith(Error);

// but for this we want to assert against the eventual value
expect(val).to.eventually.equal(123);