kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.93k stars 453 forks source link

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

Closed upupming closed 3 years ago

upupming commented 3 years ago

🐛 Bug Report

To Reproduce

Steps to reproduce the behavior:

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

      at ScriptTransformer._getTransformer (node_modules/@jest/transform/build/ScriptTransformer.js:368:15)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:444:28)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:586:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:624:25)

image

Expected behavior

Link to repo (highly encouraged)

Debug log:

Don't need this yet.

# content of ts-jest.log :

envinfo

System:
    OS: macOS
    Node version: v14.16.0

Npm packages:
    jest: 26.0.0
    ts-jest: 27.0.3
    typescript: 4.3.2
    babel(optional):

Additional context: When I downgrade ts-jest to 26.5.5, everything works perfectly. It seems the recent version of ts-jest breaks the jest command, should we add a process export as the error log said?

ahnpnl commented 3 years ago

You are not using jest 27. ts-jest major version always goes together with Jest major version.

michealroberts commented 3 years ago

@ahnpnl I can confirm that this is happening with the following versions of jest and ts-jest:

"@types/jest": "^27.0.1", "jest": "^27.1.0", "ts-jest": "^27.0.5", "typescript": "^4.4.2"

Test suite failed to run

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

      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)

Is there a ts-jest version to match v27.1.0 ?

N.B. I have uninstalled all versions of jest and ts-jest before re-running npm install prior to submitting this, in case there was a package.json mismatch with node_modules ...

ahnpnl commented 3 years ago

you can try to clear Jest cache. This error doesn't exist on a new project.

michealroberts commented 3 years ago

@ahnpnl Could you run me through that ...

michealroberts commented 3 years ago

FYI ... I've just run npx jest --clearCache and I am still seeing this error.

ahnpnl commented 3 years ago

jest --clearCache is the way to clear Jest cache. I suspect something wrong with your lock file and node_modules in your project that Jest and ts-jest having a mismatch. This can be due to you use a framework/libraries having nested dependencies.

michealroberts commented 3 years ago

@ahnpnl I would say not, here is the devDependencies list for my package-lock.json:

"devDependencies": {
        "@babel/core": "^7.15.5",
        "@fullhuman/postcss-purgecss": "^4.0.3",
        "@rollup/plugin-typescript": "^8.2.5",
        "@storybook/addon-actions": "^6.3.8",
        "@storybook/addon-essentials": "^6.3.8",
        "@storybook/addon-links": "^6.3.8",
        "@storybook/vue3": "^6.3.8",
        "@types/jest": "^27.0.1",
        "@typescript-eslint/eslint-plugin": "^4.18.0",
        "@typescript-eslint/parser": "^4.18.0",
        "@vitejs/plugin-vue": "^1.3.0",
        "@vue/cli-plugin-babel": "4.5.0",
        "@vue/cli-plugin-eslint": "4.5.0",
        "@vue/cli-plugin-typescript": "4.5.0",
        "@vue/cli-plugin-unit-jest": "4.5.0",
        "@vue/cli-service": "4.5.13",
        "@vue/compiler-sfc": "^3.2.6",
        "@vue/eslint-config-typescript": "^7.0.0",
        "@vue/test-utils": "^2.0.0-rc.14",
        "autoprefixer": "^10.3.3",
        "babel-jest": "^27.1.0",
        "babel-loader": "^8.2.2",
        "eslint": "^6.7.2",
        "eslint-plugin-vue": "^7.0.0",
        "jest": "^27.1.0",
        "lint-staged": "^9.5.0",
        "postcss": "^8.3.6",
        "rollup-plugin-postcss": "^4.0.1",
        "tailwindcss": "^2.2.9",
        "ts-jest": "^27.0.5",
        "typescript": "^4.4.2",
        "vite": "^2.5.2",
        "vue": "^3.2.9",
        "vue-loader": "^16.5.0",
        "vue-tsc": "^0.3.0",
        "vue3-jest": "^27.0.0-alpha.2"
      },

I've also deleted node_modules and re-run npm install... and the error persists.

michealroberts commented 3 years ago

Do I need to add anything extra to my "types" config in tsconfig.json? At the moment I have:

"types": [
      "webpack-env",
      "jest"
    ],       

Or perhaps, something is a mis here in my jest.config.js:

module.exports = {
  collectCoverageFrom: [
    "**/*.{js,ts,vue}", 
    "!**/node_modules/**"
  ],
  preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
  testMatch: [
    "**/__tests__/**/*.[jt]s?(x)",
    "**/?(*.)+(spec|test).[jt]s?(x)"
  ],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
    '^.+\\.vue$': 'vue3-jest'
  },
  verbose: true
}
ahnpnl commented 3 years ago

I see you are using vue3-jest alpha which is a Jest transformer for Vue. What is your full stacktrace?

michealroberts commented 3 years ago

@ahnpnl Full stack trace isn't too exciting, but leaving it here for brevity:

Screenshot 2021-09-06 at 13 39 43
ahnpnl commented 3 years ago

I suspect this is problem with vue3-jest. If I look at https://github.com/vuejs/vue-jest/blob/v27.0.0-alpha.2/packages/vue3-jest/lib/index.js#L3 , this way of exporting transformer no longer works with Jest 27.

The right way to export transformer is https://github.com/kulshekhar/ts-jest/blob/master/src/index.ts

cc @lmiller1990

lmiller1990 commented 3 years ago

Can you try @vue/vue3-jest? It's on alpha 1. https://www.npmjs.com/package/@vue/vue3-jest. Should be working with Jest 27, or at least, that's the intention (internal tests are passing, but Jest ecosystem is immense).

master for vue-jest now defaults to Jest 27. I think this process API should be correct: https://github.com/vuejs/vue-jest/blob/master/packages/vue3-jest/lib/index.js

Still figuring out Jest 27 and Vue 3 compat is work in progress but should be pretty stable and the main version in Vue CLI soon. There is some work in Vue CLI to make everything work with Jest 27, you might find some useful info here: https://github.com/vuejs/vue-cli/pull/6627