diff --git a/test/unit/constants.test.ts b/test/unit/constants.test.ts index 88654457..fe9c26d2 100644 --- a/test/unit/constants.test.ts +++ b/test/unit/constants.test.ts @@ -1,29 +1,22 @@ -import { loggerModule } from "../utils/helpers" - -// jest.mock is hoisted above the imports so we must use `require` here. -jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule) +import { createLoggerMock } from "../utils/helpers" describe("constants", () => { - beforeAll(() => { - jest.clearAllMocks() - jest.resetModules() - }) + let constants: typeof import("../../src/node/constants") + describe("with package.json defined", () => { - const { getPackageJson } = require("../../src/node/constants") - let mockPackageJson = { + const loggerModule = createLoggerMock() + const mockPackageJson = { name: "mock-code-server", description: "Run VS Code on a remote server.", repository: "https://github.com/cdr/code-server", version: "1.0.0", commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b", } - let version = "" - let commit = "" - beforeEach(() => { + beforeAll(() => { + jest.mock("@coder/logger", () => loggerModule) jest.mock("../../package.json", () => mockPackageJson, { virtual: true }) - commit = require("../../src/node/constants").commit - version = require("../../src/node/constants").version + constants = require("../../src/node/constants") }) afterAll(() => { @@ -32,18 +25,18 @@ describe("constants", () => { }) it("should provide the commit", () => { - expect(commit).toBe("f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b") + expect(constants.commit).toBe(mockPackageJson.commit) }) it("should return the package.json version", () => { - expect(version).toBe(mockPackageJson.version) + expect(constants.version).toBe(mockPackageJson.version) }) describe("getPackageJson", () => { it("should log a warning if package.json not found", () => { const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'" - getPackageJson("./package.json") + constants.getPackageJson("./package.json") expect(loggerModule.logger.warn).toHaveBeenCalled() expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage) @@ -52,38 +45,32 @@ describe("constants", () => { it("should find the package.json", () => { // the function calls require from src/node/constants // so to get the root package.json we need to use ../../ - const packageJson = getPackageJson("../../package.json") - expect(Object.keys(packageJson).length).toBeGreaterThan(0) - expect(packageJson.name).toBe("mock-code-server") - expect(packageJson.description).toBe("Run VS Code on a remote server.") - expect(packageJson.repository).toBe("https://github.com/cdr/code-server") + const packageJson = constants.getPackageJson("../../package.json") + expect(packageJson).toStrictEqual(mockPackageJson) }) }) }) describe("with incomplete package.json", () => { - let mockPackageJson = { + const mockPackageJson = { name: "mock-code-server", } - let version = "" - let commit = "" - beforeEach(() => { + beforeAll(() => { jest.mock("../../package.json", () => mockPackageJson, { virtual: true }) - version = require("../../src/node/constants").version - commit = require("../../src/node/constants").commit + constants = require("../../src/node/constants") }) - afterEach(() => { + afterAll(() => { jest.clearAllMocks() jest.resetModules() }) it("version should return 'development'", () => { - expect(version).toBe("development") + expect(constants.version).toBe("development") }) it("commit should return 'development'", () => { - expect(commit).toBe("development") + expect(constants.commit).toBe("development") }) }) }) diff --git a/test/unit/register.test.ts b/test/unit/register.test.ts index 4aa2006f..da69f21d 100644 --- a/test/unit/register.test.ts +++ b/test/unit/register.test.ts @@ -1,6 +1,6 @@ import { JSDOM } from "jsdom" import { registerServiceWorker } from "../../src/browser/register" -import { loggerModule } from "../utils/helpers" +import { createLoggerMock } from "../utils/helpers" import { LocationLike } from "./util.test" describe("register", () => { @@ -21,6 +21,7 @@ describe("register", () => { }) }) + const loggerModule = createLoggerMock() beforeEach(() => { jest.clearAllMocks() jest.mock("@coder/logger", () => loggerModule) @@ -75,6 +76,7 @@ describe("register", () => { }) describe("when navigator and serviceWorker are NOT defined", () => { + const loggerModule = createLoggerMock() beforeEach(() => { jest.clearAllMocks() jest.mock("@coder/logger", () => loggerModule) diff --git a/test/unit/util.test.ts b/test/unit/util.test.ts index 66ea0ce2..e63fcde5 100644 --- a/test/unit/util.test.ts +++ b/test/unit/util.test.ts @@ -11,7 +11,7 @@ import { trimSlashes, normalize, } from "../../src/common/util" -import { loggerModule } from "../utils/helpers" +import { createLoggerMock } from "../utils/helpers" const dom = new JSDOM() global.document = dom.window.document @@ -229,6 +229,8 @@ describe("util", () => { jest.restoreAllMocks() }) + const loggerModule = createLoggerMock() + it("should log an error with the message and stack trace", () => { const message = "You don't have access to that folder." const error = new Error(message) diff --git a/test/utils/helpers.ts b/test/utils/helpers.ts index df84ec1f..f31752b8 100644 --- a/test/utils/helpers.ts +++ b/test/utils/helpers.ts @@ -2,16 +2,21 @@ import * as fs from "fs" import * as os from "os" import * as path from "path" -export const loggerModule = { - field: jest.fn(), - level: 2, - logger: { - debug: jest.fn(), - error: jest.fn(), - info: jest.fn(), - trace: jest.fn(), - warn: jest.fn(), - }, +/** + * Return a mock of @coder/logger. + */ +export function createLoggerMock() { + return { + field: jest.fn(), + level: 2, + logger: { + debug: jest.fn(), + error: jest.fn(), + info: jest.fn(), + trace: jest.fn(), + warn: jest.fn(), + }, + } } /**