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
136
137
138
139
140
141
142
143
144
145
146
147
148
|
import { configOrError, handleShutdown } from "./config";
import { MongoClient } from "mongodb";
import { Server } from "http";
const exit = jest.spyOn(process, "exit").mockImplementation(() => {
throw Error("");
});
const CommonMocks = {
connect: jest.fn(),
isConnected: jest.fn(),
logout: jest.fn(),
watch: jest.fn(),
startSession: jest.fn(),
withSession: jest.fn(),
addListener: jest.fn(),
on: jest.fn(),
once: jest.fn(),
prependListener: jest.fn(),
prependOnceListener: jest.fn(),
removeAllListeners: jest.fn(),
removeListener: jest.fn(),
off: jest.fn(),
setMaxListeners: jest.fn(),
getMaxListeners: jest.fn(),
listeners: jest.fn(),
listenerCount: jest.fn(),
rawListeners: jest.fn(),
emit: jest.fn(),
eventNames: jest.fn()
};
const MongoMock = (p: Promise<void>): jest.Mock<MongoClient, void[]> =>
jest.fn<MongoClient, void[]>(() => ({
...CommonMocks,
close: jest.fn(() => p),
db: jest.fn()
}));
const ServerMock = (mockErr: Error | undefined): jest.Mock<Server, void[]> =>
jest.fn<Server, void[]>(() => ({
...CommonMocks,
connections: 0,
setTimeout: jest.fn(),
timeout: 0,
headersTimeout: 0,
keepAliveTimeout: 0,
close: function(c: (err?: Error | undefined) => void): Server {
c(mockErr);
return this;
},
maxHeadersCount: 0,
maxConnections: 0,
listen: jest.fn(),
listening: true,
address: jest.fn(),
getConnections: jest.fn(),
ref: jest.fn(),
unref: jest.fn()
}));
describe("configOrError", () => {
beforeEach(() => {
exit.mockClear();
});
it("should exit when a env var does not exist", () => {
// Arrange
// Act
let result;
try {
result = configOrError("APPLESAUCE");
} catch (err) {
//
}
// Assert
expect(result).toBeUndefined();
expect(exit).toHaveBeenCalledWith(1);
});
it("should return the expected env var", () => {
// Arrange
process.env.CHRYSANTHEMUM = "hello";
// Act
const result = configOrError("CHRYSANTHEMUM");
// Assert
expect(result).toEqual(process.env.CHRYSANTHEMUM);
expect(exit).toHaveBeenCalledTimes(0);
});
});
describe("handleShutdown", () => {
beforeEach(() => {
exit.mockClear();
// we don't use the MongoMock or ServerMock to directly test, so no mockClear needed
});
it("should exit gracefully without error", async () => {
// Arrange
const mongo = MongoMock(new Promise(r => r()))();
const server = ServerMock(undefined)();
// Act
try {
await handleShutdown(mongo, server)("SIGINT");
} catch (err) {
//
}
// Assert
expect(exit).toHaveBeenCalledWith(1);
});
it("should exit with error with Mongo error", async () => {
// Arrange
const mongo = MongoMock(new Promise((_, r) => r()))();
const server = ServerMock(undefined)();
// Act
try {
await handleShutdown(mongo, server)("SIGINT");
} catch (err) {
//
}
// Assert
expect(exit).toHaveBeenCalledWith(1);
});
it("should exit with error with Server error", async () => {
// Arrange
const mongo = MongoMock(new Promise(r => r()))();
const server = ServerMock(Error("oh noooo"))();
// Act
try {
await handleShutdown(mongo, server)("SIGINT");
} catch (err) {
//
}
// Assert
expect(exit).toHaveBeenCalledWith(1);
});
});
|