Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Jest errors - TypeError: Cannot redefine property: performance

Tags:

jestjs

ts-jest

I am working in an Expo project and having an issue in which all of the Jest tests are failing due to a TypeError:

> Test run started at 5/2/2023, 12:02:30 PM <

 FAIL  app/utils/story-building-utils/getCurrentWeek.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/brainstorm-utils/__tests__/slugify.test.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/models/personas/personas.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/models/brainstorms/brainstorms.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/models/wwxd/wwxd.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  test/i18n.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/api/Persona.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/services/api/api-problem.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/storage/storage.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/models/story-building/story-building.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/brainstorm-utils/__tests__/shuffleArray.test.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/brainstorm-utils/__tests__/parseParams.test.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/hooks/useGetBrainstormData/getBrainstormReducer.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/models/auth/auth.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/brainstorm-utils/__tests__/makeTitle.test.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/brainstorm-utils/createUniqueId.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

 FAIL  app/utils/persona-utils/createUniqueId.test.ts
  ● Test suite failed to run

    TypeError: Cannot redefine property: performance

      at Object.<anonymous> (node_modules/react-native/jest/setup.js:407:20)

Test Suites: 17 failed, 17 total
Tests:       0 total
Snapshots:   0 total
Time:        6.061 s
Ran all test suites.

> Test run finished at 5/2/2023, 12:02:37 PM <

It seems as if this problem started occurring after I upgraded from Expo 44 to 48. I have tried to make sure that my package versions are correct for Expo 48 but I continue to have this problem. Any help is appreciated!

like image 277
RenaissanceMan Avatar asked Sep 15 '25 05:09

RenaissanceMan


2 Answers

the node LTS version 18.16.0 is failing, I've downgraded to 18.12 everything is green again ✅

like image 194
laurent loukopoulos Avatar answered Sep 17 '25 20:09

laurent loukopoulos


I am using react-native v0.71.8, and patched (using patch-package) the file node_modules/react-native/jest/setup.js to fix the errorring lines:

--- a/node_modules/react-native/jest/setup.js
+++ b/node_modules/react-native/jest/setup.js
@@ -17,12 +17,11 @@ jest.requireActual('@react-native/polyfills/error-guard');
 
 global.__DEV__ = true;
 
-global.performance = {
-  now: jest.fn(Date.now),
-};
+global.performance ??= {};
+global.performance.now = jest.fn(Date.now);
 
 global.regeneratorRuntime = jest.requireActual('regenerator-runtime/runtime');
-global.window = global;
+global.window ??= global;
 
 global.requestAnimationFrame = function (callback) {
   return setTimeout(callback, 0);

Essentially all I did was change two assignments (=) to nullish coalescing assignments (??=), where they will only set the variable if the current value is null or undefined.

With this patch I was able to use node 18.16 without issue.

like image 27
Geoff Davids Avatar answered Sep 17 '25 20:09

Geoff Davids