Skip to content

Commit a2a6122

Browse files
committed
feat: add tests for constants
1 parent 719481e commit a2a6122

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/constants.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Note: we need to import logger from the root
2+
// because this is the logger used in logError in ../src/common/util
3+
import { logger } from "../node_modules/@coder/logger"
4+
import { commit, getPackageJson, version } from "../src/node/constants"
5+
6+
describe("constants", () => {
7+
describe("getPackageJson", () => {
8+
let spy: jest.SpyInstance
9+
10+
beforeEach(() => {
11+
spy = jest.spyOn(logger, "warn")
12+
})
13+
14+
afterEach(() => {
15+
jest.clearAllMocks()
16+
})
17+
18+
afterAll(() => {
19+
jest.restoreAllMocks()
20+
})
21+
22+
it("should log a warning if package.json not found", () => {
23+
const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'"
24+
25+
getPackageJson("./package.json")
26+
27+
expect(spy).toHaveBeenCalled()
28+
expect(spy).toHaveBeenCalledWith(expectedErrorMessage)
29+
})
30+
31+
it("should find the package.json", () => {
32+
// the function calls require from src/node/constants
33+
// so to get the root package.json we need to use ../../
34+
const packageJson = getPackageJson("../../package.json")
35+
expect(Object.keys(packageJson).length).toBeGreaterThan(0)
36+
expect(packageJson.name).toBe("code-server")
37+
expect(packageJson.description).toBe("Run VS Code on a remote server.")
38+
expect(packageJson.repository).toBe("https://github.com/cdr/code-server")
39+
})
40+
})
41+
describe("version", () => {
42+
it("should return the package.json version", () => {
43+
// Source: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39#gistcomment-2896416
44+
const validSemVar = new RegExp("^(0|[1-9]d*).(0|[1-9]d*).(0|[1-9]d*)")
45+
const isValidSemVar = validSemVar.test(version)
46+
expect(version).not.toBe(null)
47+
expect(isValidSemVar).toBe(true)
48+
})
49+
})
50+
51+
describe("commit", () => {
52+
it("should return 'development' if commit is undefined", () => {
53+
// In development, the commit is not stored in our package.json
54+
// But when we build code-server and release it, it is
55+
expect(commit).toBe("development")
56+
})
57+
})
58+
})

0 commit comments

Comments
 (0)