-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bun:test promise rejection resolves instead of rejects when throw after await
with done callback
#1546
Comments
await
with with done callbackawait
with done callback
I don't know if this is the same bug, but const action = async (): Promise<unknown> => {
return Promise.reject(new Error('some error'))
}
test('reject', async () => {
await expect(action()).rejects.toThrow('some error')
}) |
As a temporary workaround, can you try putting action() in a function? const action = async (): Promise<unknown> => {
return Promise.reject(new Error('some error'))
}
test('reject', async () => {
await expect(() => action()).rejects.toThrow('some error')
}) |
Looks like that works, yes - and in playwright, jest and deno too. Thanks! |
The last example doesn't seem to be working for me. ( const action = () => Promise.reject('some error')
test('Promise to be', async () => {
await expect(Promise.reject('some error')).rejects.toBe('some error')
}) // passes
test('Promise to throw', async () => {
await expect(action()).rejects.toThrow('some error')
}) // error: Expected value must be a function
test('=> Promise to throw', async () => {
await expect(action).rejects.toThrow('some error')
}) // error: Expected promise, Received: [Function] |
I just found this after some searching, @tlgreg the current workaround I found is really non-compliant and I documented it in this issue (@Jarred-Sumner feel free to mark as duplicated since I just found this issue): test("rejects to octopus", () => {
// This should not be receiving a callback, but it's the only way it works:
const rejecting = Promise.reject(() => new Error("octopus"));
return expect(rejecting).rejects.toThrow("octopus");
}); |
As a further workaround, this way it can be compatible with both Node.js and Bun (in my case it's a single test that is failing so I've done this until it's fixed upstream): test("rejects to octopus", () => {
let rejecting;
if (typeof Bun !== "undefined") {
rejecting = Promise.reject(() => new Error("octopus"));
} else {
rejecting = Promise.reject(new Error("octopus"));
}
return expect(rejecting).rejects.toThrow("octopus");
}); Note another non-compliance bit is that if that first line was |
Also hitting this issue and it's blocking our migration since we don't have control of the rejection logic. |
This might be resolved since the related issue is resolved as well: |
In the following test, if
expect().toBe()
fails then the test hangs foreverThe reason why is that the Promise handler for
test
appears to be callingresolve
instead ofreject
.The text was updated successfully, but these errors were encountered: