Skip to content

@gtkx/testing / renderHook

Function: renderHook()

renderHook<Result, Props>(callback, options?): Promise<RenderHookResult<Result, Props>>

Defined in: render-hook.tsx:43

Renders a React hook for testing.

Creates a test component that executes the hook and provides utilities for accessing the result, re-rendering with new props, and cleanup.

Type Parameters

Type Parameter
Result
Props

Parameters

ParameterTypeDescription
callback(props) => ResultFunction that calls the hook and returns its result
options?RenderHookOptions<Props>Render options including initialProps and wrapper

Returns

Promise<RenderHookResult<Result, Props>>

A promise resolving to the hook result and utilities

Examples

tsx
import { renderHook } from "@gtkx/testing";
import { useState } from "react";

test("useState hook", async () => {
  const { result } = await renderHook(() => useState(0));
  expect(result.current[0]).toBe(0);
});
tsx
import { renderHook } from "@gtkx/testing";

test("hook with props", async () => {
  const { result, rerender } = await renderHook(
    ({ multiplier }) => useMultiplier(multiplier),
    { initialProps: { multiplier: 2 } }
  );

  expect(result.current).toBe(2);

  await rerender({ multiplier: 3 });
  expect(result.current).toBe(3);
});