Skip to content

Commit 662b5b2

Browse files
authored
Merge pull request #2701 from cdr/add-unit-tests-constants
feat(testing): add unit tests for constants
2 parents 1da773d + f1337d5 commit 662b5b2

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"main": "out/node/entry.js",
3333
"devDependencies": {
34+
"@schemastore/package": "^0.0.6",
3435
"@types/body-parser": "^1.19.0",
3536
"@types/cookie-parser": "^1.4.2",
3637
"@types/express": "^4.17.8",
@@ -62,8 +63,8 @@
6263
"stylelint": "^13.0.0",
6364
"stylelint-config-recommended": "^3.0.0",
6465
"ts-node": "^9.0.0",
65-
"wtfnode": "^0.8.4",
66-
"typescript": "^4.1.3"
66+
"typescript": "^4.1.3",
67+
"wtfnode": "^0.8.4"
6768
},
6869
"resolutions": {
6970
"@types/node": "^12.12.7",

src/node/constants.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { logger } from "@coder/logger"
2+
import { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package"
23
import * as path from "path"
34

4-
let pkg: { version?: string; commit?: string } = {}
5-
try {
6-
pkg = require("../../package.json")
7-
} catch (error) {
8-
logger.warn(error.message)
5+
export function getPackageJson(relativePath: string): JSONSchemaForNPMPackageJsonFiles {
6+
let pkg = {}
7+
try {
8+
pkg = require(relativePath)
9+
} catch (error) {
10+
logger.warn(error.message)
11+
}
12+
13+
return pkg
914
}
1015

16+
const pkg = getPackageJson("../../package.json")
17+
1118
export const version = pkg.version || "development"
1219
export const commit = pkg.commit || "development"
1320
export const rootPath = path.resolve(__dirname, "../..")

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+
})

test/http.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { HttpCode, HttpError } from "../src/common/http"
2+
3+
describe("http", () => {
4+
describe("HttpCode", () => {
5+
it("should return the correct HTTP codes", () => {
6+
expect(HttpCode.Ok).toBe(200)
7+
expect(HttpCode.Redirect).toBe(302)
8+
expect(HttpCode.NotFound).toBe(404)
9+
expect(HttpCode.BadRequest).toBe(400)
10+
expect(HttpCode.Unauthorized).toBe(401)
11+
expect(HttpCode.LargePayload).toBe(413)
12+
expect(HttpCode.ServerError).toBe(500)
13+
})
14+
})
15+
16+
describe("HttpError", () => {
17+
it("should work as expected", () => {
18+
const message = "Bad request from client"
19+
const httpError = new HttpError(message, HttpCode.BadRequest)
20+
21+
expect(httpError.message).toBe(message)
22+
expect(httpError.status).toBe(400)
23+
expect(httpError.details).toBeUndefined()
24+
})
25+
it("should have details if provided", () => {
26+
const details = {
27+
message: "User needs to be signed-in in order to perform action",
28+
}
29+
const message = "Unauthorized"
30+
const httpError = new HttpError(message, HttpCode.BadRequest, details)
31+
32+
expect(httpError.details).toStrictEqual(details)
33+
})
34+
})
35+
})

test/util.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ describe("util", () => {
125125
})
126126

127127
describe("getOptions", () => {
128-
// Things to mock
129-
// logger
130-
// location
131-
// document
132128
beforeEach(() => {
133129
const location: LocationLike = {
134130
pathname: "/healthz",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,11 @@
980980
"@parcel/utils" "^1.11.0"
981981
physical-cpu-count "^2.0.0"
982982

983+
"@schemastore/package@^0.0.6":
984+
version "0.0.6"
985+
resolved "https://registry.yarnpkg.com/@schemastore/package/-/package-0.0.6.tgz#9a76713da1c7551293b7e72e4f387f802bfd5d81"
986+
integrity sha512-uNloNHoyHttSSdeuEkkSC+mdxJXMKlcUPOMb//qhQbIQijXg8x54VmAw3jm6GJZQ5DBtIqGBd66zEQCDCChQVA==
987+
983988
"@stylelint/postcss-css-in-js@^0.37.2":
984989
version "0.37.2"
985990
resolved "https://registry.yarnpkg.com/@stylelint/postcss-css-in-js/-/postcss-css-in-js-0.37.2.tgz#7e5a84ad181f4234a2480803422a47b8749af3d2"

0 commit comments

Comments
 (0)