Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between jest.mock and jest.doMock

I want this to be specific to a single test:

  it('should mock the module a single time', () => {
    jest.doMock('../../../../../../../components/HighCharts/HighCharts', () => {
      return () => <div id="mock-line-chart" />;
    });
  })

but it doesnt work. This works for the whole file:

jest.mock('../../../../../../../components/HighCharts/HighCharts', () => {
  return () => <div id="my-special-div" />;
});

Am I not using this right? Where is the difference between doMock and mock. I it suitable to do a module mock for a single test only?

like image 848
Chrissi Grilus Avatar asked Oct 07 '20 13:10

Chrissi Grilus


People also ask

What is the difference between spyOn and mock in Jest?

jest. mock does this automatically for all functions in a module. jest. spyOn does the same thing but allows restoring the original function.

What is __ mocks __ in Jest?

Mocking Node modules​ If the module you are mocking is a Node module (e.g.: lodash ), the mock should be placed in the __mocks__ directory adjacent to node_modules (unless you configured roots to point to a folder other than the project root) and will be automatically mocked. There's no need to explicitly call jest.

What is use Jest mock?

Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function (and the parameters passed in those calls), capturing instances of constructor functions when instantiated with new , and allowing test-time configuration of return values.

Can I use Jest mock inside a test?

mock() doesn't work inside tests, only outside tests. Bookmark this question.

What are module mocks in jest?

Module mocks are a powerful tool to write unit tests with Jest. They allow you to isolate the code under test from its dependencies, leading to focused, less brittle tests. But, as many other powerful tools, module mocks can be tricky at times. In this post we’ll explore how to mock different values for the same module in different tests.

How to mock the Lang dependency in jest?

You can use jest.mock (line 4) to mock the lang dependency. In the example above, the mock module has a current field which is set to a mock function. You want to test both branches of hello, so you use mockReturnValueOnce to make the mock function return "GL" in the first invocation, and "EN" in the second one.

How to restore mocks in jest?

Thus you have to take care of restoration yourself when manually assigning jest.fn (). The restoreMocks configuration option is available to restore mocks automatically before each test. Accepts a function that should be used as the implementation of the mock.

What is jest object in jest?

The Jest Object The jest object is automatically in scope within every test file. The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via import {jest} from '@jest/globals'.


1 Answers

jest.mock is hoisted above import when used at top level and hoisted to the the beginning of the block when used in a block (test function scope, etc), same for jest.unmock. jest.doMock and jest.dontMock serve the same purpose but aren't hoisted.

This would matter for a case like this one:

  it('should mock the module a single time', () => {
    let originalHighCharts = require('.../HighCharts');
    ...
    jest.doMock('.../HighCharts', ...);
    let mockedHighCharts = require('.../HighCharts');
    ...
  })

That doMock allows for specific order is rarely useful in practice because mocked module can be retrieved with jest.requireActual, while the same sequence may not affect other modules that depend on mocked module because of caching.

That doMock and dontMock aren't hoisted allows to use them together for a specified scenario to mock a module for single test:

let HighCharts;
jest.isolateModules(() => {
  jest.doMock('.../HighCharts', ...);;
  HighCharts = require('.../HighCharts');
  jest.dontMock('.../HighCharts');
});

The downside is that dontMock may not be executed if the import fails, so it can affect other tests, this needs to be additionally handled. It may be more straightforward to enforce default module state that is preferable for most tests:

beforeEach(() => {
  jest.unmock('.../HighCharts');
  jest.resetModules();
});
like image 92
Estus Flask Avatar answered Oct 11 '22 14:10

Estus Flask