@dreamboard-games/testing 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +89 -0
- package/NOTICE +1 -0
- package/README.md +176 -0
- package/dist/index.d.ts +121 -0
- package/dist/index.js +16728 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
- package/src/create-expect-api.test.ts +259 -0
- package/src/create-expect-api.ts +338 -0
- package/src/create-test-runtime.ts +516 -0
- package/src/definitions.ts +114 -0
- package/src/index.ts +3 -0
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dreamboard-games/testing",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dreamboard authored testing helpers and reducer-native UI test runtime",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"bun": "./src/index.ts",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"lint": "eslint 'src/**/*.ts'",
|
|
18
|
+
"test": "bun test"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"NOTICE"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@dreamboard/api-client": "npm:@dreamboard-games/api-client@0.1.0",
|
|
29
|
+
"@dreamboard/app-sdk": "npm:@dreamboard-games/app-sdk@0.0.41",
|
|
30
|
+
"@dreamboard/reducer-contract": "npm:@dreamboard-games/reducer-contract@7.0.0",
|
|
31
|
+
"@dreamboard/ui-sdk": "npm:@dreamboard-games/ui-sdk@0.0.41",
|
|
32
|
+
"zustand": "^5.0.4"
|
|
33
|
+
},
|
|
34
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/dreamboard-games/dreamboard.git"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "http://dreamboard.games/",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/dreamboard-games/dreamboard/issues"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { createExpectApi } from "./create-expect-api.ts";
|
|
3
|
+
import type { InteractionDescriptorLike } from "./definitions.ts";
|
|
4
|
+
|
|
5
|
+
function makeDescriptor(
|
|
6
|
+
descriptor: Partial<InteractionDescriptorLike>,
|
|
7
|
+
): InteractionDescriptorLike {
|
|
8
|
+
return {
|
|
9
|
+
interactionId: "placeThingCard",
|
|
10
|
+
available: true,
|
|
11
|
+
kind: "choose-zone",
|
|
12
|
+
surface: "board",
|
|
13
|
+
context: { to: "player-1" },
|
|
14
|
+
...descriptor,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("createExpectApi — value matchers", () => {
|
|
19
|
+
const expectFn = createExpectApi();
|
|
20
|
+
|
|
21
|
+
test("toBe passes on strict equality and throws otherwise", () => {
|
|
22
|
+
expectFn(1).toBe(1);
|
|
23
|
+
expect(() => expectFn(1).toBe(2)).toThrow();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("toEqual performs deep equality", () => {
|
|
27
|
+
expectFn({ a: 1, b: [2, 3] }).toEqual({ a: 1, b: [2, 3] });
|
|
28
|
+
expect(() => expectFn({ a: 1 }).toEqual({ a: 2 })).toThrow();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("toMatchObject allows partial matches", () => {
|
|
32
|
+
expectFn({ a: 1, b: 2, nested: { c: 3 } }).toMatchObject({
|
|
33
|
+
a: 1,
|
|
34
|
+
nested: { c: 3 },
|
|
35
|
+
});
|
|
36
|
+
expect(() => expectFn({ a: 1 }).toMatchObject({ a: 2 })).toThrow();
|
|
37
|
+
expect(() => expectFn({ a: 1 }).toMatchObject({ b: 1 })).toThrow();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("toBeDefined / toBeUndefined / toBeNull", () => {
|
|
41
|
+
expectFn(1).toBeDefined();
|
|
42
|
+
expectFn(undefined).toBeUndefined();
|
|
43
|
+
expectFn(null).toBeNull();
|
|
44
|
+
expect(() => expectFn(undefined).toBeDefined()).toThrow();
|
|
45
|
+
expect(() => expectFn(1).toBeUndefined()).toThrow();
|
|
46
|
+
expect(() => expectFn(1).toBeNull()).toThrow();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("toContain works for arrays and strings", () => {
|
|
50
|
+
expectFn([1, 2, 3]).toContain(2);
|
|
51
|
+
expectFn("hello world").toContain("world");
|
|
52
|
+
expect(() => expectFn([1, 2, 3]).toContain(4)).toThrow();
|
|
53
|
+
expect(() => expectFn("hello").toContain("world")).toThrow();
|
|
54
|
+
expect(() => expectFn(42 as unknown).toContain(1)).toThrow();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("toContainEqual checks deep equality in arrays", () => {
|
|
58
|
+
expectFn([{ id: "a" }, { id: "b" }]).toContainEqual({ id: "a" });
|
|
59
|
+
expect(() => expectFn([{ id: "a" }]).toContainEqual({ id: "b" })).toThrow();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("toHaveLength checks numeric length", () => {
|
|
63
|
+
expectFn([1, 2, 3]).toHaveLength(3);
|
|
64
|
+
expectFn("abcd").toHaveLength(4);
|
|
65
|
+
expect(() => expectFn([1]).toHaveLength(2)).toThrow();
|
|
66
|
+
expect(() => expectFn(42 as unknown).toHaveLength(0)).toThrow();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("toBeGreaterThanOrEqual checks numeric ordering", () => {
|
|
70
|
+
expectFn(5).toBeGreaterThanOrEqual(5);
|
|
71
|
+
expectFn(6).toBeGreaterThanOrEqual(5);
|
|
72
|
+
expect(() => expectFn(4).toBeGreaterThanOrEqual(5)).toThrow();
|
|
73
|
+
expect(() =>
|
|
74
|
+
expectFn("five" as unknown).toBeGreaterThanOrEqual(1),
|
|
75
|
+
).toThrow();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("toThrow with predicate variants", () => {
|
|
79
|
+
expectFn(() => {
|
|
80
|
+
throw new Error("boom");
|
|
81
|
+
}).toThrow();
|
|
82
|
+
expectFn(() => {
|
|
83
|
+
throw new Error("boom");
|
|
84
|
+
}).toThrow("boom");
|
|
85
|
+
expectFn(() => {
|
|
86
|
+
throw new Error("boom");
|
|
87
|
+
}).toThrow(/bo/);
|
|
88
|
+
expectFn(() => {
|
|
89
|
+
throw new Error("boom");
|
|
90
|
+
}).toThrow((error) => error.message.startsWith("boo"));
|
|
91
|
+
expect(() => expectFn(() => undefined).toThrow()).toThrow();
|
|
92
|
+
expect(() =>
|
|
93
|
+
expectFn(() => {
|
|
94
|
+
throw new Error("boom");
|
|
95
|
+
}).toThrow("baz"),
|
|
96
|
+
).toThrow();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("createExpectApi — rejection matcher", () => {
|
|
101
|
+
const expectFn = createExpectApi();
|
|
102
|
+
|
|
103
|
+
function makeError(errorCode: string, message = "rejected"): Error {
|
|
104
|
+
const err = new Error(message);
|
|
105
|
+
(err as Error & { errorCode?: string }).errorCode = errorCode;
|
|
106
|
+
return err;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
test("toRejectWith passes when errorCode matches", async () => {
|
|
110
|
+
await expectFn(async () => {
|
|
111
|
+
throw makeError("CARD_NOT_IN_HAND");
|
|
112
|
+
}).toRejectWith({ errorCode: "CARD_NOT_IN_HAND" });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("toRejectWith fails when the function resolves", async () => {
|
|
116
|
+
let threw = false;
|
|
117
|
+
try {
|
|
118
|
+
await expectFn(async () => undefined).toRejectWith({
|
|
119
|
+
errorCode: "ANY",
|
|
120
|
+
});
|
|
121
|
+
} catch {
|
|
122
|
+
threw = true;
|
|
123
|
+
}
|
|
124
|
+
expect(threw).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("toRejectWith fails when errorCode mismatches", async () => {
|
|
128
|
+
let threw = false;
|
|
129
|
+
try {
|
|
130
|
+
await expectFn(async () => {
|
|
131
|
+
throw makeError("OTHER");
|
|
132
|
+
}).toRejectWith({ errorCode: "EXPECTED" });
|
|
133
|
+
} catch {
|
|
134
|
+
threw = true;
|
|
135
|
+
}
|
|
136
|
+
expect(threw).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("toRejectWith supports regex messages", async () => {
|
|
140
|
+
await expectFn(async () => {
|
|
141
|
+
throw makeError("X", "boom happened");
|
|
142
|
+
}).toRejectWith({ message: /boom/ });
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("createExpectApi — descriptor matchers", () => {
|
|
147
|
+
const expectFn = createExpectApi();
|
|
148
|
+
|
|
149
|
+
test("toHaveInteraction finds a descriptor by interactionId", () => {
|
|
150
|
+
const descriptors = [
|
|
151
|
+
makeDescriptor({ interactionId: "placeThingCard" }),
|
|
152
|
+
makeDescriptor({ interactionId: "judgeCard" }),
|
|
153
|
+
];
|
|
154
|
+
expectFn(descriptors).toHaveInteraction("placeThingCard");
|
|
155
|
+
expect(() => expectFn(descriptors).toHaveInteraction("unknown")).toThrow();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("toHaveInteraction supports partial descriptor matching", () => {
|
|
159
|
+
const descriptors = [
|
|
160
|
+
makeDescriptor({
|
|
161
|
+
interactionId: "placeThingCard",
|
|
162
|
+
available: true,
|
|
163
|
+
}),
|
|
164
|
+
];
|
|
165
|
+
expectFn(descriptors).toHaveInteraction("placeThingCard", {
|
|
166
|
+
available: true,
|
|
167
|
+
});
|
|
168
|
+
expect(() =>
|
|
169
|
+
expectFn(descriptors).toHaveInteraction("placeThingCard", {
|
|
170
|
+
available: false,
|
|
171
|
+
}),
|
|
172
|
+
).toThrow();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("not.toHaveInteraction passes when missing", () => {
|
|
176
|
+
const descriptors = [makeDescriptor({ interactionId: "placeThingCard" })];
|
|
177
|
+
expectFn(descriptors).not.toHaveInteraction("missing");
|
|
178
|
+
expect(() =>
|
|
179
|
+
expectFn(descriptors).not.toHaveInteraction("placeThingCard"),
|
|
180
|
+
).toThrow();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("toBeGatedBy asserts unavailable descriptor with a reason", () => {
|
|
184
|
+
const descriptor = makeDescriptor({
|
|
185
|
+
interactionId: "placeThingCard",
|
|
186
|
+
available: false,
|
|
187
|
+
unavailableReason: "NOT_YOUR_TURN",
|
|
188
|
+
});
|
|
189
|
+
expectFn(descriptor).toBeGatedBy("NOT_YOUR_TURN");
|
|
190
|
+
expect(() => expectFn(descriptor).toBeGatedBy("OTHER")).toThrow();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("toBeGatedBy on array requires interactionId option", () => {
|
|
194
|
+
const descriptors = [
|
|
195
|
+
makeDescriptor({
|
|
196
|
+
interactionId: "placeThingCard",
|
|
197
|
+
available: false,
|
|
198
|
+
unavailableReason: "NOT_YOUR_TURN",
|
|
199
|
+
}),
|
|
200
|
+
];
|
|
201
|
+
expectFn(descriptors).toBeGatedBy("NOT_YOUR_TURN", {
|
|
202
|
+
interactionId: "placeThingCard",
|
|
203
|
+
});
|
|
204
|
+
expect(() => expectFn(descriptors).toBeGatedBy("NOT_YOUR_TURN")).toThrow();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("toBeActiveFor asserts descriptor targets a player and is available", () => {
|
|
208
|
+
const descriptor = makeDescriptor({
|
|
209
|
+
interactionId: "placeThingCard",
|
|
210
|
+
available: true,
|
|
211
|
+
context: { to: "player-1" },
|
|
212
|
+
});
|
|
213
|
+
expectFn(descriptor).toBeActiveFor("player-1");
|
|
214
|
+
expect(() => expectFn(descriptor).toBeActiveFor("player-2")).toThrow();
|
|
215
|
+
|
|
216
|
+
const unavailable = makeDescriptor({
|
|
217
|
+
interactionId: "placeThingCard",
|
|
218
|
+
available: false,
|
|
219
|
+
context: { to: "player-1" },
|
|
220
|
+
});
|
|
221
|
+
expect(() => expectFn(unavailable).toBeActiveFor("player-1")).toThrow();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("toBeActiveFor on array finds by interactionId", () => {
|
|
225
|
+
const descriptors = [
|
|
226
|
+
makeDescriptor({
|
|
227
|
+
interactionId: "placeThingCard",
|
|
228
|
+
available: true,
|
|
229
|
+
context: { to: "player-1" },
|
|
230
|
+
}),
|
|
231
|
+
];
|
|
232
|
+
expectFn(descriptors).toBeActiveFor("player-1", {
|
|
233
|
+
interactionId: "placeThingCard",
|
|
234
|
+
});
|
|
235
|
+
expect(() => expectFn(descriptors).toBeActiveFor("player-1")).toThrow();
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe("createExpectApi — snapshot matcher", () => {
|
|
240
|
+
test("toMatchSnapshot delegates to the configured handler", () => {
|
|
241
|
+
const calls: Array<{ name: string | undefined; actual: unknown }> = [];
|
|
242
|
+
const expectFn = createExpectApi({
|
|
243
|
+
matchSnapshot: (name, actual) => {
|
|
244
|
+
calls.push({ name, actual });
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
expectFn({ a: 1 }).toMatchSnapshot();
|
|
248
|
+
expectFn({ a: 2 }).toMatchSnapshot("seat-1.projection");
|
|
249
|
+
expect(calls).toEqual([
|
|
250
|
+
{ name: undefined, actual: { a: 1 } },
|
|
251
|
+
{ name: "seat-1.projection", actual: { a: 2 } },
|
|
252
|
+
]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("toMatchSnapshot throws when no handler is configured", () => {
|
|
256
|
+
const expectFn = createExpectApi();
|
|
257
|
+
expect(() => expectFn({ a: 1 }).toMatchSnapshot()).toThrow();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type {
|
|
3
|
+
ExpectFn,
|
|
4
|
+
ExpectMatchers,
|
|
5
|
+
InteractionDescriptorLike,
|
|
6
|
+
RejectionExpectation,
|
|
7
|
+
SnapshotMatcherHandler,
|
|
8
|
+
} from "./definitions.js";
|
|
9
|
+
|
|
10
|
+
type CreateExpectApiOptions = {
|
|
11
|
+
matchSnapshot?: SnapshotMatcherHandler;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
15
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function matchPartial(
|
|
19
|
+
actual: unknown,
|
|
20
|
+
expected: unknown,
|
|
21
|
+
path: string = "value",
|
|
22
|
+
): string | null {
|
|
23
|
+
if (!isRecord(expected)) {
|
|
24
|
+
return isDeepStrictEqual(actual, expected)
|
|
25
|
+
? null
|
|
26
|
+
: `${path} does not match`;
|
|
27
|
+
}
|
|
28
|
+
if (!isRecord(actual)) {
|
|
29
|
+
return `${path} is not an object`;
|
|
30
|
+
}
|
|
31
|
+
for (const [key, expectedValue] of Object.entries(expected)) {
|
|
32
|
+
const actualValue = actual[key];
|
|
33
|
+
if (!(key in actual)) {
|
|
34
|
+
return `${path}.${key} is missing`;
|
|
35
|
+
}
|
|
36
|
+
const mismatch = matchPartial(actualValue, expectedValue, `${path}.${key}`);
|
|
37
|
+
if (mismatch) {
|
|
38
|
+
return mismatch;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function asDescriptorList(actual: unknown): InteractionDescriptorLike[] {
|
|
45
|
+
if (!Array.isArray(actual)) {
|
|
46
|
+
throw new Error("Expected interaction descriptor array.");
|
|
47
|
+
}
|
|
48
|
+
return actual as InteractionDescriptorLike[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function findInteraction(
|
|
52
|
+
descriptors: readonly InteractionDescriptorLike[],
|
|
53
|
+
interactionId: string,
|
|
54
|
+
): InteractionDescriptorLike | null {
|
|
55
|
+
return (
|
|
56
|
+
descriptors.find(
|
|
57
|
+
(descriptor) => descriptor.interactionId === interactionId,
|
|
58
|
+
) ?? null
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function assertDescriptorMatches(
|
|
63
|
+
descriptor: InteractionDescriptorLike,
|
|
64
|
+
opts: Partial<InteractionDescriptorLike> | undefined,
|
|
65
|
+
): void {
|
|
66
|
+
if (!opts) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const mismatch = matchPartial(descriptor, opts, "interaction");
|
|
70
|
+
if (mismatch) {
|
|
71
|
+
throw new Error(mismatch);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createSubmissionErrorMatcher(
|
|
76
|
+
actual: unknown,
|
|
77
|
+
expected: RejectionExpectation,
|
|
78
|
+
): Promise<void> {
|
|
79
|
+
const resolvePromise = (): Promise<unknown> => {
|
|
80
|
+
if (typeof actual === "function") {
|
|
81
|
+
return Promise.resolve().then(() => (actual as () => unknown)());
|
|
82
|
+
}
|
|
83
|
+
return Promise.resolve(actual);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
return resolvePromise()
|
|
87
|
+
.then(() => {
|
|
88
|
+
throw new Error("Expected promise to reject.");
|
|
89
|
+
})
|
|
90
|
+
.catch((error: unknown) => {
|
|
91
|
+
if (!(error instanceof Error)) {
|
|
92
|
+
throw new Error("Expected rejection to be an Error.");
|
|
93
|
+
}
|
|
94
|
+
if (
|
|
95
|
+
expected.errorCode !== undefined &&
|
|
96
|
+
(error as Error & { errorCode?: string }).errorCode !==
|
|
97
|
+
expected.errorCode
|
|
98
|
+
) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Expected rejection errorCode '${expected.errorCode}', received '${
|
|
101
|
+
(error as Error & { errorCode?: string }).errorCode ?? "undefined"
|
|
102
|
+
}'.`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (
|
|
106
|
+
typeof expected.message === "string" &&
|
|
107
|
+
error.message !== expected.message
|
|
108
|
+
) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Expected rejection message '${expected.message}', received '${error.message}'.`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (
|
|
114
|
+
expected.message instanceof RegExp &&
|
|
115
|
+
!expected.message.test(error.message)
|
|
116
|
+
) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`Expected rejection message '${error.message}' to match ${String(expected.message)}.`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function assertLength(actual: unknown, expected: number): void {
|
|
125
|
+
if (
|
|
126
|
+
actual === null ||
|
|
127
|
+
actual === undefined ||
|
|
128
|
+
typeof (actual as { length?: unknown }).length !== "number"
|
|
129
|
+
) {
|
|
130
|
+
throw new Error("toHaveLength expects a value with a numeric length.");
|
|
131
|
+
}
|
|
132
|
+
const length = (actual as { length: number }).length;
|
|
133
|
+
if (length !== expected) {
|
|
134
|
+
throw new Error(`Expected length ${expected}, received ${length}.`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function assertThrown(
|
|
139
|
+
actual: unknown,
|
|
140
|
+
predicate?: string | RegExp | ((error: Error) => boolean),
|
|
141
|
+
): void {
|
|
142
|
+
if (typeof actual !== "function") {
|
|
143
|
+
throw new Error("toThrow expects a function.");
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
(actual as () => unknown)();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!(error instanceof Error)) {
|
|
149
|
+
throw new Error("Thrown value is not an Error.");
|
|
150
|
+
}
|
|
151
|
+
if (predicate === undefined) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (typeof predicate === "string" && error.message !== predicate) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Expected thrown message '${predicate}', received '${error.message}'.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (predicate instanceof RegExp && !predicate.test(error.message)) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Expected thrown message '${error.message}' to match ${String(predicate)}.`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (typeof predicate === "function" && !predicate(error)) {
|
|
165
|
+
throw new Error("Thrown error did not satisfy predicate.");
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
throw new Error("Expected function to throw.");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function createExpectApi(
|
|
173
|
+
options: CreateExpectApiOptions = {},
|
|
174
|
+
): ExpectFn {
|
|
175
|
+
const buildMatchers = (actual: unknown): ExpectMatchers => ({
|
|
176
|
+
toBe: (expected: unknown) => {
|
|
177
|
+
if (actual !== expected) {
|
|
178
|
+
throw new Error(
|
|
179
|
+
`Expected '${String(actual)}' to be '${String(expected)}'.`,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
toEqual: (expected: unknown) => {
|
|
184
|
+
if (!isDeepStrictEqual(actual, expected)) {
|
|
185
|
+
throw new Error("Expected values to be deeply equal.");
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
toMatchObject: (expected: Record<string, unknown>) => {
|
|
189
|
+
const mismatch = matchPartial(actual, expected);
|
|
190
|
+
if (mismatch) {
|
|
191
|
+
throw new Error(mismatch);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
toBeDefined: () => {
|
|
195
|
+
if (actual === undefined) {
|
|
196
|
+
throw new Error("Expected value to be defined.");
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
toBeUndefined: () => {
|
|
200
|
+
if (actual !== undefined) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Expected value to be undefined, but received '${String(actual)}'.`,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
toBeNull: () => {
|
|
207
|
+
if (actual !== null) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
`Expected value to be null, but received '${String(actual)}'.`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
toContain: (expected: unknown) => {
|
|
214
|
+
if (Array.isArray(actual)) {
|
|
215
|
+
if (!actual.includes(expected)) {
|
|
216
|
+
throw new Error("Expected array to contain value.");
|
|
217
|
+
}
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (typeof actual === "string") {
|
|
221
|
+
if (!actual.includes(String(expected))) {
|
|
222
|
+
throw new Error("Expected string to contain value.");
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
throw new Error("toContain expects an array or string actual value.");
|
|
227
|
+
},
|
|
228
|
+
toContainEqual: (expected: unknown) => {
|
|
229
|
+
if (!Array.isArray(actual)) {
|
|
230
|
+
throw new Error("toContainEqual expects an array actual value.");
|
|
231
|
+
}
|
|
232
|
+
if (!actual.some((value) => isDeepStrictEqual(value, expected))) {
|
|
233
|
+
throw new Error("Expected array to contain an equal value.");
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
toHaveLength: (expected: number) => {
|
|
237
|
+
assertLength(actual, expected);
|
|
238
|
+
},
|
|
239
|
+
toBeGreaterThanOrEqual: (expected: number) => {
|
|
240
|
+
if (typeof actual !== "number") {
|
|
241
|
+
throw new Error(
|
|
242
|
+
"toBeGreaterThanOrEqual expects a number actual value.",
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
if (actual < expected) {
|
|
246
|
+
throw new Error(`Expected ${actual} to be >= ${expected}.`);
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
toThrow: (predicate) => {
|
|
250
|
+
assertThrown(actual, predicate);
|
|
251
|
+
},
|
|
252
|
+
toMatchSnapshot: (filename) => {
|
|
253
|
+
if (!options.matchSnapshot) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
"Snapshot matching is not configured for this expect API.",
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
options.matchSnapshot(filename, actual);
|
|
259
|
+
},
|
|
260
|
+
toRejectWith: (expected) => createSubmissionErrorMatcher(actual, expected),
|
|
261
|
+
toHaveInteraction: (interactionId, opts) => {
|
|
262
|
+
const descriptors = asDescriptorList(actual);
|
|
263
|
+
const descriptor = findInteraction(descriptors, interactionId);
|
|
264
|
+
if (!descriptor) {
|
|
265
|
+
throw new Error(`Expected interaction '${interactionId}' to exist.`);
|
|
266
|
+
}
|
|
267
|
+
assertDescriptorMatches(descriptor, opts);
|
|
268
|
+
},
|
|
269
|
+
toBeGatedBy: (reason, opts) => {
|
|
270
|
+
const descriptor = Array.isArray(actual)
|
|
271
|
+
? (() => {
|
|
272
|
+
if (!opts?.interactionId) {
|
|
273
|
+
throw new Error(
|
|
274
|
+
"toBeGatedBy on a descriptor array requires opts.interactionId.",
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
return findInteraction(
|
|
278
|
+
asDescriptorList(actual),
|
|
279
|
+
opts.interactionId,
|
|
280
|
+
);
|
|
281
|
+
})()
|
|
282
|
+
: (actual as InteractionDescriptorLike | null);
|
|
283
|
+
if (!descriptor) {
|
|
284
|
+
throw new Error("Expected interaction descriptor to exist.");
|
|
285
|
+
}
|
|
286
|
+
if (descriptor.available !== false) {
|
|
287
|
+
throw new Error("Expected interaction to be unavailable.");
|
|
288
|
+
}
|
|
289
|
+
if (descriptor.unavailableReason !== reason) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
`Expected unavailableReason '${reason}', received '${
|
|
292
|
+
descriptor.unavailableReason ?? "undefined"
|
|
293
|
+
}'.`,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
toBeActiveFor: (playerId, opts) => {
|
|
298
|
+
const descriptor = Array.isArray(actual)
|
|
299
|
+
? (() => {
|
|
300
|
+
if (!opts?.interactionId) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
"toBeActiveFor on a descriptor array requires opts.interactionId.",
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
return findInteraction(
|
|
306
|
+
asDescriptorList(actual),
|
|
307
|
+
opts.interactionId,
|
|
308
|
+
);
|
|
309
|
+
})()
|
|
310
|
+
: (actual as InteractionDescriptorLike | null);
|
|
311
|
+
if (!descriptor) {
|
|
312
|
+
throw new Error("Expected interaction descriptor to exist.");
|
|
313
|
+
}
|
|
314
|
+
if (descriptor.context?.to !== playerId) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`Expected interaction to target '${playerId}', received '${
|
|
317
|
+
descriptor.context?.to ?? "undefined"
|
|
318
|
+
}'.`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
if (descriptor.available !== true) {
|
|
322
|
+
throw new Error("Expected interaction to be available.");
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
not: {
|
|
326
|
+
toHaveInteraction: (interactionId) => {
|
|
327
|
+
const descriptors = asDescriptorList(actual);
|
|
328
|
+
if (findInteraction(descriptors, interactionId)) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
`Expected interaction '${interactionId}' to be absent.`,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
return (actual: unknown) => buildMatchers(actual);
|
|
338
|
+
}
|