简体   繁体   English

Jest 无法导入使用 _moduleAliases 导入模块的模块

[英]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

  • either in package.json在 package.json 中
{
  "": "... rest of the package.json",

  "jest": {
    "moduleNameMapper": {
      "@v2/helpers": "src/v2-helpers"
    }
  }
}
  • or in the jest configuration (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.

相关问题 Jest Spyon在“需要”模拟模块时起作用,而在“导入”时不起作用 - Jest spyon works when 'require' mocked module and doesn't work when 'import' 如何使用npm将自定义模块导入babel-jest测试? - How to import custom modules to babel-jest tests using npm? 使用Jest从节点模块导入模拟函数的正确方法 - Correct way to import mocked functions from node modules using Jest Jest Typescript 带有 ES 模块 in node_modules 错误 - 必须使用导入来加载 ES 模块: - Jest Typescript with ES Module in node_modules error - Must use import to load ES Module: 如何使用 Jest 模拟 ES6 模块导入? - How can I mock an ES6 module import using Jest? 如何使用 jest 测试模块,该模块导入作为构造函数的外部库 - How to test a module using jest which imports an external library which is a constructor SyntaxError:无法使用 jest 在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module using jest 在玩笑中运行测试时,node_modules 导入未定义 - When runing tests in jest, node_modules imports gets undefined 开玩笑无法解析节点模块子路径模式导入 - jest cannot resolve node modules subpath pattern imports 无法使用 ES6 模块在 nodejs 中导入模块 - Unable to import module in nodejs using ES6 Modules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM