-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnonsense-tests.ts
326 lines (298 loc) · 10 KB
/
nonsense-tests.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import type { Quench } from "../quench";
/**
* Registers all example tests, which also serves as a quick self-test.
*
* @param quench - the quench instance the tests are registered with
*/
export function registerExampleTests(quench: Quench) {
for (const batchFunction of [
registerBasicPassingTestBatch,
registerBasicFailingTestBatch,
registerNestedTestBatch,
registerOtherTestBatch,
registerSnapshotTestBatch,
registerPropertyTestBatch,
registerPromiseTestBatch,
registerNonQuenchTestBatch,
]) {
batchFunction(quench);
}
}
function registerBasicPassingTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.basic-pass",
(context) => {
const { describe, it, assert, expect, should } = context;
describe("Passing Suite", function () {
it("Passing Test", function () {
assert.ok(true);
});
it("Passing Test using expect", function () {
expect(2).to.equal(2);
});
it("Passing Test using should", function () {
const foo = { bar: "baz" };
foo.should.have.property("bar", "baz");
});
it("Passing Test using should helper", function () {
should.not.equal(1, 2);
});
it("Passing Test with a snapshot", function () {
expect({ foo: "baz" }).to.matchSnapshot();
});
it("Passing Test with snapshot and assert", function () {
assert.matchSnapshot({ bar: "baz" });
});
});
},
{
displayName: "QUENCH: Basic Passing Test",
snapBaseDir: "__snapshots__/quench/some/other/weird/path",
},
);
}
function registerBasicFailingTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.basic-fail",
(context) => {
const { describe, it, assert, expect } = context;
describe("Failing Suite", function () {
it("Failing Test", function () {
expect(1).to.equal(2);
});
it("Another Failing Test", function () {
expect({ foo: "bar", baz: "bam", kel: { tok: "zam" } }).to.equal({ foo: { bar: "baz" } });
});
it("A Failing Test Using Assert", function () {
assert.ok(false);
});
it("A Failing Test Comparing an Object to undefined", function () {
expect({ foo: "bar" }).to.equal(undefined);
});
it("A Failing Test Comparing an Object to a String", function () {
expect({ foo: "bar" }).to.equal("bar");
});
it("A Failing Test Comparing undefined to a String", function () {
// eslint-disable-next-line unicorn/no-useless-undefined -- intentional test
expect(undefined).to.equal("bar");
});
});
},
{ displayName: "QUENCH: Basic Failing Test" },
);
}
function registerNestedTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.nested",
(context) => {
const { describe, it, assert, expect } = context;
describe("level 0", function () {
describe("level 1 A", function () {
it("passes A", function () {
assert.ok(true);
});
it("fails A", async function () {
assert.equal(1, 2, "not equal");
});
});
describe("level 1 B", function () {
it("times out", async function (this: Mocha.Context) {
this.timeout(200);
await quench.utils.pause(300);
assert.ok(true);
});
describe("level 2 B", function () {
it("a thing", function () {
assert.ok(true);
});
it("uses a snapshot in a nested test", function () {
expect({ foo: "bar" }).to.matchSnapshot();
});
});
it("fails B", function () {
assert.fail();
});
});
});
},
{ displayName: "QUENCH: Nested Suites" },
);
}
function registerOtherTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.other",
(context) => {
const { describe, it, assert } = context;
it("suite-less test", function () {
assert.ok(true);
});
it("pending test");
describe("suite alpha", function () {
it("test alpha", function () {
assert.ok(true);
});
});
describe("suite beta", function () {
it("test beta", function () {
assert.ok(true);
});
it("a nested pending test");
});
},
{ displayName: "QUENCH: Other" },
);
}
function registerSnapshotTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.snapshots",
(context) => {
const { describe, it, assert, expect } = context;
describe("Snapshot Testing", function () {
it("Passing Test using DOM element and expect", function () {
expect(document.querySelector("section #actors")).to.matchSnapshot();
});
it("Passing Test using simple object and assert", function () {
assert.matchSnapshot({ foo: "bar" });
});
it("Passing Test using should from prototype and string", function () {
"Some Test ¯\\_(ツ)_/¯".should.matchSnapshot();
});
it("Passing Test using temporary actor", function () {
// @ts-expect-error documentTypes will exist come v9
const types = game.system.entityTypes ?? game.system.documentTypes;
const actorType = types["Actor"][0];
const actor = new Actor({ name: "Test Actor", type: actorType });
expect(actor).to.matchSnapshot();
});
});
it("Suite-less Snapshot Test", function () {
expect(1).to.matchSnapshot();
});
},
{
displayName: "QUENCH: Snapshots Test",
snapBaseDir: "__snapshots__/quench/some/other/weird/path",
preSelected: false,
},
);
}
function registerPropertyTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.property",
(context) => {
const { describe, it, fc, expect, assert } = context;
// Code under test
// eslint-disable-next-line unicorn/consistent-function-scoping -- keep test contents together
const contains = (text: string, pattern: string) => text.includes(pattern);
describe("Basic Property Based Test", function () {
it("should always contain itself", function () {
fc.assert(
fc.property(fc.string(), (text) => {
assert.ok(contains(text, text));
}),
);
});
it("should always contain its substrings", function () {
fc.assert(
fc.property(fc.string(), fc.string(), fc.string(), (a, b, c) => {
// Regular assertions can be used (beware of longer error messages though)
expect(a + b + c).to.contain(b);
// Or return statements (failing on falsy values)
return contains(a + b + c, b);
}),
);
});
});
it("Failing Property Based Test", function () {
fc.assert(
// Returning false instead of throwing can improve error readability
fc.property(fc.string(), (text) => text.length < 5),
{ verbose: 1 },
);
});
},
{ displayName: "QUENCH: Property Test" },
);
}
function registerPromiseTestBatch(quench: Quench) {
quench.registerBatch(
"quench.examples.promises",
(context) => {
const { describe, it, assert, expect } = context;
describe("Chai as Promised", function () {
it("should resolve basic Promise", function () {
return Promise.resolve(2 + 2).should.eventually.equal(4);
});
it("should resolve Promise with property", function () {
expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("foo");
});
it("should allow async functions", async function () {
await Promise.resolve(2 + 2).should.eventually.equal(4);
});
it("should handle assertions in async functions", async function () {
return assert.eventually.equal(Promise.resolve(2 + 2), 4);
});
it("should handle Promise rejection", async function () {
await Promise.reject(new Error("Promise rejected")).should.be.rejectedWith(Error);
});
});
},
{ displayName: "QUENCH: Promises" },
);
}
// ============================ //
// Additional Quench self tests //
// ============================ //
let registerNonQuenchTestBatch = (_quench: Quench): void => undefined;
if (import.meta.env.DEV) {
registerNonQuenchTestBatch = (quench: Quench) => {
quench.registerBatch(
"not-quench.examples.basic", // should trigger an error notification due to non-package name
(context) => {
const { describe, it, assert } = context;
describe("Non-Quench Test", function () {
it("should pass", function () {
assert.ok(true);
});
it("should fail", function () {
assert.fail();
});
});
},
{ displayName: "QUENCH: Non-Quench Test" },
);
quench.registerBatch(
"quench.examples.batch-hook-error",
(context) => {
const { describe, before, it } = context;
before(() => {
throw new Error("This is an error thrown in a batch's before hook");
});
describe("This test should not run", function () {
it("should not run", function () {
unreachable();
});
});
},
{ displayName: "QUENCH: Batch Hook Error" },
);
quench.registerBatch(
"quench.examples.test-hook-error",
(context) => {
const { describe, it } = context;
describe("Suite failing due to hook", function () {
before(() => {
throw new Error("This is an error thrown in a suites's before hook");
});
it("should not run", function () {
unreachable();
});
});
},
{ displayName: "QUENCH: Suite Hook Error" },
);
};
}
function unreachable() {
throw new Error("This should not be reachable!");
}