Closed mljlynch closed 1 year ago
Try adding an await
inside the expect callback:
it('should throw an error if foo does not equal bar', async ()=>{
const { result } = renderHook(()=>useDummyHook())
await expect(async() =>{
await result.current.useAysncCallback("notBar")
}).toThrow()
})
Running:
it('should throw an error if foo does not equal bar', async ()=>{
const { result } = renderHook(()=>useDummyHook())
await expect(async() =>{
await result.current.useAysncCallback("notBar")
}).toThrow()
})
Jest fails since it did not throw
if I run the following:
it('should throw an error if foo does not equal bar', async ()=>{
const { result } = renderHook(()=>useDummyHook())
await result.current.useAysncCallback("notBar")
})
jest fails, but does not say an error was thrown:
foo did not equal bar
}
if (foo !== "bar") {
throw new Error("foo did not equal bar");
^
}
at call
at tryCatch (node_modules/regenerator-runtime/runtime.js:63:40)
This also doesn't work
await expect(async () => {
const asyncFunc = async () => Promise.reject(new Error('Foo'));
await asyncFunc();
}).toThrow();
The resolution to this was covered here: https://stackoverflow.com/questions/75377144/expect-an-async-function-to-throw-an-error-in-jest
What is your question:
Consider I have a custom hook that contains a few callbacks
I want to test this error throws using
renderHook
. I cannot get the following to pass:When I run the test, the test says the given function has not thrown an error, but If i do not assert that an error is thrown the test throws an error.
Am I using the custom hooks wrong? Am I missing something in renderHook?