简体   繁体   English

“找不到模块”错误:具有基本配置和项目的多个玩笑配置

[英]'cannot find module' error: multiple jest configurations with base config and projects

I am trying to use different jest configs for different testing (visual/unit).我正在尝试使用不同的笑话配置进行不同的测试(视觉/单元)。 So in package.json, I used --project config option:所以在 package.json 中,我使用了--project配置选项:

"projects": [
  "tests/jest.unit.config.js",
  "tests/jest.visual.config.js"
]

and the scripts:和脚本:

"test": "jest --projects tests/jest.unit.config.js --coverage",
"coverage:visual": "jest --projects tests/jest.visual.config.js --coverage"

When I created two separate configurations in corresponding files, it had worked well.当我在相应的文件中创建两个单独的配置时,它运行良好。 However, I need to use a base configuration to further simplify the two config files and extract what they had in common, so I created a third file jest.config.js in addition to the configs in two files and in package.json:但是,我需要使用基本配置来进一步简化这两个配置文件并提取它们的共同点,因此除了两个文件和 package.json 中的配置之外,我还创建了第三个文件jest.config.js

const config = {
  bail: 0,
  verbose: true,
  collectCoverage: false,
  rootDir: "../",
  moduleFileExtensions: [
    'js',
    'json',
    'ts',
    'tsx'
  ],
  moduleDirectories: [
    'node_modules',
    '<rootDir>'
  ],
  resolver: '<rootDir>/tests/__setup__/resolver.ts',
  setupFilesAfterEnv: [
    '<rootDir>/tests/__setup__/setup-env.ts'
  ],
  testEnvironment: 'jsdom',
  testPathIgnorePatterns: [
    '<rootDir>/tests/__setup__/*',
    '<rootDir>/tests/__fixtures__/*'
  ],
  transformIgnorePatterns: [
    '/node_modules/(?!(@yext/search-headless-react)/)'
  ],
  moduleNameMapper: {
    './SearchCore': '<rootDir>/tests/__fixtures__/core/SearchCore.ts',
    '\\.(css|less|scss|sass)$': 'identity-obj-proxy'
  },
  resetMocks: true,
  restoreMocks: true
};

module.exports = config;

and I wanted to import and extend it into the two configs files I had like following:我想将它导入并扩展到两个配置文件中,如下所示:

const {config} = require('jest.base.config')
module.exports = {
  ...config,
  collectCoverageFrom: [
    'src/**'
  ],
  coverageDirectory: 'coverage/unit',
  testMatch: [
    '<rootDir>/**/*.test.ts?(x)'
  ]
};

However, I get the error:但是,我收到错误:

Error: Cannot find module 'tests/jest.base.config.js'
Require stack:
- /Users/search-ui-react/tests/jest.unit.config.js
- /Users/search-ui-react/node_modules/jest-util/build/requireOrImportModule.js
- /Users/search-ui-react/node_modules/jest-util/build/index.js
- /Users/search-ui-react/node_modules/jest-config/build/getCacheDirectory.js
- /Users/search-ui-react/node_modules/jest-config/build/Defaults.js
- /Users/search-ui-react/node_modules/jest-config/build/normalize.js
- /Users/search-ui-react/node_modules/jest-config/build/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/init/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/cli/index.js
- /Users/search-ui-react/node_modules/jest-cli/build/index.js
- /Users/search-ui-react/node_modules/jest-cli/bin/jest.js
- /Users/search-ui-react/node_modules/jest/bin/jest.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at Object.<anonymous> (/Users/kzhou/search-ui-react/tests/jest.unit.config.js:1:26)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at requireOrImportModule (/Users/kzhou/search-ui-react/node_modules/jest-util/build/requireOrImportModule.js:53:28)

I tried require('jest.base.config.js') as well as require('tests/jest.base.config.js') since it is in a tests folder, but am getting the same thing.我尝试require('jest.base.config.js')require('tests/jest.base.config.js')因为它位于tests文件夹中,但得到了同样的结果。 Also, I could not use ESM syntax with import from\export because I had the error: SyntaxError: Cannot use import statement outside a module .此外,我无法将 ESM 语法与 import from\export 一起使用,因为我遇到了错误: SyntaxError: Cannot use import statement outside a module

Try require('./tests/jest.base.config.js') (dot+slash before dirname)尝试require('./tests/jest.base.config.js') (目录名前的点+斜杠)

If that doesn't work, I'd try getting ESM imports to work and see if that fixes your issues如果这不起作用,我会尝试让 ESM 导入工作,看看是否能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM