From 711abd8d840e40c19e9ca5ba1cb8996c4ce2f6e7 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 23 Feb 2021 13:54:22 -0700 Subject: [PATCH] refactor: move loggerModule into helpers --- test/constants.test.ts | 14 +++++++++----- test/register.test.ts | 17 +++++------------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/test/constants.test.ts b/test/constants.test.ts index 457f57fae..4d4d327e2 100644 --- a/test/constants.test.ts +++ b/test/constants.test.ts @@ -1,14 +1,16 @@ // 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 { logger } from "../node_modules/@coder/logger" import { commit, getPackageJson, version } from "../src/node/constants" +import { loggerModule } from "./helpers" describe("constants", () => { describe("getPackageJson", () => { - let spy: jest.SpyInstance + // let spy: jest.SpyInstance beforeEach(() => { - spy = jest.spyOn(logger, "warn") + // spy = jest.spyOn(logger, "warn") + jest.mock("@coder/logger", () => loggerModule) }) afterEach(() => { @@ -24,8 +26,10 @@ describe("constants", () => { getPackageJson("./package.json") - expect(spy).toHaveBeenCalled() - expect(spy).toHaveBeenCalledWith(expectedErrorMessage) + // expect(spy).toHaveBeenCalled() + // expect(spy).toHaveBeenCalledWith(expectedErrorMessage) + expect(loggerModule.logger.warn).toHaveBeenCalled() + expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage) }) it("should find the package.json", () => { diff --git a/test/register.test.ts b/test/register.test.ts index 1d45293b4..6ae30376c 100644 --- a/test/register.test.ts +++ b/test/register.test.ts @@ -1,5 +1,5 @@ -import { field, Level } from "@coder/logger" import { JSDOM } from "jsdom" +import { loggerModule } from "./helpers" describe("register", () => { const { window } = new JSDOM() @@ -9,17 +9,6 @@ describe("register", () => { global.location = window.location const mockRegisterFn = jest.fn() - const loggerModule = { - field, - level: Level.Info, - logger: { - debug: jest.fn(), - error: jest.fn(), - info: jest.fn(), - trace: jest.fn(), - warn: jest.fn(), - }, - } beforeAll(() => { Object.defineProperty(global.navigator, "serviceWorker", { @@ -63,5 +52,9 @@ describe("register", () => { expect(mockRegisterFn).toHaveBeenCalled() expect(loggerModule.logger.error).toHaveBeenCalled() + expect(loggerModule.logger.error).toHaveBeenCalledTimes(1) + expect(loggerModule.logger.error).toHaveBeenCalledWith( + `[Service Worker] registration: ${error.message} ${error.stack}`, + ) }) })