0

I've encountered an error with my tests from a mysterious way.

Context :

I have a lot of testing files that work for everyone expect me. In the only test that fail, the only issue is this line :

const spyOnFetch = jest.spyOn(global, 'fetch');

I don't know why but I have this error :

 FAIL   Native  ...learner-native-app/src/sections/settings/pages/DeleteAccount/DeleteAccount.test.tsx
  ● Test suite failed to run

    TypeError: Cannot assign to read only property 'fetch' of object '[object global]'

      at Object.restore (node_modules/jest-mock/build/index.js:786:35)
          at Set.forEach (<anonymous>)

(node:34885) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

Do you guys have an idea ?

I tried :

  • Change my node version with nvm
  • Delete homebrew node version
  • Updating npm version
  • Recloning the project
  • Updating yarn

Information :

  • Node version : 21.7.2

  • NPM version : 10.5.1

  • yarn version : 3.8.1

  • Jest version : 29.5.0

3
  • You can ask others for their node version. I would assume that your is too high
    – Konrad
    Commented Apr 10, 2024 at 12:01
  • Already tried, It's not that :((
    – Exiosys
    Commented Apr 10, 2024 at 12:06
  • @Exiosys you got the solution
    – Ankita
    Commented Mar 11 at 9:02

3 Answers 3

1

Jest 29.7.0 fixes this, seems it's something to do with how it sandboxes between tests.

0

Solution :

Node version is not compatible with my tests. I used 20.12 and 21.7 are not compatible.

0

The following worked for me (I added it to the jest.setup.js script)

// Fix issue when mocking fetch
Object.defineProperty(global, 'fetch', {
  writable: true,
})

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.