From cb4185b360af0058e3730d9b502f7db5b605247c Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 23 Feb 2021 16:01:11 -0700 Subject: [PATCH] refactor: use loggerModule in util --- test/constants.test.ts | 2 -- test/util.test.ts | 23 ++++++++--------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/test/constants.test.ts b/test/constants.test.ts index 7751ec53..64514981 100644 --- a/test/constants.test.ts +++ b/test/constants.test.ts @@ -22,8 +22,6 @@ describe("constants", () => { getPackageJson("./package.json") - // expect(spy).toHaveBeenCalled() - // expect(spy).toHaveBeenCalledWith(expectedErrorMessage) expect(loggerModule.logger.warn).toHaveBeenCalled() expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage) }) diff --git a/test/util.test.ts b/test/util.test.ts index 035272b6..2681d877 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -1,8 +1,4 @@ import { JSDOM } from "jsdom" -import { Cookie } from "playwright" -// 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 { arrayify, generateUuid, @@ -18,13 +14,16 @@ import { import { Cookie as CookieEnum } from "../src/node/routes/login" import { hash } from "../src/node/util" import { PASSWORD } from "./constants" -import { checkForCookie, createCookieIfDoesntExist } from "./helpers" +import { checkForCookie, createCookieIfDoesntExist, loggerModule, Cookie } from "./helpers" const dom = new JSDOM() global.document = dom.window.document type LocationLike = Pick +// jest.mock is hoisted above the imports so we must use `require` here. +jest.mock("@coder/logger", () => require("./helpers").loggerModule) + describe("util", () => { describe("normalize", () => { it("should remove multiple slashes", () => { @@ -228,12 +227,6 @@ describe("util", () => { }) describe("logError", () => { - let spy: jest.SpyInstance - - beforeEach(() => { - spy = jest.spyOn(logger, "error") - }) - afterEach(() => { jest.clearAllMocks() }) @@ -248,15 +241,15 @@ describe("util", () => { logError("ui", error) - expect(spy).toHaveBeenCalled() - expect(spy).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`) + expect(loggerModule.logger.error).toHaveBeenCalled() + expect(loggerModule.logger.error).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`) }) it("should log an error, even if not an instance of error", () => { logError("api", "oh no") - expect(spy).toHaveBeenCalled() - expect(spy).toHaveBeenCalledWith("api: oh no") + expect(loggerModule.logger.error).toHaveBeenCalled() + expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no") }) })