TypeError: Jest: a transform must export a `process` function
Asked Answered
C

8

52

Recently upgrading my app to Angular 11. Jest has been set up as the default testing framework. Running npm test results in the following error:

● 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: []
};

Package versions "@angular-builders/jest": "12.1.0", "@types/jest": "24.9.1", "ts-jest": "26.0.0", "jest": "24.9.0",

I have tried different configurations with the jest.config.js file but not able to resolve this error. Any pointers to resolve this issue would be helpful.

Chun answered 13/7, 2021 at 4:29 Comment(0)
G
92

your ts-jest version must match the jest version.

Genesisgenet answered 19/10, 2021 at 3:41 Comment(3)
@slideshowp2 actually this does provide an answer to the question and even fixes the issue. The OP has [email protected] while using [email protected] which leads to the error presented.Tartrate
It worked, in my case I tried with "jest": "^27.1.3" and "ts-jest": "^27.1.3"Enlace
It is the second time I came across this answer and a second time it solved my issue. cheers :)Corriecorriedale
C
43

Upgraded jest and ts-jest to v27 to fix this issue

Chun answered 13/7, 2021 at 6:27 Comment(3)
Unfortunately, this did not fix the issue for me.Marlin
@MichealJ.Roberts I had the same issue. See my answer below...Futures
@MichealJ.Roberts it seems that upgrade and clear jest cache has been working for some.Gilliangilliard
C
8

Met the same issue with "ts-jest": "^27.0.5" and fixed by revert to "ts-jest": "^26.4.2"

Calabro answered 7/9, 2021 at 0:7 Comment(1)
worked for angular 11 and node v16Tetragrammaton
F
7

You may also need to upgrade jest-cli, along with jest & ts-jest.

Futures answered 8/9, 2021 at 20:59 Comment(2)
I don't have jest-cli installed, unfortunately.Marlin
you might have it implicitly installed. Make sure that the version lines up using npm ls jest-cli Workhouse
P
4

If your ts-jest version is 27.x.x, you have to install a matching jest version of 27..... Simply install by

npm install -D jest@27

to get the latest version from jest 27

Pacifically answered 8/8, 2022 at 8:47 Comment(0)
E
3

Wiping node modules and .jest folder finally got rid of this.

rm -rf ./node_modules && rm -rf .jest && yarn
Epigynous answered 11/12, 2021 at 2:58 Comment(0)
A
1

Here's what I ended up doing (using this troubleshooting guide as a starting point)

Got my ts-jest, jest, and @types/jest all on the same version number (used whatever was the lowest common denominator).

Was able to find the lowest one by running:

npm list jest --depth=0
npm list ts-jest --depth=0

Installed the lowest common denominator version that matched across the dependencies:

yarn add -D ts-jest@<version> jest@<version> @types/jest<version>

In my case this ended up being 29.1.0:

yarn add -D [email protected] [email protected] @types/[email protected]

Then I removed react-scripts, and reinstalled it.

yarn remove react-scripts
yarn add -D react-scripts

Then that error went away. :)

Alphabetic answered 28/6, 2023 at 20:49 Comment(0)
N
0

It is not necessary that the versions of jest and ts-jest match.

What is really necessary is for the version to be compatible.

When I faced this issue I was using jest@^28.1.1 and to your surprise does not exist version 28.1.1 for ts-jest.

I end up upgrading ts-jest for the closest one I found. For me, I end up with the following versions

jest@^28.1.1
[email protected]
Neurosis answered 8/6, 2023 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.