testing-library/react-hooks-testing-library

Invalid hook call when I try to test a custom hook

crtdaniele opened this issue · 3 comments

Hi dev,
When I try to test a custom hook, I have this error:

● useFormesome › test the reset method

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

This is my test:

describe('useFormesome', () => {
  const WrappperContext = React.createContext('');

  const Wrapper: FC = ({ children }) => (
    <WrappperContext.Provider value=''>{children}</WrappperContext.Provider>
  );

  it('test the reset method', async () => {
    const loginForm = {
      email: {
        type: TypeInput.EMAIL,
        inputName: 'email',
        required: true,
        validation: Validations.Email,
        value: '',
        status: Status.TYPING,
      },
    };

    const { result } = renderHook(() => useFormesome('loginForm', loginForm), {
      wrapper: Wrapper,
    });

Why the wrapper doesn't work?

Thanks,
Daniele

The warning explains why you might be seeing this error.

It doesn't look like this bug report has enough info for one of us to reproduce it.

Please provide a CodeSandbox (https://react.new), or a link to a repository on GitHub.

Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

Same issue when migrated jest to vitest, install react-test-renderer works for me.

closing due to inactivity.