Skip to content

Commit bcb98a6

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

File tree

1 file changed

+89
-12
lines changed

1 file changed

+89
-12
lines changed

test/unit/constants.test.ts

+89-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from "fs"
2-
import { commit, getPackageJson, version } from "../../src/node/constants"
2+
// import { commit, getPackageJson } from "../../src/node/constants"
33
import { tmpdir } from "../../test/utils/constants"
44
import { loggerModule } from "../utils/helpers"
55

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

99
describe("constants", () => {
1010
describe("getPackageJson", () => {
11+
const { getPackageJson } = require("../../src/node/constants")
1112
afterEach(() => {
1213
jest.clearAllMocks()
1314
})
1415

1516
afterAll(() => {
1617
jest.restoreAllMocks()
18+
jest.resetModules()
1719
})
1820

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

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

0 commit comments

Comments
 (0)