Code Monkey home page Code Monkey logo

jest-mock-internal-function's Introduction

jest-mock-internal-method ๐Ÿ

Repo for researching mocking functionality in jest.

Problem

Mocking the function that is used by the tested component. In this case is function from src/demo/utils.js named getData which is used by WelcomeMessage component which is rendered inside App. While testing App we want to be able to mock that getData function.

Additionally I had a project that tests were failing suddently after upgrading react-scripts. More on this later.

Solution

Using jest.spyOn fails in CRA 4+ (react-scripts 4.X and later) because of resetMocks set to true

I was trying to recreate sherwin waters solution using jest.spyOn but it was not working. Create React App changed on version 4.X default value of resetMocks to true [source].

To fix this you can redefine this value to false, e.g. in package.json file:

"jest": {
  "resetMocks": false
}

Working example after changing resetMocks to false:

// ./demo/welcomeMessage.js
import { getData } from "./utils"

export const WelcomeMessage = ({name}) => {
    return getData(name)
}

// ./demo/utils.js
const getData = (name) => `Hello ${name}!`;

export { getData }

// ./App.js
import { WelcomeMessage } from "./demo/welcomeMessage";

function App() {
  return (
    <div className="App">
      <h1>My app</h1>
      <p>
        <WelcomeMessage name={'John'} />
      </p>
    </div>
  );
}

export default App;

// ./App.test.js
import { render, screen } from '@testing-library/react';
import App from './App';
import * as utils from "./demo/utils";

jest.spyOn(utils, "getData").mockReturnValue("mocked message");  // this doesn't work as intended

describe('App', () => {
  test('renders header', () => {
    render(<App />);
    expect(screen.getByText(/My app/i)).toBeInTheDocument()
  });

  test('renders correct welcome message', () => {
    render(<App />)
    expect(screen.getByText(/mocked message/i)).toBeInTheDocument()
  });
})

Below I propose other solutions that don't require changing jest config.

Solution using jest.spyOn in beforeEach

You can see this solution on spy-on-before-each branch. Moving the line:

jest.spyOn(utils, "getData").mockReturnValue("mocked message");

inside beforeEach in describe block solves the issue.

import * as utils from "./demo/utils";

describe('App', () => {
  beforeEach(() => {
    jest.spyOn(utils, "getData").mockReturnValue("mocked message");
  })

  test('renders correct welcome message', () => {
    render(<App />)
    expect(screen.getByText(/mocked message/i)).toBeInTheDocument()
  });
})

Why jest.spyOn has to be in beforeEach block? Because resetMocks resets the module registry before running each individual test. That means you must use jest.spyOn inside beforeEach or inside test/it block. Otherwise the mock will not work as it will be cleared. [documentation on resetMocks]

Solution using jest.mock

You can see this solution on main branch.

Instead of using import * as ... we can mock our module with jest.mock. Following test works fine:

import { render, screen } from '@testing-library/react';
import App from './App';

jest.mock('./demo/utils', () => ({
    getData: () => 'mocked message'
}));

describe('App', () => {
  test('renders header', () => {
    render(<App />);
    expect(screen.getByText(/My app/i)).toBeInTheDocument()
  });

  test('renders correct welcome message', () => {
    render(<App />)
    expect(screen.getByText(/mocked message/i)).toBeInTheDocument()
  });
})

Multiple mock implementations with jest.doMock

To mock the function with multiple implementations you can use jest.doMock. However, the it is required to import modules after mocking. More about doMock in jest documentation. Solution based on this repo by marr.

Altought it is doable I would suggest using jest.spyOn. Define them in beforeEach in describe blocks or set them directly in tests blocks.

import { render, screen } from '@testing-library/react';

describe('App', () => {
  beforeEach(() => {
    jest.resetModules();
  })

  describe('mocked', () => {
    beforeEach(() => {
      jest.doMock('./demo/utils', () => ({
        getData: () => 'mocked message'
      }));
    })
    test('renders mocked welcome message', async () => {
      const App = (await import('./App')).default;

      render(<App />)
      expect(screen.getByText(/mocked message/i)).toBeInTheDocument()
    });
  })

  describe('another mocked', () => {
    beforeEach(() => {
      jest.doMock('./demo/utils', () => ({
        getData: () => 'another data'
      }));
    })
    test('renders another mocked welcome message', async () => {
      const App = (await import('./App')).default;

      render(<App />)
      expect(screen.getByText(/another data/i)).toBeInTheDocument()
    });
  })
})

Scripts

  • npm start - to start the project
  • npm test - to run test watcher

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.