diff --git a/src/browser/register.ts b/src/browser/register.ts index 00411110b..b4f4dc6c8 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 106b9487a..4aa2006fd 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