From 80a180079e47c6c0b28a95d2b33e677a640bea77 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 10 Feb 2021 15:37:43 -0700 Subject: [PATCH] feat: add test for catching errors in Emitter --- test/emitter.test.ts | 22 +++++++++++++++++++++- test/serviceWorker.test.ts | 9 +++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/serviceWorker.test.ts diff --git a/test/emitter.test.ts b/test/emitter.test.ts index 8ff5106a..001b13e0 100644 --- a/test/emitter.test.ts +++ b/test/emitter.test.ts @@ -1,9 +1,10 @@ // Note: we need to import logger from the root // because this is the logger used in logError in ../src/common/util import { logger } from "../node_modules/@coder/logger" + import { Emitter } from "../src/common/emitter" -describe("Emitter", () => { +describe("emitter", () => { let spy: jest.SpyInstance beforeEach(() => { @@ -59,6 +60,25 @@ describe("Emitter", () => { 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 () => { const HELLO_WORLD = "HELLO_WORLD" const mockCallback = jest.fn(() => "Mock function called") diff --git a/test/serviceWorker.test.ts b/test/serviceWorker.test.ts new file mode 100644 index 00000000..32e6effd --- /dev/null +++ b/test/serviceWorker.test.ts @@ -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.. + }) +}) \ No newline at end of file