React - Testing custom hooks

less than 1 minute read

React testing library provides a way to test a custom hook by using renderHook from @testing-library/react.

Github code

1
2
3
4
5
6
7
8
9
import { expect, test } from "@jest/globals"
import { renderHook } from "@testing-library/react"
import { useImageDropZone } from "../hooks/useImageDropZone"

test("should render useImageDropZone", async () => {
  const { result } = renderHook(() => useImageDropZone())
  const [filename, fileContent, ImageDropZone] = result.current
  expect(ImageDropZone).toBeDefined()
})

Categories:

Updated: