jest-plugin-must-assert icon indicating copy to clipboard operation
jest-plugin-must-assert copied to clipboard

Async try...catch is not properly handled and throws an error where it shouldn't

Open frabbit opened this issue 2 years ago • 0 comments

This perfectly fine jest test fails with jest-plugin-must-assert:

describe("Async Try Catch", () => {
    it('should work', async () => {
        try {
            await Promise.reject('whoop')
        } catch (e) {
            console.log('error catched')
        }
        expect(true).toBe(true)
  })
})

The test works fine when the promise rejection is delayed with setTimeout:


describe("Async Try Catch 2", () => {
    it('should work', async () => {
        try {
            await new Promise((_, reject) => {
                setTimeout(() => reject('whoop'))
            })
        } catch (e) {
            console.log('error catched')
        }
        expect(true).toBe(true)
  })
})

This was especially hard to debug because I was initially using mockRejectedValue(...) in a jest test.

frabbit avatar Nov 17 '23 16:11 frabbit