Socket
Socket
Sign inDemoInstall

@testing-library/react-hooks

Package Overview
Dependencies
Maintainers
16
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@testing-library/react-hooks

Simple and complete React hooks testing utilities that encourage good testing practices.


Version published
Weekly downloads
2.4M
decreased by-3.01%
Maintainers
16
Weekly downloads
Β 
Created

What is @testing-library/react-hooks?

The @testing-library/react-hooks package is designed to facilitate the testing of custom React hooks. It provides a simple and complete set of utilities that work well with the React Testing Library ecosystem. With this package, you can render hooks in isolation without having to deal with components, and you can test their behavior, state changes, and side effects.

What are @testing-library/react-hooks's main functionalities?

Rendering hooks and testing their initial state

This feature allows you to render a custom hook and assert its initial state. The `renderHook` function is used to render the hook, and the `result` object is used to access the hook's return values.

import { renderHook } from '@testing-library/react-hooks';

function useCustomHook() {
  const [value, setValue] = useState(0);
  return { value, setValue };
}

test('should start with initial value', () => {
  const { result } = renderHook(() => useCustomHook());
  expect(result.current.value).toBe(0);
});

Testing hook updates

This feature allows you to test the updates of a hook's state. The `act` function is used to wrap any code that triggers updates to the hook.

import { renderHook, act } from '@testing-library/react-hooks';

function useCounter() {
  const [count, setCount] = useState(0);
  return { count, increment: () => setCount(c => c + 1) };
}

test('should increment counter', () => {
  const { result } = renderHook(() => useCounter());
  act(() => {
    result.current.increment();
  });
  expect(result.current.count).toBe(1);
});

Testing asynchronous hooks

This feature is for testing hooks that have asynchronous operations, such as data fetching. The `waitForNextUpdate` function is used to wait for the hook to update after the asynchronous operation.

import { renderHook } from '@testing-library/react-hooks';

function useFetch(url) {
  const [data, setData] = useState(null);
  useEffect(() => {
    fetch(url).then(response => response.json()).then(setData);
  }, [url]);
  return data;
}

test('should fetch data', async () => {
  const { result, waitForNextUpdate } = renderHook(() => useFetch('https://api.example.com/data'));
  await waitForNextUpdate();
  expect(result.current).not.toBeNull();
});

Other packages similar to @testing-library/react-hooks

Keywords

FAQs

Package last updated on 10 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc