Reputation: 17783
In jest config setup.js
, I mock two modules:
jest.mock('somePath1/Translator');
jest.mock('somePath2/Translator');
When running tests I get this:
jest-haste-map: duplicate manual mock found:
Module name: Translator
Duplicate Mock path: C:\XXX\dev\YYY\src\components\ZZZ\services\__mocks__\Translator.js
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in:
C:\XXX\dev\YYY\src\components\ZZZ\services\__mocks__\Translator.js
Please delete one of the following two files:
C:\XXX\dev\YYY\src\common\services\__mocks__\Translator.js
C:\XXX\dev\YYY\src\components\ZZZ\services\__mocks__\Translator.js
However, both are needed, some tests use service from once locations, other from the second.
How can I fix it?
Upvotes: 5
Views: 1783
Reputation: 2115
I was having the same issue since I have multiple modules with the same name, but on different paths under /src/
.
jest-haste-map: duplicate manual mock found: fetch.service
The following files share their name; please delete one of them:
* <rootDir>/src/modules/order/__mocks__/fetch.service.ts
* <rootDir>/src/modules/restaurant/__mocks__/fetch.service.ts
After some research, I updated the jest.config.ts
so it includes:
modulePathIgnorePatterns: ["<rootDir>/.*/__mocks__"]
this change made the warning to dissapear :9
Upvotes: 5
Reputation: 119
you could try using a beforeEach()
and calling jest.resetModules()
from inside it. Then just mock your dependency on a test by test basis:
describe("some test", () => {
beforeEach(() => {
jest.resetModules();
});
test("Some test", () => {
jest.mock("somePath1/Translator");
// run tests here
});
test("Some other test", () => {
jest.mock("somePath2/Translator");
// run tests here
});
});
This will clear your mocks before each test so you shouldn't have any collisions with file names.
Upvotes: -1