Skip to content

Commit 41b0215

Browse files
authored
test: fix tests not awaiting expect with resolves or rejects (@fehmer) (monkeytypegame#6308)
1 parent 41ee26a commit 41b0215

File tree

5 files changed

+93
-77
lines changed

5 files changed

+93
-77
lines changed

backend/__tests__/dal/blocklist.spec.ts

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("BlocklistDal", () => {
2121
await BlacklistDal.add({ name, email });
2222

2323
//THEN
24-
expect(
24+
await expect(
2525
BlacklistDal.getCollection().findOne({
2626
emailHash: BlacklistDal.hash(email),
2727
})
@@ -30,7 +30,7 @@ describe("BlocklistDal", () => {
3030
timestamp: now,
3131
});
3232

33-
expect(
33+
await expect(
3434
BlacklistDal.getCollection().findOne({
3535
usernameHash: BlacklistDal.hash(name),
3636
})
@@ -52,7 +52,7 @@ describe("BlocklistDal", () => {
5252
await BlacklistDal.add({ name, email, discordId });
5353

5454
//THEN
55-
expect(
55+
await expect(
5656
BlacklistDal.getCollection().findOne({
5757
discordIdHash: BlacklistDal.hash(discordId),
5858
})
@@ -75,21 +75,21 @@ describe("BlocklistDal", () => {
7575
await BlacklistDal.add({ name, email: email2 });
7676

7777
//THEN
78-
expect(
78+
await expect(
7979
BlacklistDal.getCollection()
8080
.find({
8181
usernameHash: BlacklistDal.hash(name),
8282
})
8383
.toArray()
8484
).resolves.toHaveLength(1);
85-
expect(
85+
await expect(
8686
BlacklistDal.getCollection()
8787
.find({
8888
emailHash: BlacklistDal.hash(email),
8989
})
9090
.toArray()
9191
).resolves.toHaveLength(1);
92-
expect(
92+
await expect(
9393
BlacklistDal.getCollection()
9494
.find({
9595
emailHash: BlacklistDal.hash(email2),
@@ -111,7 +111,7 @@ describe("BlocklistDal", () => {
111111
await BlacklistDal.add({ name: name2, email });
112112

113113
//THEN
114-
expect(
114+
await expect(
115115
BlacklistDal.getCollection()
116116
.find({
117117
emailHash: BlacklistDal.hash(email),
@@ -136,7 +136,7 @@ describe("BlocklistDal", () => {
136136

137137
//THEN
138138

139-
expect(
139+
await expect(
140140
BlacklistDal.getCollection()
141141
.find({
142142
discordIdHash: BlacklistDal.hash(discordId),
@@ -156,34 +156,34 @@ describe("BlocklistDal", () => {
156156

157157
//WHEN / THEN
158158
//by name
159-
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
160-
expect(
159+
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
160+
await expect(
161161
BlacklistDal.contains({ name: name.toUpperCase() })
162162
).resolves.toBeTruthy();
163-
expect(
163+
await expect(
164164
BlacklistDal.contains({ name, email: "unknown", discordId: "unknown" })
165165
).resolves.toBeTruthy();
166166

167167
//by email
168-
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
169-
expect(
168+
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
169+
await expect(
170170
BlacklistDal.contains({ email: email.toUpperCase() })
171171
).resolves.toBeTruthy();
172-
expect(
172+
await expect(
173173
BlacklistDal.contains({ name: "unknown", email, discordId: "unknown" })
174174
).resolves.toBeTruthy();
175175

176176
//by discordId
177-
expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
178-
expect(
177+
await expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
178+
await expect(
179179
BlacklistDal.contains({ discordId: discordId.toUpperCase() })
180180
).resolves.toBeTruthy();
181-
expect(
181+
await expect(
182182
BlacklistDal.contains({ name: "unknown", email: "unknown", discordId })
183183
).resolves.toBeTruthy();
184184

185185
//by name and email and discordId
186-
expect(
186+
await expect(
187187
BlacklistDal.contains({ name, email, discordId })
188188
).resolves.toBeTruthy();
189189
});
@@ -193,20 +193,24 @@ describe("BlocklistDal", () => {
193193
await BlacklistDal.add({ name: "test2", email: "[email protected]" });
194194

195195
//WHEN / THEN
196-
expect(BlacklistDal.contains({ name: "unknown" })).resolves.toBeFalsy();
197-
expect(BlacklistDal.contains({ email: "unknown" })).resolves.toBeFalsy();
198-
expect(
196+
await expect(
197+
BlacklistDal.contains({ name: "unknown" })
198+
).resolves.toBeFalsy();
199+
await expect(
200+
BlacklistDal.contains({ email: "unknown" })
201+
).resolves.toBeFalsy();
202+
await expect(
199203
BlacklistDal.contains({ discordId: "unknown" })
200204
).resolves.toBeFalsy();
201-
expect(
205+
await expect(
202206
BlacklistDal.contains({
203207
name: "unknown",
204208
email: "unknown",
205209
discordId: "unknown",
206210
})
207211
).resolves.toBeFalsy();
208212

209-
expect(BlacklistDal.contains({})).resolves.toBeFalsy();
213+
await expect(BlacklistDal.contains({})).resolves.toBeFalsy();
210214
});
211215
});
212216

@@ -222,12 +226,14 @@ describe("BlocklistDal", () => {
222226
await BlacklistDal.remove({ name });
223227

224228
//THEN
225-
expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
226-
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
229+
await expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
230+
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
227231

228232
//decoy still exists
229-
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
230-
expect(
233+
await expect(
234+
BlacklistDal.contains({ name: "test" })
235+
).resolves.toBeTruthy();
236+
await expect(
231237
BlacklistDal.contains({ email: "[email protected]" })
232238
).resolves.toBeTruthy();
233239
});
@@ -242,12 +248,14 @@ describe("BlocklistDal", () => {
242248
await BlacklistDal.remove({ email });
243249

244250
//THEN
245-
expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
246-
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
251+
await expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
252+
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
247253

248254
//decoy still exists
249-
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
250-
expect(
255+
await expect(
256+
BlacklistDal.contains({ name: "test" })
257+
).resolves.toBeTruthy();
258+
await expect(
251259
BlacklistDal.contains({ email: "[email protected]" })
252260
).resolves.toBeTruthy();
253261
});
@@ -267,16 +275,18 @@ describe("BlocklistDal", () => {
267275
await BlacklistDal.remove({ discordId });
268276

269277
//THEN
270-
expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
271-
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
272-
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
278+
await expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
279+
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
280+
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
273281

274282
//decoy still exists
275-
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
276-
expect(
283+
await expect(
284+
BlacklistDal.contains({ name: "test" })
285+
).resolves.toBeTruthy();
286+
await expect(
277287
BlacklistDal.contains({ email: "[email protected]" })
278288
).resolves.toBeTruthy();
279-
expect(
289+
await expect(
280290
BlacklistDal.contains({ discordId: "testDiscordId" })
281291
).resolves.toBeTruthy();
282292
});
@@ -296,16 +306,18 @@ describe("BlocklistDal", () => {
296306
await BlacklistDal.remove({ name, email, discordId });
297307

298308
//THEN
299-
expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
300-
expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
301-
expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
309+
await expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
310+
await expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
311+
await expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
302312

303313
//decoy still exists
304-
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
305-
expect(
314+
await expect(
315+
BlacklistDal.contains({ name: "test" })
316+
).resolves.toBeTruthy();
317+
await expect(
306318
BlacklistDal.contains({ email: "[email protected]" })
307319
).resolves.toBeTruthy();
308-
expect(
320+
await expect(
309321
BlacklistDal.contains({ discordId: "testDiscordId" })
310322
).resolves.toBeTruthy();
311323
});
@@ -322,9 +334,9 @@ describe("BlocklistDal", () => {
322334
await BlacklistDal.remove({});
323335

324336
//THEN
325-
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
326-
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
327-
expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
337+
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
338+
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
339+
await expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
328340
});
329341
});
330342
describe("hash", () => {

backend/__tests__/dal/preset.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe("PresetDal", () => {
6262
}
6363

6464
//WHEN / THEN
65-
expect(() =>
65+
await expect(() =>
6666
PresetDal.addPreset(uid, { name: "max", config: {} })
6767
).rejects.toThrowError("Too many presets");
6868
});
@@ -349,7 +349,7 @@ describe("PresetDal", () => {
349349

350350
describe("removePreset", () => {
351351
it("should fail if preset is unknown", async () => {
352-
expect(() =>
352+
await expect(() =>
353353
PresetDal.removePreset("uid", new ObjectId().toHexString())
354354
).rejects.toThrowError("Preset not found");
355355
});
@@ -412,7 +412,7 @@ describe("PresetDal", () => {
412412
).presetId;
413413

414414
//WHEN
415-
expect(() =>
415+
await expect(() =>
416416
PresetDal.removePreset(decoyUid, first)
417417
).rejects.toThrowError("Preset not found");
418418

backend/__tests__/dal/user.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ describe("UserDal", () => {
11291129
});
11301130
describe("getPartialUser", () => {
11311131
it("should throw for unknown user", async () => {
1132-
expect(async () =>
1132+
await expect(async () =>
11331133
UserDAL.getPartialUser("1234", "stack", [])
11341134
).rejects.toThrowError("User not found\nStack: stack");
11351135
});
@@ -1164,7 +1164,7 @@ describe("UserDal", () => {
11641164
});
11651165
describe("updateEmail", () => {
11661166
it("throws for nonexisting user", async () => {
1167-
expect(async () =>
1167+
await expect(async () =>
11681168
UserDAL.updateEmail("unknown", "[email protected]")
11691169
).rejects.toThrowError("User not found\nStack: update email");
11701170
});
@@ -1182,7 +1182,7 @@ describe("UserDal", () => {
11821182
});
11831183
describe("resetPb", () => {
11841184
it("throws for nonexisting user", async () => {
1185-
expect(async () => UserDAL.resetPb("unknown")).rejects.toThrowError(
1185+
await expect(async () => UserDAL.resetPb("unknown")).rejects.toThrowError(
11861186
"User not found\nStack: reset pb"
11871187
);
11881188
});
@@ -1208,7 +1208,7 @@ describe("UserDal", () => {
12081208
});
12091209
describe("linkDiscord", () => {
12101210
it("throws for nonexisting user", async () => {
1211-
expect(async () =>
1211+
await expect(async () =>
12121212
UserDAL.linkDiscord("unknown", "", "")
12131213
).rejects.toThrowError("User not found\nStack: link discord");
12141214
});
@@ -1230,9 +1230,9 @@ describe("UserDal", () => {
12301230
});
12311231
describe("unlinkDiscord", () => {
12321232
it("throws for nonexisting user", async () => {
1233-
expect(async () => UserDAL.unlinkDiscord("unknown")).rejects.toThrowError(
1234-
"User not found\nStack: unlink discord"
1235-
);
1233+
await expect(async () =>
1234+
UserDAL.unlinkDiscord("unknown")
1235+
).rejects.toThrowError("User not found\nStack: unlink discord");
12361236
});
12371237
it("should update", async () => {
12381238
//given

backend/__tests__/middlewares/auth.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@monkeytype/contracts/schemas/api";
1616
import * as Prometheus from "../../src/utils/prometheus";
1717
import { TsRestRequestWithContext } from "../../src/api/types";
18+
import { error } from "console";
1819

1920
const mockDecodedToken: DecodedIdToken = {
2021
uid: "123456789",
@@ -95,19 +96,20 @@ describe("middlewares/auth", () => {
9596
it("should fail if token is not fresh", async () => {
9697
//GIVEN
9798
Date.now = vi.fn(() => 60001);
99+
const expectedError = new Error(
100+
"Unauthorized\nStack: This endpoint requires a fresh token"
101+
);
98102

99103
//WHEN
100-
expect(() =>
104+
await expect(() =>
101105
authenticate({}, { requireFreshToken: true })
102-
).rejects.toThrowError(
103-
"Unauthorized\nStack: This endpoint requires a fresh token"
104-
);
106+
).rejects.toThrowError(expectedError);
105107

106108
//THEN
107109

108-
expect(nextFunction).not.toHaveBeenCalled();
110+
expect(nextFunction).toHaveBeenLastCalledWith(expectedError);
109111
expect(prometheusIncrementAuthMock).not.toHaveBeenCalled();
110-
expect(prometheusRecordAuthTimeMock).not.toHaveBeenCalled();
112+
expect(prometheusRecordAuthTimeMock).toHaveBeenCalledOnce();
111113
});
112114
it("should allow the request if token is fresh", async () => {
113115
//GIVEN

0 commit comments

Comments
 (0)