Skip to content

Commit b65b703

Browse files
authored
Merge pull request #1221 from neet/prettify-test-assertions
chore: Use equality tester to simplify assertions
2 parents 129e8b0 + 0b6b46e commit b65b703

26 files changed

+80
-107
lines changed

test-utils/async-next-tick.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

test-utils/jest-extend-expect.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1-
/* eslint-disable @typescript-eslint/no-empty-object-type */
2-
/* eslint-disable @typescript-eslint/no-namespace */
3-
export interface CustomMatchers<R = unknown> {
4-
toContainId(id: string): R;
5-
}
1+
import { expect } from "@jest/globals";
2+
3+
// https://jestjs.io/docs/expect#expectaddequalitytesterstesters
4+
function idChecker(a: unknown, b: unknown) {
5+
const hasAId = typeof a === "object" && a !== null && "id" in a;
6+
const hasBId = typeof b === "object" && b !== null && "id" in b;
67

7-
declare global {
8-
namespace jest {
9-
interface Expect extends CustomMatchers {}
10-
interface Matchers<R> extends CustomMatchers<R> {}
11-
interface InverseAsymmetricMatchers extends CustomMatchers {}
8+
if (hasAId && hasBId) {
9+
return a.id === b.id;
10+
} else if (hasAId === hasBId) {
11+
return;
12+
} else {
13+
return false;
1214
}
1315
}
1416

15-
expect.extend({
16-
toContainId<T extends { id: string }>(received?: T, expected?: string) {
17-
if (!Array.isArray(received)) {
18-
return { pass: false, message: () => "Expected an array" };
19-
}
20-
21-
if (received.length === 0) {
22-
return {
23-
pass: false,
24-
message: () => "Expected an array with at least one element",
25-
};
26-
}
27-
28-
const pass = received.some((entity) => entity.id === expected);
29-
30-
return {
31-
pass,
32-
message: () => {
33-
return `List does not contain ${expected}`;
34-
},
35-
};
36-
},
37-
});
17+
expect.addEqualityTesters([idChecker]);

test-utils/jest-setup-after-env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import "./jest-extend-expect";
44
import { createRestAPIClient } from "../src";
55
import { SessionPoolImpl } from "./pools";
66

7-
jest.retryTimes(3);
7+
if (process.env.CI) {
8+
jest.retryTimes(3);
9+
}
10+
811
jest.setTimeout(1000 * 60);
912

1013
globalThis.admin = createRestAPIClient({

test-utils/pools/session-pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class SessionPoolImpl implements Pool<Session> {
5454
if (token == undefined) {
5555
// eslint-disable-next-line no-console
5656
console.warn(
57-
`Session ${session.id} (${session.acct}) is already released`,
57+
`Session ${session.id} (${session.account.acct}) is already released`,
5858
);
5959
return;
6060
}

test-utils/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66

77
export interface Session {
88
readonly id: string;
9-
readonly acct: string;
9+
readonly account: mastodon.v1.Account;
1010
readonly rest: mastodon.rest.Client;
1111
readonly ws: mastodon.streaming.Client;
1212
readonly [Symbol.asyncDispose]: () => Promise<void>;
@@ -32,7 +32,7 @@ export const createSession = async (
3232

3333
return Object.freeze({
3434
id: account.id,
35-
acct: account.acct,
35+
account,
3636
rest,
3737
ws,
3838
[Symbol.asyncDispose]: dispose,

tests/rest/v1/accounts.spec.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe("account", () => {
220220
.$select(bob.id)
221221
.followers.list();
222222

223-
expect(followers).toContainId(alice.id);
223+
expect(followers).toContainEqual(alice.account);
224224
await alice.rest.v1.accounts.$select(bob.id).unfollow();
225225
});
226226

@@ -233,7 +233,7 @@ describe("account", () => {
233233
.$select(alice.id)
234234
.following.list();
235235

236-
expect(accounts).toContainId(bob.id);
236+
expect(accounts).toContainEqual(bob.account);
237237
await alice.rest.v1.accounts.$select(bob.id).unfollow();
238238
});
239239

@@ -244,7 +244,7 @@ describe("account", () => {
244244
.$select(status.account.id)
245245
.statuses.list();
246246

247-
expect(statuses).toContainId(status.id);
247+
expect(statuses).toContainEqual(status);
248248
});
249249

250250
it("searches", async () => {
@@ -253,7 +253,7 @@ describe("account", () => {
253253
const accounts = await client.rest.v1.accounts.search.list({
254254
q: me.username,
255255
});
256-
expect(accounts).toContainId(me.id);
256+
expect(accounts).toContainEqual(me);
257257
});
258258

259259
it("lists lists", async () => {
@@ -266,10 +266,8 @@ describe("account", () => {
266266
await alice.rest.v1.lists.$select(list.id).accounts.create({
267267
accountIds: [bob.id],
268268
});
269-
const accounts = await alice.rest.v1.accounts
270-
.$select(bob.id)
271-
.lists.list();
272-
expect(accounts).toContainId(list.id);
269+
const lists = await alice.rest.v1.accounts.$select(bob.id).lists.list();
270+
expect(lists).toContainEqual(list);
273271
} finally {
274272
await alice.rest.v1.lists.$select(list.id).remove();
275273
}
@@ -284,7 +282,7 @@ describe("account", () => {
284282
const tags = await client.rest.v1.accounts
285283
.$select(client.id)
286284
.featuredTags.list();
287-
expect(tags).toContainId(featuredTag.id);
285+
expect(tags).toContainEqual(featuredTag);
288286

289287
await client.rest.v1.featuredTags.$select(featuredTag.id).remove();
290288
});
@@ -308,7 +306,7 @@ describe("account", () => {
308306
it("lookup", async () => {
309307
await using client = await sessions.acquire();
310308
const account = await client.rest.v1.accounts.lookup({
311-
acct: client.acct,
309+
acct: client.account.acct,
312310
});
313311
expect(account.id).toBe(client.id);
314312
});

tests/rest/v1/admin/canonical-email-blocks.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ describe("canonical-email-blocks", () => {
1414
const result = await admin.v1.admin.canonicalEmailBlocks.test({
1515
1616
});
17-
expect(result).toContainId(canonicalEmailBlock.id);
17+
expect(result).toContainEqual(canonicalEmailBlock);
1818

1919
const canonicalEmailBlocks =
2020
await admin.v1.admin.canonicalEmailBlocks.list();
21-
expect(canonicalEmailBlocks).toContainId(canonicalEmailBlock.id);
21+
expect(canonicalEmailBlocks).toContainEqual(canonicalEmailBlock);
2222
} finally {
2323
await admin.v1.admin.canonicalEmailBlocks
2424
.$select(canonicalEmailBlock.id)

tests/rest/v1/admin/domain-allows.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ it("handles domain allows", async () => {
1111
expect(domainAllow.domain).toMatch(/example.domain.to.allow.com/);
1212

1313
const list = await admin.v1.admin.domainAllows.list();
14-
expect(list).toContainId(domainAllow.id);
14+
expect(list).toContainEqual(domainAllow);
1515
} finally {
1616
await admin.v1.admin.domainAllows.$select(domainAllow.id).remove();
1717
}

tests/rest/v1/admin/domain-blocks.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ it("handles domain blocks", async () => {
1818
expect(domainBlock.rejectMedia).toBe(false);
1919

2020
const list = await admin.v1.admin.domainBlocks.list();
21-
expect(list).toContainId(domainBlock.id);
21+
expect(list).toContainEqual(domainBlock);
2222
} finally {
2323
await admin.v1.admin.domainBlocks.$select(domainBlock.id).remove();
2424
}

tests/rest/v1/admin/email-domain-blocks.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ it("handle email domain block", async () => {
1111
expect(emailDomainBlock.domain).toMatch(/example.domain.to.block.com/);
1212

1313
const list = await admin.v1.admin.emailDomainBlocks.list();
14-
expect(list).toContainId(emailDomainBlock.id);
14+
expect(list).toContainEqual(emailDomainBlock);
1515
} finally {
1616
await admin.v1.admin.emailDomainBlocks
1717
.$select(emailDomainBlock.id)

0 commit comments

Comments
 (0)