dam*_*ton 60 typescript nest jestjs babeljs puppeteer
我曾经在仅使用 JavaScript 来使用 Jest 时解决了类似的错误,但目前我无法使用 Typescript 来解决类似的错误。
我的所有测试都运行良好,直到我安装了需要@types/jest-environment-puppeteer,@types/puppeteer和的 Puppeteer @types/expect-puppeteer。
安装它们后,puppeteer 测试运行完美,但其他测试开始失败并出现以下错误。
D:\...\api\node_modules\uuid\dist\esm-browser\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
at Object.require (../node_modules/@nestjs/common/decorators/core/injectable.decorator.js:4:16)
Run Code Online (Sandbox Code Playgroud)
我做了什么?
allowJs: true打开tsconfig.json并设置transformIgnorePatternson jest 配置。这样笑话就可以从node_modules/
之后编译文件,此错误停止但测试因另一个奇怪的原因失败。更糟糕的是,测试开始时间增加太多。
所以我保留了allowJs原始设置并更新了笑话配置
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
Run Code Online (Sandbox Code Playgroud)
到
"transform": {
"^.+\\.(t)s$": "ts-jest"
}
Run Code Online (Sandbox Code Playgroud)
所以目前 ts-jest 不编译 js 文件。但我认为我无法让 babel 选择js文件的转换。这些是我的笑话配置:
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t)s$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
},
"transformIgnorePatterns": ["<rootDir>/node_modules/.+.(js|jsx)$"]
}
Run Code Online (Sandbox Code Playgroud)
dam*_*ton 101
感谢此回复: /sf/answers/3788204451/
我开始在谷歌上搜索类似的修复并最终到达这里: https://github.com/uuidjs/uuid/issues/451
这解决了我的问题:https://github.com/uuidjs/uuid/issues/451#issuecomment-1112328417
// jest.config.js
{
//................
moduleNameMapper: {
// Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
"uuid": require.resolve('uuid'),
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用 jest-babel 能解决这个问题,我仍然会很高兴。
因为我必须将笑话配置从package.json一个单独的.js文件中转移。
编辑:根据此github 问题,最新版本的库已解决兼容性问题uuid。
jaw*_*awn 34
只需添加moduleNameMapper:{"^uuid$": "uuid"}到我的 jest.config.js 中即可解决我的问题:
传递依赖uuid:^8.3.2
笑话:28.1.3
角度:14.0.1