Jest 配置 setup.js
,我模拟了两个模块:
jest.mock('somePath1/Translator');
jest.mock('somePath2/Translator');
运行测试时,我得到这个:
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
但是,两者都是必需的,一些测试使用来自一次位置的服务,其他来自第二个位置。
我该如何解决?
最佳答案
我遇到了同样的问题,因为我有多个具有相同名称的模块,但在 /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
经过一番研究,我更新了jest.config.ts
所以它包括:modulePathIgnorePatterns: ["<rootDir>/.*/__mocks__"]
此更改使警告消失:9
关于reactjs - 两个具有相同名称的模块 - 模拟问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50989033/