Archived
1
0

refactor: use loggerModule in util

This commit is contained in:
Joe Previte 2021-02-23 16:01:11 -07:00
parent 1c737f1f93
commit cb4185b360
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 8 additions and 17 deletions

View File

@ -22,8 +22,6 @@ describe("constants", () => {
getPackageJson("./package.json") getPackageJson("./package.json")
// expect(spy).toHaveBeenCalled()
// expect(spy).toHaveBeenCalledWith(expectedErrorMessage)
expect(loggerModule.logger.warn).toHaveBeenCalled() expect(loggerModule.logger.warn).toHaveBeenCalled()
expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage) expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage)
}) })

View File

@ -1,8 +1,4 @@
import { JSDOM } from "jsdom" 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 { import {
arrayify, arrayify,
generateUuid, generateUuid,
@ -18,13 +14,16 @@ import {
import { Cookie as CookieEnum } from "../src/node/routes/login" import { Cookie as CookieEnum } from "../src/node/routes/login"
import { hash } from "../src/node/util" import { hash } from "../src/node/util"
import { PASSWORD } from "./constants" import { PASSWORD } from "./constants"
import { checkForCookie, createCookieIfDoesntExist } from "./helpers" import { checkForCookie, createCookieIfDoesntExist, loggerModule, Cookie } from "./helpers"
const dom = new JSDOM() const dom = new JSDOM()
global.document = dom.window.document global.document = dom.window.document
type LocationLike = Pick<Location, "pathname" | "origin"> type LocationLike = Pick<Location, "pathname" | "origin">
// jest.mock is hoisted above the imports so we must use `require` here.
jest.mock("@coder/logger", () => require("./helpers").loggerModule)
describe("util", () => { describe("util", () => {
describe("normalize", () => { describe("normalize", () => {
it("should remove multiple slashes", () => { it("should remove multiple slashes", () => {
@ -228,12 +227,6 @@ describe("util", () => {
}) })
describe("logError", () => { describe("logError", () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(logger, "error")
})
afterEach(() => { afterEach(() => {
jest.clearAllMocks() jest.clearAllMocks()
}) })
@ -248,15 +241,15 @@ describe("util", () => {
logError("ui", error) logError("ui", error)
expect(spy).toHaveBeenCalled() expect(loggerModule.logger.error).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`) expect(loggerModule.logger.error).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`)
}) })
it("should log an error, even if not an instance of error", () => { it("should log an error, even if not an instance of error", () => {
logError("api", "oh no") logError("api", "oh no")
expect(spy).toHaveBeenCalled() expect(loggerModule.logger.error).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith("api: oh no") expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no")
}) })
}) })