[英]Jest doesn't achieve to import module who imports modules using _moduleAliases
My module imports modules in that manner:我的模块以这种方式导入模块:
const { Item, Item1 } = require('@v2/helpers');
This is my package.json:这是我的 package.json:
"_moduleAliases": { "@v2/helpers": "src/v2-helpers" }
Then in a test file I try to import a file who imports in the above mentioned manner and it gets failed because Jest cannot import those modules.然后在一个测试文件中,我尝试导入一个以上述方式导入的文件,但它失败了,因为 Jest 无法导入这些模块。
Test suite failed to run
Cannot find module '@v2/helpers' from 'src/path/to/my-module.js'
What to do?该怎么办?
Try using moduleNameMapper尝试使用moduleNameMapper
{
"": "... rest of the package.json",
"jest": {
"moduleNameMapper": {
"@v2/helpers": "src/v2-helpers"
}
}
}
(jest.config.js)
(jest.config.js)
const {defaults} = require('jest-config');
const {_moduleAliases} = require('./package.json');
module.exports = async () => {
return {
...defaults,
// rest of the configuration
moduleNameMapper: _moduleAliases
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.