问题描述
使用 TS,可以使用语法导入来自 CJS 模块的导出,就像它们被命名为 ES 模块导出一样,例如:
import { sync } from 'glob';
但是,对于 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
使用:node --experimental-vm-modules node_modules/jest/bin/jest.js
运行 jest(如 https://jestjs.io/docs/ecmascript-modules 中所述),并使用 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",}
},}
有没有办法解决这个问题,让 import { whatever } from 'whatever'
;是否适用于 Jest 测试文件中的 CJS 模块?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)