Skip to content

Commit 3243bb3

Browse files
authored
Merge pull request #3290 from cdr/jsjoeio/update-constants-test
feat(testing): add test for src/node/constants.ts
2 parents d27b12b + cb5ab48 commit 3243bb3

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"clover"
143143
],
144144
"coveragePathIgnorePatterns": [
145-
"out"
145+
"/out"
146146
],
147147
"coverageThreshold": {
148148
"global": {

test/unit/constants.test.ts

+67-31
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,90 @@
11
import * as fs from "fs"
2-
import { commit, getPackageJson, version } from "../../src/node/constants"
32
import { tmpdir } from "../../test/utils/constants"
43
import { loggerModule } from "../utils/helpers"
54

65
// jest.mock is hoisted above the imports so we must use `require` here.
76
jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)
87

98
describe("constants", () => {
10-
describe("getPackageJson", () => {
11-
afterEach(() => {
12-
jest.clearAllMocks()
9+
beforeAll(() => {
10+
jest.clearAllMocks()
11+
jest.resetModules()
12+
})
13+
describe("with package.json defined", () => {
14+
const { getPackageJson } = require("../../src/node/constants")
15+
let mockPackageJson = {
16+
name: "mock-code-server",
17+
description: "Run VS Code on a remote server.",
18+
repository: "https://github.com/cdr/code-server",
19+
version: "1.0.0",
20+
commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b",
21+
}
22+
let version = ""
23+
let commit = ""
24+
25+
beforeEach(() => {
26+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
27+
commit = require("../../src/node/constants").commit
28+
version = require("../../src/node/constants").version
1329
})
1430

1531
afterAll(() => {
16-
jest.restoreAllMocks()
32+
jest.clearAllMocks()
33+
jest.resetModules()
1734
})
1835

19-
it("should log a warning if package.json not found", () => {
20-
const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'"
21-
22-
getPackageJson("./package.json")
36+
it("should provide the commit", () => {
37+
expect(commit).toBe("f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b")
38+
})
2339

24-
expect(loggerModule.logger.warn).toHaveBeenCalled()
25-
expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage)
40+
it("should return the package.json version", () => {
41+
expect(version).toBe(mockPackageJson.version)
2642
})
2743

28-
it("should find the package.json", () => {
29-
// the function calls require from src/node/constants
30-
// so to get the root package.json we need to use ../../
31-
const packageJson = getPackageJson("../../package.json")
32-
expect(Object.keys(packageJson).length).toBeGreaterThan(0)
33-
expect(packageJson.name).toBe("code-server")
34-
expect(packageJson.description).toBe("Run VS Code on a remote server.")
35-
expect(packageJson.repository).toBe("https://github.com/cdr/code-server")
44+
describe("getPackageJson", () => {
45+
it("should log a warning if package.json not found", () => {
46+
const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'"
47+
48+
getPackageJson("./package.json")
49+
50+
expect(loggerModule.logger.warn).toHaveBeenCalled()
51+
expect(loggerModule.logger.warn).toHaveBeenCalledWith(expectedErrorMessage)
52+
})
53+
54+
it("should find the package.json", () => {
55+
// the function calls require from src/node/constants
56+
// so to get the root package.json we need to use ../../
57+
const packageJson = getPackageJson("../../package.json")
58+
expect(Object.keys(packageJson).length).toBeGreaterThan(0)
59+
expect(packageJson.name).toBe("mock-code-server")
60+
expect(packageJson.description).toBe("Run VS Code on a remote server.")
61+
expect(packageJson.repository).toBe("https://github.com/cdr/code-server")
62+
})
3663
})
3764
})
38-
describe("version", () => {
39-
it("should return the package.json version", () => {
40-
// Source: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39#gistcomment-2896416
41-
const validSemVar = new RegExp("^(0|[1-9]d*).(0|[1-9]d*).(0|[1-9]d*)")
42-
const isValidSemVar = validSemVar.test(version)
43-
expect(version).not.toBe(null)
44-
expect(isValidSemVar).toBe(true)
65+
66+
describe("with incomplete package.json", () => {
67+
let mockPackageJson = {
68+
name: "mock-code-server",
69+
}
70+
let version = ""
71+
let commit = ""
72+
73+
beforeEach(() => {
74+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
75+
version = require("../../src/node/constants").version
76+
commit = require("../../src/node/constants").commit
4577
})
46-
})
4778

48-
describe("commit", () => {
49-
it("should return 'development' if commit is undefined", () => {
50-
// In development, the commit is not stored in our package.json
51-
// But when we build code-server and release it, it is
79+
afterEach(() => {
80+
jest.clearAllMocks()
81+
jest.resetModules()
82+
})
83+
84+
it("version should return 'development'", () => {
85+
expect(version).toBe("development")
86+
})
87+
it("commit should return 'development'", () => {
5288
expect(commit).toBe("development")
5389
})
5490
})

0 commit comments

Comments
 (0)