【发布时间】:2022-10-05 21:03:05
【问题描述】:
当我只使用 JavaScript 使用 Jest 时,我曾经解决过类似的错误,但目前我无法使用 Typescript 来解决。
在我安装需要@types/jest-environment-puppeteer、@types/puppeteer 和@types/expect-puppeteer 的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)
我做了什么?
allowJs: true on tsconfig.json 并在 jest configs 上设置 transformIgnorePatterns。这样 jest 就可以从node_modules/ 编译文件了
之后,此错误停止,但由于另一个奇怪的原因测试失败。
更糟糕的是,测试开始时间增加了太多。
所以我离开了allowJs,就像原来的设置一样,并更新了 jest 配置
\"transform\": {
\"^.+\\\\.(t|j)s$\": \"ts-jest\"
}
至
\"transform\": {
\"^.+\\\\.(t)s$\": \"ts-jest\"
}
所以目前 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)$\"]
}
标签: typescript jestjs babeljs puppeteer nest