Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b56e65

Browse files
committedFeb 26, 2021
feat(testing): add serviceWorker tests
1 parent ee0973c commit 6b56e65

File tree

7 files changed

+108
-6
lines changed

7 files changed

+108
-6
lines changed
 

‎ci/dev/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ main() {
66

77
eslint --max-warnings=0 --fix $(git ls-files "*.ts" "*.tsx" "*.js" | grep -v "lib/vscode")
88
stylelint $(git ls-files "*.css" | grep -v "lib/vscode")
9-
tsc --noEmit
9+
tsc --noEmit --skipLibCheck
1010
shellcheck -e SC2046,SC2164,SC2154,SC1091,SC1090,SC2002 $(git ls-files "*.sh" | grep -v "lib/vscode")
1111
if command -v helm && helm kubeval --help > /dev/null; then
1212
helm kubeval ci/helm-chart

‎test/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"#": "We must put jest in a sub-directory otherwise VS Code somehow picks up the types and generates conflicts with mocha.",
3+
"scripts": {
4+
"postinstall": "./scripts/patch.sh"
5+
},
36
"devDependencies": {
47
"@types/jest": "^26.0.20",
58
"@types/jsdom": "^16.2.6",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--- node_modules/service-worker-mock/fetch.js 2021-02-18 13:47:55.000000000 -0700
2+
+++ patches/service-worker-mock-fixed.js 2021-02-18 15:49:47.000000000 -0700
3+
@@ -1,8 +1,11 @@
4+
module.exports = async (request) => {
5+
+ const Response = require('./models/Response');
6+
const response = new Response('Response from service-worker-mock/fetch.js', {
7+
status: 200,
8+
statusText: 'ok.'
9+
});
10+
- response.url = request.url;
11+
+ if (request && request.url) {
12+
+ response.url = request.url;
13+
+ }
14+
return response;
15+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- node_modules/service-worker-mock/models/Response.js 2021-02-18 13:47:55.000000000 -0700
2+
+++ patches/service-worker-mock-response.js 2021-02-18 15:57:12.000000000 -0700
3+
@@ -1,5 +1,6 @@
4+
// stubs https://developer.mozilla.org/en-US/docs/Web/API/Response
5+
const Body = require('./Body');
6+
+const Blob = require('./Blob');
7+
const Headers = require('./Headers');
8+
9+
const isSupportedBodyType = (body) =>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- node_modules/@types/service-worker-mock/index.d.ts 2021-02-18 13:51:50.000000000 -0700
2+
+++ patches/service-workertypes.d.ts 2021-02-18 16:30:01.000000000 -0700
3+
@@ -3,6 +3,11 @@
4+
// Definitions by: Remco Haszing <https://github.com/remcohaszing>
5+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6+
// TypeScript Version: 2.8
7+
+// https://gist.github.com/shqld/32df51a4a4ed429f2c76e4e2cfdf6f96#gistcomment-2793376
8+
+// excludes default libs such as 'dom' conflicting with 'webworker'
9+
+/// <reference no-default-lib="true"/>
10+
+/// <reference lib="esnext" />
11+
+/// <reference lib="webworker" />
12+
13+
export = makeServiceWorkerEnv;
14+
declare function makeServiceWorkerEnv(): WorkerGlobalScope;

‎test/scripts/patch.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
apply_service_worker_mock_patches() {
5+
# The `service-worker-mock` package is no longer maintained
6+
# so we have to apply patches ourselves
7+
8+
# This patch fixes an undefined error in fetch.js and adds a missing import
9+
patch --forward node_modules/service-worker-mock/fetch.js < patches/service-worker-mock-fetch.patch
10+
11+
# This patch adds a missing import
12+
patch --forward node_modules/service-worker-mock/models/Response.js < patches/service-worker-mock-response.patch
13+
14+
# This patch fixes the types declaration file
15+
# See discussion:
16+
patch --forward node_modules/@types/service-worker-mock/index.d.ts < patches/service-worker-types.patch
17+
}
18+
19+
main() {
20+
echo -e "🔨 Applying patches..."
21+
apply_service_worker_mock_patches
22+
23+
echo -e "✅ Patches applied successfully."
24+
}
25+
26+
main "$@"

‎test/serviceWorker.test.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1+
import makeServiceWorkerEnv = require("service-worker-mock")
2+
const makeFetchMock = require("service-worker-mock/fetch")
3+
14
describe("serviceWorker", () => {
2-
it("should add the proper eventListeners", () => {
3-
// make sure install, active and fetch were added as event listeners
5+
let spy: jest.SpyInstance
6+
beforeEach(() => {
7+
Object.assign(
8+
global,
9+
makeServiceWorkerEnv(),
10+
makeFetchMock(),
11+
// If you're using sinon ur similar you'd probably use below instead of makeFetchMock
12+
// fetch: sinon.stub().returns(Promise.resolve())
13+
)
14+
jest.resetModules()
15+
16+
spy = jest.spyOn(console, "log")
17+
})
18+
19+
afterEach(() => {
20+
jest.restoreAllMocks()
21+
spy.mockRestore()
22+
})
23+
24+
it("should add listeners", () => {
25+
require("../src/browser/serviceWorker.ts")
26+
const _self = (self as unknown) as WorkerGlobalScope
27+
expect(_self.listeners.get("install")).toBeDefined()
28+
expect(_self.listeners.get("activate")).toBeDefined()
29+
expect(_self.listeners.get("fetch")).toBeDefined()
30+
})
31+
32+
it("should call the proper callbacks for 'install'", async () => {
33+
require("../src/browser/serviceWorker.ts")
34+
await self.trigger("install")
35+
expect(spy).toHaveBeenCalledWith("[Service Worker] installed")
436
})
37+
it("should call the proper callbacks for 'activate'", async () => {
38+
require("../src/browser/serviceWorker.ts")
39+
await self.trigger("activate")
540

6-
it("should call the proper callbacks", () => {
7-
// somehow test Line 8 with the events waitUntil..
41+
// Activate serviceWorker
42+
expect(spy).toHaveBeenCalledWith("[Service Worker] activated")
843
})
9-
})
44+
})

0 commit comments

Comments
 (0)
Please sign in to comment.