mirror of https://github.com/coder/code-server.git
refactor: use LogModule from Asher in tests
This commit is contained in:
parent
a44a51462c
commit
027e8e5adf
|
@ -114,12 +114,9 @@ export const getFirstString = (value: string | string[] | object | undefined): s
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logError(prefix: string, err: any): void {
|
export function logError(prefix: string, err: any): void {
|
||||||
console.log("hey joe log error was called")
|
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
console.log("it was an instance of error")
|
|
||||||
logger.error(`${prefix}: ${err.message} ${err.stack}`)
|
logger.error(`${prefix}: ${err.message} ${err.stack}`)
|
||||||
} else {
|
} else {
|
||||||
console.log("not an isntance errro")
|
|
||||||
logger.error(`${prefix}: ${err}`)
|
logger.error(`${prefix}: ${err}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
|
import { field, Level } from "@coder/logger"
|
||||||
import { JSDOM } from "jsdom"
|
import { JSDOM } from "jsdom"
|
||||||
// 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"
|
|
||||||
|
|
||||||
describe("register", () => {
|
describe("register", () => {
|
||||||
const { window } = new JSDOM()
|
const { window } = new JSDOM()
|
||||||
|
@ -10,9 +8,18 @@ describe("register", () => {
|
||||||
global.navigator = window.navigator
|
global.navigator = window.navigator
|
||||||
global.location = window.location
|
global.location = window.location
|
||||||
|
|
||||||
let spy: jest.SpyInstance
|
const mockRegisterFn = jest.fn()
|
||||||
let loggerSpy: jest.SpyInstance
|
const loggerModule = {
|
||||||
const mockRegisterFn = jest.fn(() => console.log("Mock register fn called"))
|
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", {
|
||||||
|
@ -23,10 +30,11 @@ describe("register", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
spy = jest.spyOn(global.navigator.serviceWorker, "register")
|
jest.mock("@coder/logger", () => loggerModule)
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
mockRegisterFn.mockClear()
|
||||||
jest.resetModules()
|
jest.resetModules()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -35,18 +43,14 @@ describe("register", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should register a ServiceWorker", () => {
|
it("should register a ServiceWorker", () => {
|
||||||
spy = jest.spyOn(global.navigator.serviceWorker, "register")
|
|
||||||
// Load service worker like you would in the browser
|
// Load service worker like you would in the browser
|
||||||
require("../src/browser/register")
|
require("../src/browser/register")
|
||||||
// Load service worker like you would in the browser
|
// Load service worker like you would in the browser
|
||||||
// expect spy to have been called
|
expect(mockRegisterFn).toHaveBeenCalled()
|
||||||
expect(spy).toHaveBeenCalled()
|
expect(mockRegisterFn).toHaveBeenCalledTimes(1)
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
|
||||||
spy.mockClear()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should log an error if something doesn't work", () => {
|
it("should log an error if something doesn't work", () => {
|
||||||
loggerSpy = jest.spyOn(logger, "error")
|
|
||||||
const message = "Can't find browser"
|
const message = "Can't find browser"
|
||||||
const error = new Error(message)
|
const error = new Error(message)
|
||||||
|
|
||||||
|
@ -57,10 +61,7 @@ describe("register", () => {
|
||||||
// Load service worker like you would in the browser
|
// Load service worker like you would in the browser
|
||||||
require("../src/browser/register")
|
require("../src/browser/register")
|
||||||
|
|
||||||
expect(spy).toHaveBeenCalled()
|
expect(mockRegisterFn).toHaveBeenCalled()
|
||||||
expect(loggerSpy).toHaveBeenCalled()
|
expect(loggerModule.logger.error).toHaveBeenCalled()
|
||||||
// expect(loggerSpy).toHaveBeenCalledTimes(1)
|
|
||||||
// Because we use logError, it will log the prefix along with the error message
|
|
||||||
// expect(loggerSpy).toHaveBeenCalledWith(`[Service Worker] registration: ${error.message} ${error.stack}`)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue