Skip to content

Commit 80a1800

Browse files
committed
feat: add test for catching errors in Emitter
1 parent b232dcb commit 80a1800

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

test/emitter.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Note: we need to import logger from the root
22
// because this is the logger used in logError in ../src/common/util
33
import { logger } from "../node_modules/@coder/logger"
4+
45
import { Emitter } from "../src/common/emitter"
56

6-
describe("Emitter", () => {
7+
describe("emitter", () => {
78
let spy: jest.SpyInstance
89

910
beforeEach(() => {
@@ -59,6 +60,25 @@ describe("Emitter", () => {
5960
emitter.dispose()
6061
})
6162

63+
it("should log an error if something goes wrong", async () => {
64+
const HELLO_WORLD = "HELLO_WORLD"
65+
const mockCallback = jest.fn(() => "Mock function called")
66+
const message = "You don't have access to that folder."
67+
68+
const emitter = new Emitter<{ event: string; callback: () => void }>()
69+
70+
const onHelloWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
71+
if (event === HELLO_WORLD) {
72+
callback()
73+
throw new Error(message)
74+
}
75+
}
76+
77+
emitter.event(onHelloWorld)
78+
79+
await emitter.emit({ event: HELLO_WORLD, callback: mockCallback })
80+
})
81+
6282
it("should log an error if something goes wrong", async () => {
6383
const HELLO_WORLD = "HELLO_WORLD"
6484
const mockCallback = jest.fn(() => "Mock function called")

test/serviceWorker.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe("serviceWorker", () => {
2+
it("should add the proper eventListeners", () => {
3+
// make sure install, active and fetch were added as event listeners
4+
})
5+
6+
it("should call the proper callbacks", () => {
7+
// somehow test Line 8 with the events waitUntil..
8+
})
9+
})

0 commit comments

Comments
 (0)