Skip to content

Commit 027106a

Browse files
committed
feat(testing): add test for constants "version" and commit
1 parent af5a1c9 commit 027106a

File tree

1 file changed

+88
-12
lines changed

1 file changed

+88
-12
lines changed

test/unit/constants.test.ts

+88-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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

@@ -8,12 +7,14 @@ jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)
87

98
describe("constants", () => {
109
describe("getPackageJson", () => {
10+
const { getPackageJson } = require("../../src/node/constants")
1111
afterEach(() => {
1212
jest.clearAllMocks()
1313
})
1414

1515
afterAll(() => {
1616
jest.restoreAllMocks()
17+
jest.resetModules()
1718
})
1819

1920
it("should log a warning if package.json not found", () => {
@@ -36,20 +37,95 @@ describe("constants", () => {
3637
})
3738
})
3839
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)
40+
describe("with package.json.version defined", () => {
41+
let mockPackageJson = {
42+
name: "mock-code-server",
43+
version: "1.0.0",
44+
}
45+
let version = ""
46+
47+
beforeEach(() => {
48+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
49+
version = require("../../src/node/constants").version
50+
})
51+
52+
afterEach(() => {
53+
jest.resetAllMocks()
54+
jest.resetModules()
55+
})
56+
57+
it("should return the package.json version", () => {
58+
// Source: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39#gistcomment-2896416
59+
const validSemVar = new RegExp("^(0|[1-9]d*).(0|[1-9]d*).(0|[1-9]d*)")
60+
const isValidSemVar = validSemVar.test(version)
61+
expect(version).not.toBe(null)
62+
expect(isValidSemVar).toBe(true)
63+
expect(version).toBe("1.0.0")
64+
})
4565
})
46-
})
66+
describe("with package.json.version missing", () => {
67+
let mockPackageJson = {
68+
name: "mock-code-server",
69+
}
70+
let version = ""
4771

72+
beforeEach(() => {
73+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
74+
version = require("../../src/node/constants").version
75+
})
76+
77+
afterEach(() => {
78+
jest.resetAllMocks()
79+
jest.resetModules()
80+
})
81+
82+
it("should return 'development'", () => {
83+
expect(version).toBe("development")
84+
})
85+
})
86+
})
4887
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
52-
expect(commit).toBe("development")
88+
describe("with package.json.commit defined", () => {
89+
let mockPackageJson = {
90+
name: "mock-code-server",
91+
commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b",
92+
}
93+
let commit = ""
94+
95+
beforeEach(() => {
96+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
97+
commit = require("../../src/node/constants").commit
98+
})
99+
100+
afterEach(() => {
101+
jest.resetAllMocks()
102+
jest.resetModules()
103+
})
104+
105+
it("should return the package.json.commit", () => {
106+
// Source: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39#gistcomment-2896416
107+
expect(commit).toBe("f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b")
108+
})
109+
})
110+
describe("with package.json.commit missing", () => {
111+
let mockPackageJson = {
112+
name: "mock-code-server",
113+
}
114+
let commit = ""
115+
116+
beforeEach(() => {
117+
jest.mock("../../package.json", () => mockPackageJson, { virtual: true })
118+
commit = require("../../src/node/constants").commit
119+
})
120+
121+
afterEach(() => {
122+
jest.resetAllMocks()
123+
jest.resetModules()
124+
})
125+
126+
it("should return 'development'", () => {
127+
expect(commit).toBe("development")
128+
})
53129
})
54130
})
55131
})

0 commit comments

Comments
 (0)