- The path in
jest.mock(...)
has no autocompletion (like import/require has) - The path is not covered by refactoring rules of IDEs when moving or renaming files
- The path is not covered by the jest ESLint rules
- The path is not covered by other famous ESLint rules like import/no-unresolved
- The path is not covered by
tsc
when using TypeScript
=> only running the tests could reveal the error
code:
import someFunction from 'some-package';
jest.mock('some-package');
code:
import someFunction from '../some/path/to/file';
jest.mock('../some/path/to/file');
code:
import { someFunction } from 'some-package';
jest.mock('some-package');
code:
import { someFunction } from '../some/path/to/file';
jest.mock('../some/path/to/file');
code:
import * as someFunction from 'some-package';
jest.mock('some-package');
code:
import * as someFunction from '../some/path/to/file';
jest.mock('../some/path/to/file');
code:
const someFunction = require('some-package');
jest.mock('some-package');
code:
const someFunction = require('../some/path/to/file');
jest.mock('../some/path/to/file');
code:
const { someFunction } = require('some-package');
jest.mock('some-package');
code:
const { someFunction } = require('../some/path/to/file');
jest.mock('../some/path/to/file');
code:
jest.mock('some-package');
configuration:
['error', { ignorePaths: ['some-package'] }]
code:
jest.mock('some-package');
configuration:
['error', { ignorePatterns: ['some-.*'] }]
code:
jest.mock('some-package');
configuration:
['error', { ignorePatterns: [/some-.*/ }]
code:
jest.mock('some-package', () => { someFunction: jest.mock() });
configuration:
['error', { ignoreMockWithFactory: true }]
code:
jest.mock('some-package', () => { someFunction: jest.mock() }, { virtual: true });
configuration:
['error', { ignoreVirtual: true }]
code:
jest.mock('some-package');
code:
jest.mock('../path/to/someFile');
code:
jest.mock('some-package', () => { someFunction: jest.fn() });
There are multiple ways to exclude jest.mock
calls.
This might be useful / necessary when:
- using a factory for mocking without the need to override it
- using an external mock file like
__mocks__/<file-to-mock>
without the need to override it
Configuration | Type | Consequence |
---|---|---|
ignorePaths | string[] | Paths added to this array are ignored. So no linting error is thrown, even if this path is not imported into the test file but used in jest.mock |
ignorePatterns | string[] / Regex Array | Basically the same as ignorePaths , but Regular Expressions (as string or Regex) can be used. |
ignoreMockWithFactory | boolean | The second parameter of the jest.mock() function is a so called factory. It allows to directly specify the mock without using .mockReturnValue . If ignoreMockWithFactory is set to true , jest.mock() calls with a factory set, are not checked. |
ignoreVirtual | boolean | The third parameter of jest.mock() allows to pass options. virtual allows mocking of modules, which don't exist in the system. This might be useful for native modules (like e.g. react-native has). |
'jest-mock-path/detect-jest-mock-without-import': ['error', {
ignorePaths: ['some-path'],
ignorePatterns: ['pattern-as-string', /pattern-as-regex/],
ignoreMockWithFactory: true
}],