How to mock/spy useState hook in jest?

24,140

Solution 1

You need to use React.useState instead of the single import useState.

I think is about how the code gets transpiled, as you can see in the babel repl the useState from the single import ends up being different from the one of the module import

_react.useState // useState
_react.default.useState // React.useState;

So you spy on _react.default.useState but your component uses _react.useState. It seems impossible to spyOn a single import since you need the function to belong to an object, here is a very extensive guide that explains the ways of mocking/spying modules https://github.com/HugoDF/mock-spy-module-import

And as @Alex Mackay mentioned, you probably want to change your mindset about testing react components, moving to react-testing-library is recommended, but if you really need to stick to enzyme you don't need to go that far as to mock react library itself

Solution 2

diedu's answer led me the right direction and I came up with this solution:

  1. Mock use state from react to return a jest.fn() as useState: 1.1 Also import useState immediately after - that will now be e jest mock (returned from the jest.fn() call)

jest.mock('react', ()=>({
  ...jest.requireActual('react'),
  useState: jest.fn()
}))
import { useState } from 'react';
  1. Later on in the beforeEach, set it to the original useState, for all the cases where you need it to not be mocked

describe("Test", ()=>{
  beforeEach(()=>{
    useState.mockImplementation(jest.requireActual('react').useState);
    //other preperations
  })
  //tests
})
  1. In the test itself mock it as needed:

it("Actual test", ()=>{
  useState.mockImplementation(()=>["someMockedValue", someMockOrSpySetter])
})

Parting notes: While it might be conceptually somewhat wrong to get your hands dirty inside the "black box" one is unit testing, it is indeed super useful at times to do it.

Solution 3

Annoyingly Codesandbox is currently having trouble with its testing module so I can't post a working example but I will try to explain why mocking useState is generally a bad thing to do.

The user doesn't care if useState has been called, they care about when I click increment the count should increase by one therefore that is what you should be testing for.

