mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-06 05:26:54 +08:00
test: fix tests not awaiting expect with resolves or rejects (@fehmer) (#6308)
This commit is contained in:
parent
41ee26a725
commit
41b02155f2
5 changed files with 94 additions and 78 deletions
|
@ -21,7 +21,7 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.add({ name, email });
|
||||
|
||||
//THEN
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection().findOne({
|
||||
emailHash: BlacklistDal.hash(email),
|
||||
})
|
||||
|
@ -30,7 +30,7 @@ describe("BlocklistDal", () => {
|
|||
timestamp: now,
|
||||
});
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection().findOne({
|
||||
usernameHash: BlacklistDal.hash(name),
|
||||
})
|
||||
|
@ -52,7 +52,7 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.add({ name, email, discordId });
|
||||
|
||||
//THEN
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection().findOne({
|
||||
discordIdHash: BlacklistDal.hash(discordId),
|
||||
})
|
||||
|
@ -75,21 +75,21 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.add({ name, email: email2 });
|
||||
|
||||
//THEN
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection()
|
||||
.find({
|
||||
usernameHash: BlacklistDal.hash(name),
|
||||
})
|
||||
.toArray()
|
||||
).resolves.toHaveLength(1);
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection()
|
||||
.find({
|
||||
emailHash: BlacklistDal.hash(email),
|
||||
})
|
||||
.toArray()
|
||||
).resolves.toHaveLength(1);
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection()
|
||||
.find({
|
||||
emailHash: BlacklistDal.hash(email2),
|
||||
|
@ -111,7 +111,7 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.add({ name: name2, email });
|
||||
|
||||
//THEN
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection()
|
||||
.find({
|
||||
emailHash: BlacklistDal.hash(email),
|
||||
|
@ -136,7 +136,7 @@ describe("BlocklistDal", () => {
|
|||
|
||||
//THEN
|
||||
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.getCollection()
|
||||
.find({
|
||||
discordIdHash: BlacklistDal.hash(discordId),
|
||||
|
@ -156,34 +156,34 @@ describe("BlocklistDal", () => {
|
|||
|
||||
//WHEN / THEN
|
||||
//by name
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: name.toUpperCase() })
|
||||
).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name, email: "unknown", discordId: "unknown" })
|
||||
).resolves.toBeTruthy();
|
||||
|
||||
//by email
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: email.toUpperCase() })
|
||||
).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "unknown", email, discordId: "unknown" })
|
||||
).resolves.toBeTruthy();
|
||||
|
||||
//by discordId
|
||||
expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ discordId: discordId.toUpperCase() })
|
||||
).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "unknown", email: "unknown", discordId })
|
||||
).resolves.toBeTruthy();
|
||||
|
||||
//by name and email and discordId
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name, email, discordId })
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -193,12 +193,16 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.add({ name: "test2", email: "test2@example.com" });
|
||||
|
||||
//WHEN / THEN
|
||||
expect(BlacklistDal.contains({ name: "unknown" })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ email: "unknown" })).resolves.toBeFalsy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "unknown" })
|
||||
).resolves.toBeFalsy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: "unknown" })
|
||||
).resolves.toBeFalsy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ discordId: "unknown" })
|
||||
).resolves.toBeFalsy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({
|
||||
name: "unknown",
|
||||
email: "unknown",
|
||||
|
@ -206,7 +210,7 @@ describe("BlocklistDal", () => {
|
|||
})
|
||||
).resolves.toBeFalsy();
|
||||
|
||||
expect(BlacklistDal.contains({})).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({})).resolves.toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -222,12 +226,14 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.remove({ name });
|
||||
|
||||
//THEN
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
|
||||
//decoy still exists
|
||||
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "test" })
|
||||
).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: "test@example.com" })
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -242,12 +248,14 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.remove({ email });
|
||||
|
||||
//THEN
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
|
||||
//decoy still exists
|
||||
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "test" })
|
||||
).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: "test@example.com" })
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -267,16 +275,18 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.remove({ discordId });
|
||||
|
||||
//THEN
|
||||
expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
|
||||
//decoy still exists
|
||||
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "test" })
|
||||
).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: "test@example.com" })
|
||||
).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ discordId: "testDiscordId" })
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -296,16 +306,18 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.remove({ name, email, discordId });
|
||||
|
||||
//THEN
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
|
||||
expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeFalsy();
|
||||
await expect(BlacklistDal.contains({ discordId })).resolves.toBeFalsy();
|
||||
|
||||
//decoy still exists
|
||||
expect(BlacklistDal.contains({ name: "test" })).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ name: "test" })
|
||||
).resolves.toBeTruthy();
|
||||
await expect(
|
||||
BlacklistDal.contains({ email: "test@example.com" })
|
||||
).resolves.toBeTruthy();
|
||||
expect(
|
||||
await expect(
|
||||
BlacklistDal.contains({ discordId: "testDiscordId" })
|
||||
).resolves.toBeTruthy();
|
||||
});
|
||||
|
@ -322,9 +334,9 @@ describe("BlocklistDal", () => {
|
|||
await BlacklistDal.remove({});
|
||||
|
||||
//THEN
|
||||
expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ email })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ name })).resolves.toBeTruthy();
|
||||
await expect(BlacklistDal.contains({ discordId })).resolves.toBeTruthy();
|
||||
});
|
||||
});
|
||||
describe("hash", () => {
|
||||
|
|
|
@ -62,7 +62,7 @@ describe("PresetDal", () => {
|
|||
}
|
||||
|
||||
//WHEN / THEN
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
PresetDal.addPreset(uid, { name: "max", config: {} })
|
||||
).rejects.toThrowError("Too many presets");
|
||||
});
|
||||
|
@ -349,7 +349,7 @@ describe("PresetDal", () => {
|
|||
|
||||
describe("removePreset", () => {
|
||||
it("should fail if preset is unknown", async () => {
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
PresetDal.removePreset("uid", new ObjectId().toHexString())
|
||||
).rejects.toThrowError("Preset not found");
|
||||
});
|
||||
|
@ -412,7 +412,7 @@ describe("PresetDal", () => {
|
|||
).presetId;
|
||||
|
||||
//WHEN
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
PresetDal.removePreset(decoyUid, first)
|
||||
).rejects.toThrowError("Preset not found");
|
||||
|
||||
|
|
|
@ -1129,7 +1129,7 @@ describe("UserDal", () => {
|
|||
});
|
||||
describe("getPartialUser", () => {
|
||||
it("should throw for unknown user", async () => {
|
||||
expect(async () =>
|
||||
await expect(async () =>
|
||||
UserDAL.getPartialUser("1234", "stack", [])
|
||||
).rejects.toThrowError("User not found\nStack: stack");
|
||||
});
|
||||
|
@ -1164,7 +1164,7 @@ describe("UserDal", () => {
|
|||
});
|
||||
describe("updateEmail", () => {
|
||||
it("throws for nonexisting user", async () => {
|
||||
expect(async () =>
|
||||
await expect(async () =>
|
||||
UserDAL.updateEmail("unknown", "test@example.com")
|
||||
).rejects.toThrowError("User not found\nStack: update email");
|
||||
});
|
||||
|
@ -1182,7 +1182,7 @@ describe("UserDal", () => {
|
|||
});
|
||||
describe("resetPb", () => {
|
||||
it("throws for nonexisting user", async () => {
|
||||
expect(async () => UserDAL.resetPb("unknown")).rejects.toThrowError(
|
||||
await expect(async () => UserDAL.resetPb("unknown")).rejects.toThrowError(
|
||||
"User not found\nStack: reset pb"
|
||||
);
|
||||
});
|
||||
|
@ -1208,7 +1208,7 @@ describe("UserDal", () => {
|
|||
});
|
||||
describe("linkDiscord", () => {
|
||||
it("throws for nonexisting user", async () => {
|
||||
expect(async () =>
|
||||
await expect(async () =>
|
||||
UserDAL.linkDiscord("unknown", "", "")
|
||||
).rejects.toThrowError("User not found\nStack: link discord");
|
||||
});
|
||||
|
@ -1230,9 +1230,9 @@ describe("UserDal", () => {
|
|||
});
|
||||
describe("unlinkDiscord", () => {
|
||||
it("throws for nonexisting user", async () => {
|
||||
expect(async () => UserDAL.unlinkDiscord("unknown")).rejects.toThrowError(
|
||||
"User not found\nStack: unlink discord"
|
||||
);
|
||||
await expect(async () =>
|
||||
UserDAL.unlinkDiscord("unknown")
|
||||
).rejects.toThrowError("User not found\nStack: unlink discord");
|
||||
});
|
||||
it("should update", async () => {
|
||||
//given
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
} from "@monkeytype/contracts/schemas/api";
|
||||
import * as Prometheus from "../../src/utils/prometheus";
|
||||
import { TsRestRequestWithContext } from "../../src/api/types";
|
||||
import { error } from "console";
|
||||
|
||||
const mockDecodedToken: DecodedIdToken = {
|
||||
uid: "123456789",
|
||||
|
@ -95,19 +96,20 @@ describe("middlewares/auth", () => {
|
|||
it("should fail if token is not fresh", async () => {
|
||||
//GIVEN
|
||||
Date.now = vi.fn(() => 60001);
|
||||
|
||||
//WHEN
|
||||
expect(() =>
|
||||
authenticate({}, { requireFreshToken: true })
|
||||
).rejects.toThrowError(
|
||||
const expectedError = new Error(
|
||||
"Unauthorized\nStack: This endpoint requires a fresh token"
|
||||
);
|
||||
|
||||
//WHEN
|
||||
await expect(() =>
|
||||
authenticate({}, { requireFreshToken: true })
|
||||
).rejects.toThrowError(expectedError);
|
||||
|
||||
//THEN
|
||||
|
||||
expect(nextFunction).not.toHaveBeenCalled();
|
||||
expect(nextFunction).toHaveBeenLastCalledWith(expectedError);
|
||||
expect(prometheusIncrementAuthMock).not.toHaveBeenCalled();
|
||||
expect(prometheusRecordAuthTimeMock).not.toHaveBeenCalled();
|
||||
expect(prometheusRecordAuthTimeMock).toHaveBeenCalledOnce();
|
||||
});
|
||||
it("should allow the request if token is fresh", async () => {
|
||||
//GIVEN
|
||||
|
|
|
@ -5,33 +5,35 @@ describe("british-english", () => {
|
|||
describe("replace", () => {
|
||||
beforeEach(() => (Config.mode = "time"));
|
||||
|
||||
it("should not replace words with no rule", () => {
|
||||
expect(replace("test", "")).resolves.toEqual("test");
|
||||
expect(replace("Test", "")).resolves.toEqual("Test");
|
||||
it("should not replace words with no rule", async () => {
|
||||
await expect(replace("test", "")).resolves.toEqual("test");
|
||||
await expect(replace("Test", "")).resolves.toEqual("Test");
|
||||
});
|
||||
|
||||
it("should replace words", () => {
|
||||
expect(replace("math", "")).resolves.toEqual("maths");
|
||||
expect(replace("Math", "")).resolves.toEqual("Maths");
|
||||
it("should replace words", async () => {
|
||||
await expect(replace("math", "")).resolves.toEqual("maths");
|
||||
await expect(replace("Math", "")).resolves.toEqual("Maths");
|
||||
});
|
||||
|
||||
it("should replace words with non-word characters around", () => {
|
||||
expect(replace(" :math-. ", "")).resolves.toEqual(" :maths-. ");
|
||||
expect(replace(" :Math-. ", "")).resolves.toEqual(" :Maths-. ");
|
||||
it("should replace words with non-word characters around", async () => {
|
||||
await expect(replace(" :math-. ", "")).resolves.toEqual(" :maths-. ");
|
||||
await expect(replace(" :Math-. ", "")).resolves.toEqual(" :Maths-. ");
|
||||
});
|
||||
|
||||
it("should not replace in quote mode if previousWord matches excepted words", () => {
|
||||
it("should not replace in quote mode if previousWord matches excepted words", async () => {
|
||||
//GIVEN
|
||||
Config.mode = "quote";
|
||||
|
||||
//WHEN/THEN
|
||||
expect(replace("tire", "will")).resolves.toEqual("tire");
|
||||
expect(replace("tire", "")).resolves.toEqual("tyre");
|
||||
await expect(replace("tire", "will")).resolves.toEqual("tire");
|
||||
await expect(replace("tire", "")).resolves.toEqual("tyre");
|
||||
});
|
||||
|
||||
it("should replace hyphenated words", () => {
|
||||
expect(replace("cream-colored", "")).resolves.toEqual("cream-coloured");
|
||||
expect(replace("armor-flavoring", "")).resolves.toEqual(
|
||||
it("should replace hyphenated words", async () => {
|
||||
await expect(replace("cream-colored", "")).resolves.toEqual(
|
||||
"cream-coloured"
|
||||
);
|
||||
await expect(replace("armor-flavoring", "")).resolves.toEqual(
|
||||
"armour-flavouring"
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue