TypeError: Jest: a transform must export something
Asked Answered
A

7

27

After configuring jest, node.js and typescript the script npx jest throws the error in a console

TypeError: Jest: a transform must export something.
    at C:\projects\project-api\node_modules\@jest\transform\build\ScriptTransformer.js:386:19
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 0)
    at async ScriptTransformer.loadTransformers (C:\projects\project-api\node_modules\@jest\transform\build\ScriptTransformer.js:378:5)
    at async createScriptTransformer (C:\projects\project-api\node_modules\@jest\transform\build\ScriptTransformer.js:1105:3)
    at async C:\projects\project-api\node_modules\@jest\core\build\TestScheduler.js:283:31
    at async Promise.all (index 0)
    at async TestScheduler.scheduleTests (C:\projects\project-api\node_modules\@jest\core\build\TestScheduler.js:276:5)
    at async runJest (C:\projects\project-api\node_modules\@jest\core\build\runJest.js:387:19)
    at async _run10000 (C:\projects\project-api\node_modules\@jest\core\build\cli\index.js:408:7)

jest.config.js

export default {
  roots: [
    '<rootDir>/src'
  ],
  testMatch: [
    '**/__tests__/**/*.+(ts)',
    '**/?(*.)+(test).+(ts)'
  ],
  transform: {
    '^.+\\.(ts)$': 'ts-jest'
  }
}

Where did I fail to configure it correctly?

Abode answered 25/5, 2021 at 15:27 Comment(1)
I was able to fix this issue by updating the ts-jest to the latest version (e.g. yarn add ts-jest@next). See GitHub issueAbode
E
35

I had your same issue.

For me, it end up it was because I was having old version of following packages:

  • ts-loader
  • ts-jest
  • ts-node
Ewan answered 26/5, 2021 at 12:23 Comment(2)
had a slightly different permutation of this answer, I didn't have ts-loader installed, installing it with yarn add --dev ts-loader solved itObadiah
if using npm then install it with npm i ts-loader --save-dev to avoid inconsistencies between package managersSwiss
G
10

Running yarn add ts-jest@next solved it for me.

Giesecke answered 6/6, 2021 at 13:48 Comment(1)
This answer is dependent on 'next' which will change over time. What version of ts-jest did you install?Unalterable
W
10

I tried to debug Jest by Node debugger (something like node inspect test.js) , and I found the culprit. For me it was jest-svg-transformer package. Apparently, it not compatible with jest@27.

Generally this error raises if some item in transform section of jest.config.js broken.

Wooton answered 25/6, 2021 at 19:49 Comment(1)
Also happened to me. Swapped it out for jest-transform-stubPentaprism
B
6

I had this error and it ended up being a miss-configuration in my jest.config (transform).

I had a property that should have been inside moduleNameMapper and not transform. Once I updated the file, jest executed correctly.

jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  testPathIgnorePatterns: ['/node_modules/', '/dist'],
  collectCoverage: true,
  verbose: true,
  transform: {
    '^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
  },
  moduleNameMapper: {
    '\\.(css|scss|less)$': '<rootDir>/test/styleMock.js',
  },
};

where styleMock.js:

module.exports = {};
Bedad answered 20/7, 2021 at 15:39 Comment(0)
W
6

In my case, I had mismatched versions of jest (major version 26) and ts-jest (major version 27). Downgrading ts-jest to major version 26 solved my issue.

Waylonwayman answered 23/8, 2021 at 21:19 Comment(0)
U
0

My fault was a custom transformer exporting a process function but not a default export, so I've got it working following jest v27 documentation about it: https://jestjs.io/docs/next/code-transformation#typescript-with-type-checking

Utmost answered 5/11, 2021 at 6:1 Comment(0)
A
0

If you upgraded from jest@28 to jest@29, you should change your transformer to return an object {code:"foo"} instead direct string "foo"

jest@28

process(sourceText, sourcePath, options) {
  return `foo+bar`;
}

jest@29

process(sourceText, sourcePath, options) {
  return {code: `foo + bar`};
}

Reference: https://jestjs.io/docs/28.x/upgrading-to-jest28%23transformer

enter image description here

Ardor answered 9/2, 2023 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.