Closed Bug 1868561 Opened 2 years ago Closed 2 years ago

Change our mocking of fetch to fetch-mock-jest

Categories

(Testing :: PerfCompare, task)

task

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: julienw, Assigned: julienw)

References

Details

(Whiteboard: [pcf])

fetch-mock-jest provides a convenient way to mock the fetch API in Jest. It wraps fetch-mock in a way that's more integrated into Jest.

This is used in the profiler this way:
Setup file (not exactly as advertised in their doc, but works well for us):

beforeEach(function () {
  // Install fetch and fetch-related objects globally.
  // Using the sandbox ensures that parallel tests run properly.
  global.fetch = fetchMock.sandbox();
  global.Headers = Headers;
  global.Request = Request;
  global.Response = Response;
});

https://github.com/firefox-devtools/profiler/blob/99d8f0d90f3802da323dbf26a84508605850ec4d/src/test/setup.js#L34-L41

An example in a test file:

      window.fetch.get(
        expectedUrl,
        makeProfileSerializable(_getSimpleProfile())
      );

https://github.com/firefox-devtools/profiler/blob/99d8f0d90f3802da323dbf26a84508605850ec4d/src/test/store/receive-profile.test.js#L948-L951

This can also be asserted:

      expect(window.fetch).toHaveBeenCalledWith(
        'https://symbolication.services.mozilla.com/symbolicate/v5',
        expect.objectContaining({
          body: expect.stringMatching(/memoryMap.*firefox/),
        })
      );

https://github.com/firefox-devtools/profiler/blob/99d8f0d90f3802da323dbf26a84508605850ec4d/src/test/store/receive-profile.test.js#L909-L914

Assignee: nobody → felash
Status: NEW → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.