Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest 28: createJestExpect - TypeError: Cannot read properties of undefined (reading 'extend')

Tags:

jestjs

I migrated from Jest 27 to 28. If I try to start a test now, I just get this error:

  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'extend')

      at createJestExpect (node_modules/@jest/expect/build/index.js:35:19)
      at Object.<anonymous> (node_modules/@jest/expect/build/index.js:47:20)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
      at runJest (node_modules/@jest/core/build/runJest.js:407:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:338:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

Test Suites: 1 failed, 1 total

I can't even debug my tests, because the internal test setup is already failing. Any clue, what's going wrong?

like image 598
Michael Decker Avatar asked Sep 05 '25 17:09

Michael Decker


2 Answers

This is a bug (see here) that appears when the moduleDirectories option includes ".".

To fix this, change "." to "<rootDir>":

moduleDirectories: ["node_modules", "<rootDir>"]

Alternatively, if you use a jest.config.js file, you can change "." to __dirname:

module.exports = {
  moduleDirectories: ["node_modules", __dirname]
};
like image 96
Lupina Avatar answered Sep 11 '25 05:09

Lupina


i had a similiar error using angular together with jest. after updating jest and angular dependencies to the latest version i fixed this error in a slightly different way than @Lupina but using <rootDir> instead of __dirname. this is my current jest.config.js

jest.config.js
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',
  moduleDirectories: ['node_modules', '<rootDir>'],
  transformIgnorePatterns: ['node_modules/(?!@angular|rxjs)'],
  collectCoverage: true,
  coverageDirectory: '<rootDir>/coverage/',
};
like image 39
Fiehra Avatar answered Sep 11 '25 05:09

Fiehra