You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{describe,expect,test}from'bun:test';constaskGPT=async(text: string,prompt: string,context: string): Promise<void>=>{thrownewError('⛔ API error.');};describe('',()=>{test('',async()=>{awaitexpect(async()=>awaitaskGPT('text','prompt','context')).toThrow(Error(`⛔ API error.`));});});
This works only in bun, but not in vitest, so it's not compatible. It seems .rejects.toThrow is not implemented correctly or at all in bun, it throws Expected value must be a function on code like this that works in vitest:
Reacted by hsnmkls, Ryan Wersal, Robert Payne, Josh McCormick, silverwind, Bjön Limell, Austin Baldwin, Igor Krupenja, Xiliang Chen, Andy Matuschak and 17 more
Reacted by Thomas Cauquil, Mark Jaquith and silverwind
I believe I'm experiencing the same issue.
I have the following code that works with jest but fails with bun:
it('rejects with a dom exception', () => {
fetch.mockAbort()
expect(fetch('/')).rejects.toThrow(expect.any(DOMException))
})
It results in the following failure:
error: expect(received).toThrow(expected)
Expected constructor: ExpectAny
Received constructor: DOMException
The intended behavior that the .toThrow() function should be that toThrow should be checking what ExpectAny holds but it seems in this case toThrow is comparing against ExpectAny directly.
There seems to be no combination that I can use to get this to work (make a test pass where we expect the object being tested to throw an error), so I definitely believe this to be a bug.
Activity
mrbrianevans commentedon Oct 9, 2023
This works when run with jest, but not when run with Bun. The above error output is from running
bun test, but when run with jest this test passes.tghpereira commentedon Oct 29, 2023
This bug also happens for synchronous calls.
Version of Bun is running: 1.0.7+b0393fba6200d8573f3433fb0af258a0e33ac157
Platform: Linux 6.2.0-35-generic x86_64 x86_64
MauriceArikoglu commentedon Jan 27, 2024
Still happening in bun 1.0.25
di-sukharev commentedon Mar 7, 2024
i do like this:
it works.
the
BalanceWithdrawalService.addWithdrawalToQueueis asyncstephenh commentedon Apr 2, 2024
@Electroid I just upgraded to bun 1.1, and it looks like this has been fixed!
RichAyotte commentedon May 22, 2024
workaround
MattBDev commentedon Jul 9, 2024
I believe I'm experiencing the same issue.
I have the following code that works with jest but fails with bun:
It results in the following failure:
The intended behavior that the
.toThrow()function should be that toThrow should be checking what ExpectAny holds but it seems in this case toThrow is comparing against ExpectAny directly.Update `expect().rejects` to call `async` functions
digisomni commentedon Feb 23, 2025
There seems to be no combination that I can use to get this to work (make a test pass where we expect the object being tested to throw an error), so I definitely believe this to be a bug.
docs: split UPSTREAM_ISSUES.md from ISSUES_WILL_CLOSE.md; drop phanto…
robobun commentedon Jun 16, 2026
This works correctly now. Verified on Bun v1.4.0:
No more
Expected value must be a functionerror.