From 4c6245a672cc89f1637b0cb2aad041971cb5b1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Wed, 25 Aug 2021 23:37:52 +0000 Subject: [PATCH] jest: mock fetch kinda working --- __tests__/plugins/fetch.test.js | 16 ++++++++++++++++ package-lock.json | 31 +++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 48 insertions(+) diff --git a/__tests__/plugins/fetch.test.js b/__tests__/plugins/fetch.test.js index d7de3bd..4ef9821 100644 --- a/__tests__/plugins/fetch.test.js +++ b/__tests__/plugins/fetch.test.js @@ -1,5 +1,9 @@ const makeServiceWorkerEnv = require('service-worker-mock'); +const fetch = require('jest-fetch-mock'); +fetch.enableMocks() +jest.setMock('node-fetch', fetch); + describe("plugin: fetch", () => { beforeEach(() => { Object.assign(global, makeServiceWorkerEnv()); @@ -10,9 +14,21 @@ describe("plugin: fetch", () => { 'fetch':{} } } + self.log = function(component, ...items) { + console.debug(component + ' :: ', ...items) + } }) test("it should register in LibResilientPlugins", () => { require("../../plugins/fetch.js"); expect(self.LibResilientPlugins[0].name).toEqual('fetch'); }); + + test("it should return data from fetch()", async () => { + require("../../plugins/fetch.js"); + + fetch.mockResponse(JSON.stringify({test: "success"})); + + const returnedJSON = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json'); + expect(returnedJSON).toEqual({test: "success"}) + }); }); diff --git a/package-lock.json b/package-lock.json index 3c9cb21..0e55fd2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1250,6 +1250,15 @@ "safe-buffer": "5.1.2" } }, + "cross-fetch": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.4.tgz", + "integrity": "sha512-1eAtFWdIubi6T4XPy6ei9iUFoKpUkIF971QLN8lIvvvwueI65+Nw5haMNKUwfJxabqlIIDODJKGrQ66gxC0PbQ==", + "dev": true, + "requires": { + "node-fetch": "2.6.1" + } + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -2273,6 +2282,16 @@ "jest-util": "27.0.6" } }, + "jest-fetch-mock": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz", + "integrity": "sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==", + "dev": true, + "requires": { + "cross-fetch": "3.1.4", + "promise-polyfill": "8.2.0" + } + }, "jest-get-type": { "version": "27.0.6", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-27.0.6.tgz", @@ -2890,6 +2909,12 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "dev": true + }, "node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -3086,6 +3111,12 @@ "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", "dev": true }, + "promise-polyfill": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-8.2.0.tgz", + "integrity": "sha512-k/TC0mIcPVF6yHhUvwAp7cvL6I2fFV7TzF1DuGPI8mBh4QQazf36xCKEHKTZKRysEoTQoQdKyP25J8MPJp7j5g==", + "dev": true + }, "prompts": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.1.tgz", diff --git a/package.json b/package.json index 10e59b8..c48163f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "devDependencies": { "eslint": "^7.32.0", "jest": "^27.0.6", + "jest-fetch-mock": "^3.0.3", "service-worker-mock": "^2.0.5" }, "dependencies": {}