Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Jest: a transform must export something

Tags:

jestjs

ts-jest

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?

like image 601
Roman Mahotskyi Avatar asked May 25 '21 15:05

Roman Mahotskyi


People also ask

What does TypeError jest mean?

TypeError: Jest: a transform must export something. · Issue #2612 · kulshekhar/ts-jest · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

What are jest's default options and how to use them?

These options let you control Jest's behavior in your package.json file. The Jest philosophy is to work great by default, but sometimes you just need more configuration power. You can retrieve Jest's default options to expand them if needed: This option tells Jest that all imported modules in your tests should be mocked automatically.

Can jest read global variables defined in globalsetup?

Note: Any global variables that are defined through globalSetup can only be read in globalTeardown. You cannot retrieve globals defined here in your test suites. Note: While code transformation is applied to the linked setup-file, Jest will not transform any code in node_modules.

How to stop jest from running tests after n failures?

The bail config option can be used here to have Jest stop running tests after n failures. Setting bail to true is the same as setting bail to 1. The directory where Jest should store its cached dependency information.


5 Answers

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
like image 174
koalaok Avatar answered Oct 23 '22 13:10

koalaok


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

like image 10
Rotimi Avatar answered Oct 23 '22 14:10

Rotimi


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.

like image 10
EvgeniyRRU Avatar answered Oct 23 '22 14:10

EvgeniyRRU


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.

like image 6
huntzinger92 Avatar answered Oct 23 '22 14:10

huntzinger92


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 = {};
like image 4
Menelaos Kotsollaris Avatar answered Oct 23 '22 12:10

Menelaos Kotsollaris