-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathloadEnvironment.test.ts
135 lines (108 loc) · 4.04 KB
/
loadEnvironment.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as fs from "fs"
import { loadEnvironment } from "../src/loadEnvironment"
const contentfulEnvironment = () => ({
getContentTypes: () => [],
getLocales: () => [],
})
const getContentfulEnvironmentFileFactory = jest.fn((_type: string) => contentfulEnvironment)
jest.mock(
require("path").resolve(process.cwd(), "./getContentfulEnvironment.js"),
() => getContentfulEnvironmentFileFactory("js"),
{ virtual: true },
)
jest.mock(
require("path").resolve(process.cwd(), "./getContentfulEnvironment.cjs"),
() => getContentfulEnvironmentFileFactory("cjs"),
{ virtual: true },
)
jest.mock(
require("path").resolve(process.cwd(), "./getContentfulEnvironment.ts"),
() => getContentfulEnvironmentFileFactory("ts"),
{ virtual: true },
)
const tsNodeRegistererEnabled = jest.fn()
const tsNodeRegister = jest.fn()
jest.mock("ts-node", () => ({ register: tsNodeRegister }))
describe("loadEnvironment", () => {
beforeEach(() => {
jest.resetAllMocks()
jest.restoreAllMocks()
jest.resetModules()
getContentfulEnvironmentFileFactory.mockReturnValue(contentfulEnvironment)
tsNodeRegister.mockReturnValue({ enabled: tsNodeRegistererEnabled })
})
describe("when getContentfulEnvironment.ts exists", () => {
beforeEach(() => {
jest.spyOn(fs, "existsSync").mockImplementation(path => {
return (path as string).endsWith(".ts")
})
})
describe("when ts-node is not found", () => {
beforeEach(() => {
// technically this is throwing after the `require` call,
// but it still tests the same code path so is fine
tsNodeRegister.mockImplementation(() => {
throw new (class extends Error {
public code: string
constructor(message?: string) {
super(message)
this.code = "MODULE_NOT_FOUND"
}
})()
})
})
it("throws a nice error", async () => {
await expect(loadEnvironment()).rejects.toThrow(
"'ts-node' is required for TypeScript configuration files",
)
})
})
describe("when there is another error", () => {
beforeEach(() => {
tsNodeRegister.mockImplementation(() => {
throw new Error("something else went wrong!")
})
})
it("re-throws", async () => {
await expect(loadEnvironment()).rejects.toThrow("something else went wrong!")
})
})
describe("when called multiple times", () => {
it("re-uses the registerer", async () => {
await loadEnvironment()
await loadEnvironment()
expect(tsNodeRegister).toHaveBeenCalledTimes(1)
})
})
it("requires the typescript config", async () => {
await loadEnvironment()
expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("ts")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("js")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("cjs")
})
it("disables the registerer afterwards", async () => {
await loadEnvironment()
expect(tsNodeRegistererEnabled).toHaveBeenCalledWith(false)
})
})
describe("when getContentfulEnvironment.cjs exists", () => {
beforeEach(() => {
jest.spyOn(fs, "existsSync").mockImplementation(path => {
return (path as string).endsWith(".cjs")
})
})
it("requires the javascript config on ESM environments", async () => {
await loadEnvironment()
expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("cjs")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("js")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts")
})
})
it("requires the javascript config", async () => {
jest.spyOn(fs, "existsSync").mockReturnValue(false)
await loadEnvironment()
expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("js")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("cjs")
})
})