Jest测试中导入纯ESM模块失败问题求助
Jest纯ESM模块加载问题求助
Jest加载纯ESM模块存在问题,相关议题可见#11373和#9771。根据版本说明,Jest 28.0.0及以上应已支持该特性,但我使用最新版Jest(29.1.2)仍遇到类似#11373的问题,现有资料未给出后续解决方法,特求助。
配置信息
package.json
"main": "./dist/src/index.js", "types": "./dist/src/index.d.ts", "files": [ "dist/src", "!dist/**/*.map" ], "type": "module" "dependencies":{ "jest": "^29.1.2" }
jest.config.js
export default { preset: 'ts-jest', testEnvironment: 'node', testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx|js)$', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/dist'], };
tsconfig.json
{ "compilerOptions": { "module": "ES2020", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "ES2015", "sourceMap": true, "outDir": "./dist", "baseUrl": "./", "incremental": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false, "esModuleInterop": true, "moduleResolution": "node" } }
尝试过的方案及报错
曾尝试设置jest resolver为enhanced-resolver,但出现如下错误:
Error [ERR_REQUIRE_ESM]: require() of ES Module <rootPath>\resolver.js from <rootPath>\node_modules\jest-resolve\build\resolver.js not supported
内容的提问来源于stack exchange,提问作者Zhou2333
相关产品推荐
相关产品推荐

