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