npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-overridable-hooks

v2.0.2

Published

Provides an easy way to mock hooks, for testing and for Storybook integration

Readme

react-overridable-hooks

An easy way to mock custom React hooks. Especially useful for:

  • Mocking a component's UI state or data in Storybook
  • Mocking state for Unit testing
  • Overriding hooks depending on context
npm install --save react-overridable-hooks

Guide

Please see GUIDE.md to see how to use testableHook to create testable, mockable components.

API

Table of Contents generated with DocToc

overridableHook and testableHook

Creates an overridable version of a hook.

// Counter.jsx
import { overridableHook } from "react-overridable-hooks";

// A custom hook, wrapped so it can be overridden
export const useCounter = overridableHook(() => {
  const [ count, setCount ] = React.useState(0);
  const increment = () => setCount((c) => c + 1);
  return { count, increment };
});

// A component uses the hook normally:
export const Counter = () => {
  const { count, increment } = useCounter();
  return <div>
    Count: {count}
    <button onClick={increment}> Increment</button>
  </div>;
}

The overridableHook and testableHook signatures are identical.
The only difference is that a testableHook is only overridable when the following environment variables are set:

  • NODE_ENV=test (the default for Jest)
  • STORYBOOK=true (the default for Storybook)
  • TESTABLE_HOOKS_ENABLED=true (manually set by you)

Otherwise, the original function is simply returned. This means:

  • These hooks are overridable in unit tests and Storybook.
  • These hooks cannot be overridden in development or production.
  • Therefore, there is zero performance overhead in production!

createHookOverridesProvider

An overridable hook behaves normally, until we override it using a HookOverrides provider that we create using createHookOverridesProvider.

For example, we can mock the hook's value in Storybook:

// Counter.stories.jsx
import { createHookOverridesProvider } from 'react-overridable-hooks';
import { useCounter } from './Counter';

const HookOverrides = createHookOverridesProvider({ useCounter })

export const NormalCounter = () => (
  <Counter/> // renders "Count: 0"
);

export const WithBigValue = () => (
  <HookOverrides useCounter={() => ({ count: 999, increment: action('increment') })}>
    <Counter/> <!-- renders "Count: 999" -->
  </HookOverrides>
);

And we can mock the hook's value in Unit Tests:

// Counter.test.jsx
describe('Counter', () => {
  const HookOverrides = createHookOverridesProvider({ useCounter })
  it("should show the count", () => {
    const mockState = { count: 99, increment: jest.fn() };
    const wrapper = ({ children }) => (
      <HookOverrides useCounter={() => mockState}>{children}</HookOverrides>
    );

    render(<Counter/>, { wrapper })

    expect(screen.getByText("Count: 99")).toBeInTheDocument();
    fireEvent.click(screen.getByText("Increment"));
    expect(state.increment).toHaveBeenCalledTimes(1)
  });
});

Using defaults

You can specify default overrides to createHookOverridesProvider:

const HookOverrides = createHookOverridesProvider(
  { useCounter },
  { defaults: { useCounter: () => ({ count: 999, increment: action('increment')}) } }
);

You can then use the provider without specifying the override each time:

export const WithDefaults = () => (
  <HookOverrides>
    <Counter /> <!-- renders "Count: 999" -->
  </HookOverrides>
)

Using help mode

When rendering components, it can sometimes be tricky to determine what overridable hooks can be overridden. Enabling the help property will help you identify all overridable hooks that are being rendered.

export const WithDefaults = () => (
  <HookOverrides help>
    <Counter /> <!-- Logs react-overridable-hooks: register "useCounter" by adding it to createOverridesProvider({  }) -->
  </HookOverrides>
)

The help option will log all these missing hooks to the console, making it easy to configure things correctly.

More Examples