// App
import React, { useState } from "react";
export default function App() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={() => setCount((prev) => prev + 1)}>Increment</button>
    </div>
  );
}
// Tests
import React from "react";
import App from "./App";
import { screen, render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

describe("App should", () => {
  it('increment count value when "Increment" btn clicked', () => {
    // Render the App
    render(<App />);
    // Get the count in the same way the user would, by looking for 'Count'
    let count = screen.getByText(/count:/);
    // As long as the h1 element contains a '0' this test will pass
    expect(count).toContain(0);
    // Once again get the button in the same the user would, by the 'Increment'
    const button = screen.getByText(/increment/);
    // Simulate the click event
    userEvent.click(button);
    // Refetch the count
    count = screen.getByText(/count:/);
    // The 'Count' should no longer contain a '0'
    expect(count).not.toContain(0);
    // The 'Count' should contain a '1'
    expect(count).toContain(1);
  });
  // And so on...
  it('reset count value when "Reset" btn is clicked', () => {});
  it('decrement count value when "Decrement" btn is clicked', () => {});
});

Definitely check out @testing-library if you are interested in this style of testing. I switched from enzyme about 2 years ago and haven't touched it since.

Solution 4

just you need to import React in your test file like:

import * as React from 'react';

after that you can use the mock function.

import * as React from 'react';

:
:
it('increment counter correctlry', () => {
    let wrapper = shallow(<Counter/>);
    const setState = jest.fn();
    const useStateSpy = jest.spyOn(React, 'useState');

    useStateSpy.mockImplementation((init) => [init, setState]);
     const button = wrapper.find("button")
     button.simulate('click');
     expect(setState).toHaveBeenCalledWith(1);
})

Solution 5

you should use React.useState() instead useState(), But there are other ways... in React you can set useState without React with this config

// setupTests.js
    const { configure } = require('enzyme')
    const Adapter = require('@wojtekmaj/enzyme-adapter-react-17')
    const { createSerializer } = require('enzyme-to-json')

    configure({ adapter: new Adapter() });
    expect.addSnapshotSerializer(createSerializer({
        ignoreDefaultProps: true,
        mode: 'deep',
        noKey: true,
    }));
import React, { useState } from "react";

    const Home = () => {

        const [count, setCount] = useState(0);

        return (
            <section>

                <h3>{count}</h3>
                <span>
                    <button id="count-up" type="button" onClick={() => setCount(count + 1)}>Count Up</button>
                    <button id="count-down" type="button" onClick={() => setCount(count - 1)}>Count Down</button>
                    <button id="zero-count" type="button" onClick={() => setCount(0)}>Zero</button>
                </span>
            </section>
        );

    }

    export default Home;

// index.test.js

    import { mount } from 'enzyme';
    import Home from '../';
    import React, { useState as useStateMock } from 'react';


    jest.mock('react', () => ({
        ...jest.requireActual('react'),
        useState: jest.fn(),
    }));

    describe('<Home />', () => {
        let wrapper;

        const setState = jest.fn();

        beforeEach(() => {
            useStateMock.mockImplementation(init => [init, setState]);
            wrapper = mount(<Home />);
        });

        afterEach(() => {
            jest.clearAllMocks();
        });

        describe('Count Up', () => {
            it('calls setCount with count + 1', () => {
                wrapper.find('#count-up').simulate('click');
                expect(setState).toHaveBeenCalledWith(1);
            });
        });

        describe('Count Down', () => {
            it('calls setCount with count - 1', () => {
                wrapper.find('#count-down').props().onClick();
                expect(setState).toHaveBeenCalledWith(-1);
            });
        });

        describe('Zero', () => {
            it('calls setCount with 0', () => {
                wrapper.find('#zero-count').props().onClick();
                expect(setState).toHaveBeenCalledWith(0);
            });
        });
    });
Share:
24,140
Saher Elgendy
Author by

Saher Elgendy

Updated on July 12, 2022

Comments

  • Saher Elgendy
    Saher Elgendy almost 2 years

    I am trying to spy on useState React hook but i always get the test failed

    This is my React component:

    const Counter= () => {
        const[counter, setCounter] = useState(0);
    
        const handleClick=() => {
            setCounter(counter + 1);
        }
    
        return (
            <div>
                <h2>{counter}</h2>
                <button onClick={handleClick} id="button">increment</button>
            </div>
        )
    }
    

    counter.test.js:

    it('increment counter correctlry', () => {
        let wrapper = shallow(<Counter/>);
        const setState = jest.fn();
        const useStateSpy = jest.spyOn(React, 'useState');
    
        useStateSpy.mockImplementation((init) => [init, setState]);
         const button = wrapper.find("button")
         button.simulate('click');
         expect(setState).toHaveBeenCalledWith(1);
    })
    

    Unfortunately this doesn't work and i get the test failed with that message:

    expected 1
    Number of calls: 0
    
  • Saher Elgendy
    Saher Elgendy over 3 years
    Thank you, that is a good answer, but i tried the same solution using enzyme but also failed, simulating the button click then logging the counter also printed 0, i really will migrate to react testing library, thank you
  • Saher Elgendy
    Saher Elgendy over 3 years
    Why when run this code i get the test fail with that message expected 1 received <h2>counter 1</h2>
  • Alex Mckay
    Alex Mckay over 3 years
    Add this library and use the matcher .toHaveTextContent. See here specifically
  • Alex Mckay
    Alex Mckay over 3 years
    Alternatively, use count.innerHTML or count.textContent
  • AxeEffect
    AxeEffect over 3 years
    You can need to mock useState not to know whether it has been called but to prevent errors and warnings on console (like wrap your component in act()) and other issues when useState is called. So mocking it to only return dumb data under control is an efficient way of preventing these issues.
  • jsibs
    jsibs over 2 years
    Thanks for the post, William. How does this work with multiple useStates in a component?
  • William Penagos
    William Penagos over 2 years
    better is don't use multiple useStates, you should use one with a middle function like: ``` setStateFn = (key, value) => setState((oldState) => ({ ...oldState, [key]: value })); ```
  • jsibs
    jsibs over 2 years
    can you please update you're answer so that it is inclusive of the case? I think it would help others. Thank you for the time.
  • Michael Freidgeim
    Michael Freidgeim about 2 years
    Similar article linkedin.com/pulse/…