jest: mock fetch kinda working

merge-requests/9/merge
Michał 'rysiek' Woźniak 2021-08-25 23:37:52 +00:00
rodzic e7958b9504
commit 4c6245a672
3 zmienionych plików z 48 dodań i 0 usunięć

Wyświetl plik

@ -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"})
});
});

31
package-lock.json wygenerowano
Wyświetl plik

@ -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",

Wyświetl plik

@ -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": {}