|
1 | 1 | import * as fs from "fs"
|
2 |
| -import { commit, getPackageJson, version } from "../../src/node/constants" |
3 | 2 | import { tmpdir } from "../../test/utils/constants"
|
4 | 3 | import { loggerModule } from "../utils/helpers"
|
5 | 4 |
|
6 | 5 | // jest.mock is hoisted above the imports so we must use `require` here.
|
7 | 6 | jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)
|
8 | 7 |
|
9 | 8 | 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 |
13 | 29 | })
|
14 | 30 |
|
15 | 31 | afterAll(() => {
|
16 |
| - jest.restoreAllMocks() |
| 32 | + jest.clearAllMocks() |
| 33 | + jest.resetModules() |
17 | 34 | })
|
18 | 35 |
|
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 | + }) |
23 | 39 |
|
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) |
26 | 42 | })
|
27 | 43 |
|
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 | + }) |
36 | 63 | })
|
37 | 64 | })
|
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 |
45 | 77 | })
|
46 |
| - }) |
47 | 78 |
|
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'", () => { |
52 | 88 | expect(commit).toBe("development")
|
53 | 89 | })
|
54 | 90 | })
|
|
0 commit comments