From 10babb4a0c380095e79bca99737130b33c61eb7c Mon Sep 17 00:00:00 2001 From: Asher Date: Mon, 3 May 2021 15:56:53 -0500 Subject: [PATCH] Replace console with logger in sw register --- src/browser/register.ts | 4 ++-- test/unit/register.test.ts | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/browser/register.ts b/src/browser/register.ts index 00411110..b4f4dc6c 100644 --- a/src/browser/register.ts +++ b/src/browser/register.ts @@ -14,7 +14,7 @@ export async function registerServiceWorker(): Promise { await navigator.serviceWorker.register(path, { scope: options.base + "/", }) - console.log("[Service Worker] registered") + logger.info(`[Service Worker] registered`) } catch (error) { logError(logger, `[Service Worker] registration`, error) } @@ -23,5 +23,5 @@ export async function registerServiceWorker(): Promise { if (typeof navigator !== "undefined" && "serviceWorker" in navigator) { registerServiceWorker() } else { - console.error(`[Service Worker] navigator is undefined`) + logger.error(`[Service Worker] navigator is undefined`) } diff --git a/test/unit/register.test.ts b/test/unit/register.test.ts index 106b9487..4aa2006f 100644 --- a/test/unit/register.test.ts +++ b/test/unit/register.test.ts @@ -22,11 +22,11 @@ describe("register", () => { }) beforeEach(() => { + jest.clearAllMocks() jest.mock("@coder/logger", () => loggerModule) }) afterEach(() => { - mockRegisterFn.mockClear() jest.resetModules() }) @@ -39,6 +39,7 @@ describe("register", () => { global.navigator = (undefined as unknown) as Navigator & typeof globalThis global.location = (undefined as unknown) as Location & typeof globalThis }) + it("test should have access to browser globals from beforeAll", () => { expect(typeof global.window).not.toBeFalsy() expect(typeof global.document).not.toBeFalsy() @@ -74,24 +75,24 @@ describe("register", () => { }) describe("when navigator and serviceWorker are NOT defined", () => { - let spy: jest.SpyInstance - beforeEach(() => { - spy = jest.spyOn(console, "error") + jest.clearAllMocks() + jest.mock("@coder/logger", () => loggerModule) }) afterAll(() => { jest.restoreAllMocks() }) - it("should log an error to the console", () => { + it("should log an error", () => { // Load service worker like you would in the browser require("../../src/browser/register") - expect(spy).toHaveBeenCalled() - expect(spy).toHaveBeenCalledTimes(1) - expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined") + expect(loggerModule.logger.error).toHaveBeenCalled() + expect(loggerModule.logger.error).toHaveBeenCalledTimes(1) + expect(loggerModule.logger.error).toHaveBeenCalledWith("[Service Worker] navigator is undefined") }) }) + describe("registerServiceWorker", () => { let serviceWorkerPath: string let serviceWorkerScope: string