Skip to content

Cannot test async useCallback in custom hook  #951

@mljlynch

Description

@mljlynch

What is your question:

Consider I have a custom hook that contains a few callbacks

const useDummyHook= ()=>{
  const useAysncCallback = useCallback(async(foo:string) =>{
    if(foo !== "bar){
      throw new Error("foo did not equal bar);
    }
  })
}

I want to test this error throws using renderHook. I cannot get the following to pass:

it('should throw an error if foo does not equal bar', async ()=>{
   const { result } = renderHook(()=>useDummyHook())
   await expect(async() =>{
     result.current.useAysncCallback("notBar")
  }).toThrow()
})

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions