diff --git a/__tests__/plugins/fetch.test.js b/__tests__/plugins/fetch.test.js index 13bd47b..4b8ab9f 100644 --- a/__tests__/plugins/fetch.test.js +++ b/__tests__/plugins/fetch.test.js @@ -1,15 +1,21 @@ const makeServiceWorkerEnv = require('service-worker-mock'); global.fetch = require('node-fetch'); -jest.mock('node-fetch', () => { - const context = { - then: jest.fn().mockImplementation(() => { - const response = { test: "success" }; - return Promise.resolve(response); - }) - }; - return jest.fn(() => context); -}); +jest.mock('node-fetch') + +global.fetch.mockImplementation((url, init) => { + const response = new Response( + new Blob( + [JSON.stringify({ test: "success" })], + {type: "application/json"} + ), + { + status: 200, + statusText: "OK", + headers: [] + }); + return Promise.resolve(response); + }); describe("plugin: fetch", () => { beforeEach(() => { @@ -33,8 +39,8 @@ describe("plugin: fetch", () => { test("it should return data from fetch()", async () => { require("../../plugins/fetch.js"); - const returnedJSON = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); + const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); expect(fetch).toHaveBeenCalled(); - expect(returnedJSON).toEqual({test: "success"}) + expect(await response.json()).toEqual({test: "success"}) }); });