refactor: move loggerModule into helpers

This commit is contained in:
Joe Previte 2021-02-23 13:54:22 -07:00
parent 027e8e5adf
commit 711abd8d84
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 14 additions and 17 deletions

View File

@ -1,14 +1,16 @@
// Note: we need to import logger from the root // Note: we need to import logger from the root
// because this is the logger used in logError in ../src/common/util // 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 { commit, getPackageJson, version } from "../src/node/constants"
import { loggerModule } from "./helpers"
describe("constants", () => { describe("constants", () => {
describe("getPackageJson", () => { describe("getPackageJson", () => {
let spy: jest.SpyInstance // let spy: jest.SpyInstance
beforeEach(() => { beforeEach(() => {
spy = jest.spyOn(logger, "warn") // spy = jest.spyOn(logger, "warn")
jest.mock("@coder/logger", () => loggerModule)
}) })
afterEach(() => { afterEach(() => {
@ -24,8 +26,10 @@ describe("constants", () => {
getPackageJson("./package.json") getPackageJson("./package.json")
expect(spy).toHaveBeenCalled() // expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledWith(expectedErrorMessage) // expect(spy).toHaveBeenCalledWith(expectedErrorMessage)
expect(loggerModule.logger.warn).toHaveBeenCalled()
expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage)
}) })
it("should find the package.json", () => { it("should find the package.json", () => {

View File

@ -1,5 +1,5 @@
import { field, Level } from "@coder/logger"
import { JSDOM } from "jsdom" import { JSDOM } from "jsdom"
import { loggerModule } from "./helpers"
describe("register", () => { describe("register", () => {
const { window } = new JSDOM() const { window } = new JSDOM()
@ -9,17 +9,6 @@ describe("register", () => {
global.location = window.location global.location = window.location
const mockRegisterFn = jest.fn() 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(() => { beforeAll(() => {
Object.defineProperty(global.navigator, "serviceWorker", { Object.defineProperty(global.navigator, "serviceWorker", {
@ -63,5 +52,9 @@ describe("register", () => {
expect(mockRegisterFn).toHaveBeenCalled() expect(mockRegisterFn).toHaveBeenCalled()
expect(loggerModule.logger.error).toHaveBeenCalled() expect(loggerModule.logger.error).toHaveBeenCalled()
expect(loggerModule.logger.error).toHaveBeenCalledTimes(1)
expect(loggerModule.logger.error).toHaveBeenCalledWith(
`[Service Worker] registration: ${error.message} ${error.stack}`,
)
}) })
}) })