feat: add test for catching errors in Emitter

This commit is contained in:
Joe Previte 2021-02-10 15:37:43 -07:00
parent b232dcbd4a
commit 80a180079e
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 30 additions and 1 deletions

View File

@ -1,9 +1,10 @@
// Note: we need to import logger from the root // Note: we need to import logger from the root
// because this is the logger used in logError in ../src/common/util // because this is the logger used in logError in ../src/common/util
import { logger } from "../node_modules/@coder/logger" import { logger } from "../node_modules/@coder/logger"
import { Emitter } from "../src/common/emitter" import { Emitter } from "../src/common/emitter"
describe("Emitter", () => { describe("emitter", () => {
let spy: jest.SpyInstance let spy: jest.SpyInstance
beforeEach(() => { beforeEach(() => {
@ -59,6 +60,25 @@ describe("Emitter", () => {
emitter.dispose() emitter.dispose()
}) })
it("should log an error if something goes wrong", async () => {
const HELLO_WORLD = "HELLO_WORLD"
const mockCallback = jest.fn(() => "Mock function called")
const message = "You don't have access to that folder."
const emitter = new Emitter<{ event: string; callback: () => void }>()
const onHelloWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
if (event === HELLO_WORLD) {
callback()
throw new Error(message)
}
}
emitter.event(onHelloWorld)
await emitter.emit({ event: HELLO_WORLD, callback: mockCallback })
})
it("should log an error if something goes wrong", async () => { it("should log an error if something goes wrong", async () => {
const HELLO_WORLD = "HELLO_WORLD" const HELLO_WORLD = "HELLO_WORLD"
const mockCallback = jest.fn(() => "Mock function called") const mockCallback = jest.fn(() => "Mock function called")

View File

@ -0,0 +1,9 @@
describe("serviceWorker", () => {
it("should add the proper eventListeners", () => {
// make sure install, active and fetch were added as event listeners
})
it("should call the proper callbacks", () => {
// somehow test Line 8 with the events waitUntil..
})
})