我的模块以这种方式导入模块:
const { Item, Item1 } = require('@v2/helpers');
这是我的 package.json:
"_moduleAliases": { "@v2/helpers": "src/v2-helpers" }
然后在一个测试文件中,我尝试导入一个以上述方式导入的文件,但由于 Jest 无法导入这些模块而失败。
Test suite failed to run
Cannot find module '@v2/helpers' from 'src/path/to/my-module.js'
怎么办?
答案 0 :(得分:1)
尝试使用 moduleNameMapper
{
"": "... rest of the package.json",
"jest": {
"moduleNameMapper": {
"@v2/helpers": "src/v2-helpers"
}
}
}
(jest.config.js)
const {defaults} = require('jest-config');
const {_moduleAliases} = require('./package.json');
module.exports = async () => {
return {
...defaults,
// rest of the configuration
moduleNameMapper: _moduleAliases
}
}