ner*_*ger 5 typescript jestjs babel-jest es6-modules ts-jest
使用 TS,可以使用语法导入 CJS 模块的导出,就好像它们被命名为 ES 模块导出一样,例如:
import { sync } from 'glob';
Run Code Online (Sandbox Code Playgroud)
但是,对于 Jest 和 ES 模块,当这种导入方式位于测试文件或测试文件的依赖项中时,它会显示SyntaxError: The requested module 'glob' does not provide an export named 'sync'.
逐一检查所有内容并将它们更改为import glob from 'glob';然后调用glob.sync()似乎可行,但是当将一些遗留内容从另一个测试运行程序迁移到 Jest 时,这可能不是一个选择,因为代码库中有很多这样的导入。
转载于此: https: //github.com/themaskedavenger/tsjestcjerepro
运行 jest:(如https://jestjs.io/docs/ecmascript-modulesnode --experimental-vm-modules node_modules/jest/bin/jest.js中所述),并使用 Jest 配置:
resetMocks: true,
testEnvironment: "node",
testMatch: [
"**/src/**/*.(spec|test).[tj]s?(x)"
],
preset: 'ts-jest/presets/default-esm',
transform: {},
'extensionsToTreatAsEsm': [".ts", ".tsx"],
globals: {
'ts-jest': {
useESM: true,
tsconfig: {
allowSyntheticDefaultImports: true,
declaration: true,
esModuleInterop: true,
jsx: "react",
lib: ["esnext"],
module: "es2020",
moduleResolution: "node",
outDir: "build",
sourceMap: true,
strictNullChecks: true,
target: "ES2020",
}
},
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题,以便import { whatever } from 'whatever';是否适用于 jest 测试文件中的 CJS 模块?
| 归档时间: |
|
| 查看次数: |
1392 次 |
| 最近记录: |