Fix helpers not working in e2e tests
It errors that jest is not defined so put it behind a function instead of immediately creating the mock (this is probably a better pattern anyway). The constant tests had to be reworked a little. Since the logger mock is hoisted it runs before createLoggerMock is imported. I moved it into a beforeAll which means the require call also needed to be moved there (since we need to mock the logger before requiring the constants or it'll pull the non-mocked logger). This means getPackageJson needs to be a let and assigned afterward. To avoid having to define a type for getPackageJson I just added a let var set to the type of the imported constants file and modified the other areas to use the same paradigm. I also replaced some hardcoded strings with the mocked package.json object.
This commit is contained in:
parent
ad4a70c684
commit
e8443e2602
|
@ -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")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -2,7 +2,11 @@ import * as fs from "fs"
|
|||
import * as os from "os"
|
||||
import * as path from "path"
|
||||
|
||||
export const loggerModule = {
|
||||
/**
|
||||
* Return a mock of @coder/logger.
|
||||
*/
|
||||
export function createLoggerMock() {
|
||||
return {
|
||||
field: jest.fn(),
|
||||
level: 2,
|
||||
logger: {
|
||||
|
@ -12,6 +16,7 @@ export const loggerModule = {
|
|||
trace: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue