wheresrhys/fetch-mock

Fetch is undefined after updating to version 10.0.6 in jest 29.7.0

Closed this issue ยท 3 comments

Hi everyone! ๐Ÿ‘‹

I'm experiencing an issue with fetch-mock while using Jest version 29.7.0.

Problem Description:
After updating fetch-mock from version 9.11.0 to 10.0.6, I'm encountering the following problem:
image

It appears that the issue is due to a lack of native support for fetch. I've tried a few solutions, such as:

jest.mock('node-fetch', () => require('fetch-mock').sandbox())
global.fetch = jest.fn(() =>
  Promise.resolve({
    json: () => Promise.resolve({ test: 100 }),
  }),
) as jest.Mock;

Unfortunately, these solutions didn't work. Adding the whatwg-fetch package did solve the problem, but I don't think it's ideal to add another dependency just for this.

๐Ÿ” Can anyone help me with a better solution?

Thanks in advance! ๐Ÿ™

I get this error as well. When I have testEnvironment: "jsdom" in jest.config.ts, fetchMock.config.Request is undefined:

console.log(fetchMock);

fetchMock {
      _uncompiledRoutes: [],
      routes: [],
      fallbackResponse: undefined,
      config: {
        fallbackToNetwork: false,
        includeContentLength: true,
        sendAsJson: true,
        warnOnFallback: true,
        overwriteRoutes: undefined,
        Request: undefined,
        Response: undefined,
        Headers: [class Headers],
        fetch: undefined
      },
      _calls: [],
      _holdingPromises: [],
      fetchHandler: [Function: bound ],
      restore: [Function: bound ],
      reset: [Function: bound ],
      resetHistory: [Function: bound ],
      resetBehavior: [Function: bound ]
    }

If I remove testEnvironment: "jsdom" then the test runs fine:

console.log(fetchMock);

fetchMock {
      _uncompiledRoutes: [],
      routes: [],
      fallbackResponse: undefined,
      config: {
        fallbackToNetwork: false,
        includeContentLength: true,
        sendAsJson: true,
        warnOnFallback: true,
        overwriteRoutes: undefined,
        Request: [class Request],
        Response: [class Response] {
          error: [Function: error],
          json: [Function: json],
          redirect: [Function: redirect]
        },
        Headers: [class Headers],
        fetch: [Function: fetch]
      },
      _calls: [],
      _holdingPromises: [],
      fetchHandler: [Function: bound ],
      restore: [Function: bound ],
      reset: [Function: bound ],
      resetHistory: [Function: bound ],
      resetBehavior: [Function: bound ]
    }

@gabrieldeespindula Unless something is forcing you on to v10 I would just stick with v9 for now. I'm currently working on a big rewrite to ease compatibility problems more broadly, and don't want to spend time on compatibilty issues with the existing versions if I can avoid it.

Thank you for the quick response! ๐Ÿ™

I'll stick with version 9 for now and wait for the upcoming updates. Looking forward to the improvements in the new version.

Thanks again! ๐Ÿ˜Š