Display a warning in CLI when detecting jest.mock is used more than once for the same module in one test.
Motivation
When using jest.mock more than once for the same module in one test, which result of these jest.mocks will be used depends on internal implementations of the modules used in the test.
Especially, it can occur when the team separate functions and classes only for tests as helper files.
This feature request seeks to address this by promoting clear guidelines on CLI when detecting this situation.
Example
test.js
import { a } from 'a.js'
import { helper } from 'helper.js'
jest.mock('mockedModule', factory1)
// tests are written below
helper.js
import { b } from 'b.js' // Assume b uses 'mockedModule'
jest.mock('mockedModule',factory2)
// helpers are written below
When executing tests from test.js, the process follows the below order.
Resolve jest.mock in test.js.
If there's an import/require for mockedModule afterwards, factory1 is executed and its result is used.
Resolve import of a from a.js.
Resolve import of helper from helper.js.
Resolve jest.mock in helper.js.
If there's an import/require for mockedModule afterwards, factory2 is executed and its result is used (factory1 is discarded).
Import b from b.js.
Assuming b uses mockedModule, resolve mockedModule
Here, which factory is used for 'mockedModule' in b depends on whether a imports/requires mockedModule.
Hence, when jest.mock spans multiple files, the content of the mock can vary depending on the internal implementation of the modules being imported.
This feature request seeks to address this by promoting clear guidelines on CLI when detecting this situation.
Pitch
It's about Jest and Jest can notice it efficiently.
π Feature Proposal
Display a warning in CLI when detecting
jest.mock
is used more than once for the same module in one test.Motivation
When using
jest.mock
more than once for the same module in one test, which result of thesejest.mock
s will be used depends on internal implementations of the modules used in the test.Especially, it can occur when the team separate functions and classes only for tests as helper files.
This feature request seeks to address this by promoting clear guidelines on CLI when detecting this situation.
Example
test.js
helper.js
When executing tests from test.js, the process follows the below order.
jest.mock
in test.js.mockedModule
afterwards,factory1
is executed and its result is used.a
from a.js.helper
from helper.js.jest.mock
in helper.js.mockedModule
afterwards,factory2
is executed and its result is used (factory1
is discarded).b
from b.js.b
usesmockedModule
, resolvemockedModule
Here, which factory is used for 'mockedModule' in
b
depends on whethera
imports/requiresmockedModule
.Hence, when jest.mock spans multiple files, the content of the mock can vary depending on the internal implementation of the modules being imported.
This feature request seeks to address this by promoting clear guidelines on CLI when detecting this situation.
Pitch
It's about Jest and Jest can notice it efficiently.