Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock a cross-fetch ponyfill #122

Open
nikolakov opened this issue Jul 4, 2019 · 4 comments · May be fixed by #226
Open

Mock a cross-fetch ponyfill #122

nikolakov opened this issue Jul 4, 2019 · 4 comments · May be fixed by #226

Comments

@nikolakov
Copy link

nikolakov commented Jul 4, 2019

I'm currently testing a project, which uses cross-fetch ponyfills in the files (i.e. import fetch from "cross-fetch").
this comment in #82 suggests just using cross-fetch as polyfill instead of ponyfill, but that's not an option in my case.
Is it possible to mock the fetch imported as a module?

@ctaylo21
Copy link

ctaylo21 commented Oct 15, 2019

I use unfetch and I was able to get everything working with this setup file:

import { GlobalWithFetchMock } from 'jest-fetch-mock';

const customGlobal = global as GlobalWithFetchMock;
customGlobal.fetch = require('jest-fetch-mock');
customGlobal.fetchMock = customGlobal.fetch;

jest.setMock('unfetch', fetch); // Use this to mock your ponyfilled fetch module

Hope this helps!

@derolf
Copy link

derolf commented Sep 6, 2021

export const mockedFetch = jest.fn() as jest.MockedFunction<typeof fetch>;

jest.mock("cross-fetch", () => ({
  ...jest.requireActual("cross-fetch"),
  __esModule: true,
  fetch: mockedFetch,
  default: mockedFetch,
}));

// must come AFTER mocking!
import fetch, { Response } from "cross-fetch";

export function mockFetchOnce(status: number, json: unknown): void {
  mockedFetch.mockResolvedValueOnce(new Response(JSON.stringify(json), { status }));
}

@zainfathoni
Copy link

zainfathoni commented Sep 6, 2021

I managed to do it by simply doing this in the setup test file:

import fetchMock from "jest-fetch-mock";

jest.setMock("cross-fetch", fetchMock);

You can look at the complete PR where I introduced jest-fetch-mock into our open source project over here: kawalcovid19/wargabantuwarga.com#788

ryanrhee added a commit to ryanrhee/jest-fetch-mock that referenced this issue Jan 26, 2022
@ryanrhee ryanrhee linked a pull request Jan 26, 2022 that will close this issue
@landabaso
Copy link

landabaso commented Apr 22, 2022

This is the recommended way according to jest documentation:

jest.mock('cross-fetch', () => {
  const fetchMock = require('jest-fetch-mock');
  // Require the original module to not be mocked...
  const originalFetch = jest.requireActual('cross-fetch');
  return {
    __esModule: true,
    ...originalFetch,
    default: fetchMock
  };
});

import fetch from 'cross-fetch';
//Do not mock unless we explicitly request it.
fetch.dontMock();

describe('fetch', () => {
  test('fetch', async () => {
    fetch.mockResponse(JSON.stringify({ hello: 'world' }));
    //Activate the mock
    //This will not result into a query to google:
    fetch.doMock();
    expect(await (await fetch('https://www.google.com')).json()).toEqual({
      hello: 'world'
    });
    //Turn off
    fetch.dontMock();
    //Now you can fetch() stuff and the mock won't be applied.
    //This will really query google.com:
    expect((await fetch('https://www.google.com')).status).toEqual(200);
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants