类型错误:Jest:转换必须导出“process”函数

jexiocij  于 2023-06-20  发布在  Jest
关注(0)|答案(7)|浏览(175)

最近我的应用程序升级到了Angular 11。Jest已被设置为默认测试框架。运行npm测试会导致以下错误:

● Test suite failed to run

    TypeError: Jest: a transform must export a `process` function.

      20 | @Component({
      21 |     selector: 'app-bx-input-textbox',
    > 22 |     template: require('./bx-input-textbox.component.html'),
         |               ^
      23 |     styles: [String(require('./bx-input-textbox.component.scss'))],
      24 |     providers: [
      25 |         {

      at ScriptTransformer._getTransformer (node_modules/@jest/transform/build/ScriptTransformer.js:357:15)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:419:28)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:523:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at src/app/core/components/bx-input-textbox/bx-input-textbox.component.ts:22:15
      at Object.<anonymous> (src/app/core/components/bx-input-textbox/bx-input-textbox.component.ts:38:1)

//jest.config.js

module.exports = {
  testEnvironment: "jsdom",
  transform: {
    ".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform",
    "\\.(ts|js)$": ['ts-jest']
  },
  setupFilesAfterEnv: ['./jest.setup.js'],
  globals: {    
    "ts-jest": {
      tsConfig: {
        // allow js in typescript
        allowJs: true,
      },
    },
  },
  // If you need to test a specifc file just include it here
  //testMatch: ['**/repository.jobs.spec.ts'],
  testMatch: []
};

包版本“@angular-builders/jest”:“12.1.0”,“@types/jest”:“24.9.1”,“ts-jest”:“26.0.0”,“jest”:“24.9.0”,
我尝试了不同的配置与jest.config.js文件,但无法解决此错误。任何解决此问题的建议都将有所帮助。

vuktfyat

vuktfyat1#

您的ts-jest版本必须与jest版本匹配。

1szpjjfi

1szpjjfi2#

已将jestts-jest升级到v27以修复此问题

e7arh2l6

e7arh2l63#

遇到了同样的问题与“ts-jest”:“^27.0.5”,并通过恢复为“ts-jest”进行修复:“^26.4.2”

rslzwgfq

rslzwgfq4#

您可能还需要升级jest-cli沿着jestts-jest

gorkyyrv

gorkyyrv5#

如果您的ts-jest版本是27.x.x,则必须安装27....的匹配jest版本。只需安装

npm install -D jest@27

jest 27获取最新版本

rqcrx0a6

rqcrx0a66#

擦拭节点模块和.jest文件夹终于摆脱了这一点。

rm -rf ./node_modules && rm -rf .jest && yarn
px9o7tmv

px9o7tmv7#

jest和ts-jest的版本不一定匹配。
真正需要的是版本兼容。
当我遇到这个问题时,我正在使用jest@^28.1.1,令人惊讶的是,ts-jest的版本不存在28.1.1。
我最终升级ts-jest为我找到的最接近的一个。对我来说,我最终得到了以下版本

jest@^28.1.1
ts-ject@28.0.8

相关问题