deno.land / std@0.166.0 / testing / snapshot_test.ts

snapshot_test.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { stripColor } from "../fmt/colors.ts";import { dirname, fromFileUrl, join, toFileUrl } from "../path/mod.ts";import { assert, assertInstanceOf, AssertionError, assertRejects, fail,} from "./asserts.ts";import { assertSnapshot, createAssertSnapshot, serialize } from "./snapshot.ts";
const SNAPSHOT_MODULE_URL = toFileUrl(join( dirname(fromFileUrl(import.meta.url)), "snapshot.ts",));
function formatTestOutput(string: string) { // Strip colors and obfuscate any timings return stripColor(string).replace(/([0-9])+m?s/g, "--ms").replace( /(?<=running ([0-9])+ test(s)? from )(.*)(?=test.ts)/g, "<tempDir>/", );}
function formatTestError(string: string) { // Strip colors and remove "Check file:///workspaces/deno_std/testing/.tmp/test.ts" // as this is always output to stderr return stripColor(string).replace(/^Check file:\/\/(.+)\n/g, "");}
function testFnWithTempDir( fn: (t: Deno.TestContext, tempDir: string) => Promise<void>,) { return async (t: Deno.TestContext) => { const tempDir = await Deno.makeTempDir(); try { await fn(t, tempDir); } finally { await Deno.remove(tempDir, { recursive: true }); } };}
class TestClass { a = 1; b = 2; init() { this.b = 3; } get getA() { return this.a; } func() {}}
const map = new Map();map.set("Hello", "World!");map.set(() => "Hello", "World!");map.set(1, 2);
Deno.test("Snapshot Test", async (t) => { await assertSnapshot(t, { a: 1, b: 2 }); await assertSnapshot(t, new TestClass()); await assertSnapshot(t, map); await assertSnapshot(t, new Set([1, 2, 3])); await assertSnapshot(t, { fn() {} }); await assertSnapshot(t, function fn() {}); await assertSnapshot(t, [1, 2, 3]); await assertSnapshot(t, "hello world");});
Deno.test("Snapshot Test - step", async (t) => { await assertSnapshot(t, { a: 1, b: 2 }); await t.step("Nested", async (t) => { await assertSnapshot(t, new TestClass()); await assertSnapshot(t, map); await t.step("Nested Nested", async (t) => { await assertSnapshot(t, new Set([1, 2, 3])); await assertSnapshot(t, { fn() {} }); await assertSnapshot(t, function fn() {}); }); await assertSnapshot(t, [1, 2, 3]); }); await assertSnapshot(t, "hello world");});
Deno.test("Snapshot Test - Adverse String \\ ` ${}", async (t) => { await assertSnapshot(t, "\\ ` ${}");});
Deno.test("Snapshot Test - Multi-Line Strings", async (t) => { await t.step("string", async (t) => { await assertSnapshot( t, `<html> <head> <title>Snapshot Test - Multi-Line Strings</title> </head> <body> <h1> Snapshot Test - Multi-Line Strings </h2> <p> This is a snapshot of a multi-line string. </p> </body></html>`, ); });
await t.step("string in array", async (t) => { await assertSnapshot(t, [ `<h1> Header</h1>`, `<p> Content</p>`, ]); });
await t.step("string in object", async (t) => { await assertSnapshot(t, { str: ` Line #1 Line #2 Line #3`, }); });});
Deno.test( "Snapshot Test - Failed Assertion", testFnWithTempDir(async (t, tempDir) => { let count = 0; async function testFailedAssertion<T>( snapshot: T, actual: T, ): Promise<AssertionError> { const snapshotFilePath = join(tempDir, `snapshot_file_${++count}.snap`); await Deno.writeTextFile( snapshotFilePath, `export const snapshot = {};
snapshot[\`name 1\`] = \`${serialize(snapshot)}\`;`, );
try { await assertSnapshot(t, actual, { path: snapshotFilePath, mode: "assert", name: "name", }); fail("Snapshot assertion passed when it was expected to fail"); } catch (error) { assertInstanceOf(error, AssertionError); return error as AssertionError; } }
await t.step("Object", async (t) => { const error = await testFailedAssertion([1, 2, 3], [1, 2]); await assertSnapshot(t, stripColor(error.message)); });
await t.step("String", async (t) => { const error = await testFailedAssertion("Hello World!", "Hello!"); await assertSnapshot(t, stripColor(error.message)); }); }),);
Deno.test("Snapshot Test - Options", async (t) => { const VALUE = [1, 2, 3];
await t.step("dir", async (t) => { await t.step("relative", async (t) => { await assertSnapshot(t, VALUE, { dir: "__snapshots__/options_tests/", }); });
await t.step("absolute", async (t) => { await assertSnapshot(t, VALUE, { dir: join(Deno.cwd(), "testing/__snapshots__/options_tests/"), }); }); });
await t.step("path", async (t) => { await t.step("relative", async (t) => { await assertSnapshot(t, VALUE, { path: "__snapshots__/options_tests/custom_path.snap", }); });
await t.step("absolute", async (t) => { await assertSnapshot(t, VALUE, { path: join( Deno.cwd(), "testing/__snapshots__/options_tests/custom_path.snap", ), }); }); });
await t.step("name", async (t) => { await assertSnapshot(t, VALUE, { name: "custom name", });
await assertSnapshot(t, VALUE, { name: "custom name", }); });
await t.step("serializer", async (t) => { await assertSnapshot<Array<number>>(t, VALUE, { serializer: (actual) => { return `Array Length: ${actual.length}\n\n${serialize(actual)}`; }, }); });
await t.step("msg", async (t) => { await t.step("missing snapshot", async (t) => { try { await assertSnapshot<Array<number>>(t, VALUE, { msg: "[CUSTOM ERROR MESSAGE - MISSING SNAPSHOT]", mode: "assert", name: "MISSING SNAPSHOT", }); fail("Snapshot should not exist"); } catch (error) { assertInstanceOf(error, AssertionError); await assertSnapshot(t, error.message); } });
await t.step("missing snapshot file", async (t) => { try { await assertSnapshot<Array<number>>(t, VALUE, { msg: "[CUSTOM ERROR MESSAGE - MISSING SNAPSHOT]", mode: "assert", path: "MISSING_SNAPSHOT_FILE.snap", }); fail("Snapshot file should not exist"); } catch (error) { assertInstanceOf(error, AssertionError); await assertSnapshot(t, error.message); } }); });
await t.step( "mode", testFnWithTempDir(async (t, tempDir) => { const snapshotFilePath = join(tempDir, "snapshot.snap"); const snapshotName = "snapshot";
async function runTest(test: string) { const tempTestFileName = "test.ts"; const tempTestFilePath = join(tempDir, tempTestFileName); await Deno.writeTextFile(tempTestFilePath, test);
const process = await Deno.run({ cmd: [ Deno.execPath(), "test", "--allow-all", tempTestFilePath, "--", "-u", ], stdout: "piped", stderr: "piped", }); const output = await process.output(); const error = await process.stderrOutput(); process.close();
return { output: new TextDecoder().decode(output), error: new TextDecoder().decode(error), }; }
const result = await runTest(` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("${snapshotName}", async (t) => { await assertSnapshot(t, [1, 2, 3], { path: "${snapshotFilePath.replace(/\\/g, "\\\\")}", mode: "update", }); }); `);
const { snapshot } = await import(toFileUrl(snapshotFilePath).toString());
await assertSnapshot(t, snapshot[`${snapshotName} 1`]); await assertSnapshot(t, formatTestOutput(result.output)); assert(!formatTestError(result.error), "unexpected output to stderr"); }), );});
Deno.test( "Snapshot Test - Update", testFnWithTempDir(async (t, tempDir) => { const tempTestFileName = "test.ts"; const tempTestFilePath = join(tempDir, tempTestFileName); const tempSnapshotFilePath = join( tempDir, "__snapshots__", `${tempTestFileName}.snap`, );
async function runTestWithUpdateFlag(test: string) { await Deno.writeTextFile(tempTestFilePath, test);
const command = new Deno.Command(Deno.execPath(), { args: ["test", "--allow-all", tempTestFilePath, "--", "-u"], }); const { stdout, stderr } = await command.output();
return { output: new TextDecoder().decode(stdout), error: new TextDecoder().decode(stderr), snapshots: await Deno.readTextFile(tempSnapshotFilePath), }; }
function assertNoError(error: string) { if (formatTestError(error)) { throw new AssertionError(`Unexpected Error:\n\n${error}\n`); } }
/** * New snapshot */ const result1 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - Update", async (t) => { await assertSnapshot(t, [ 1, 2, ]); });`, );
assertNoError(result1.error); await assertSnapshot(t, formatTestOutput(result1.output), { name: "Snapshot Test - Update - New snapshot", }); await assertSnapshot(t, result1.snapshots, { name: "Snapshot Test - Update - New snapshot", });
/** * Existing snapshot - no changes */ const result2 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - Update", async (t) => { await assertSnapshot(t, [ 1, 2, ]); });`, );
assertNoError(result2.error); await assertSnapshot(t, formatTestOutput(result2.output), { name: "Snapshot Test - Update - Existing snapshot - no changes", }); await assertSnapshot(t, result2.snapshots, { name: "Snapshot Test - Update - Existing snapshot - no changes", });
/** * Existing snapshot - updates */ const result3 = await runTestWithUpdateFlag(` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - Update", async (t) => { await assertSnapshot(t, [ 1, 2, 3, 5, ]); }); `);
assertNoError(result3.error); await assertSnapshot(t, formatTestOutput(result3.output), { name: "Snapshot Test - Update - Existing snapshot - updates", }); await assertSnapshot(t, result3.snapshots, { name: "Snapshot Test - Update - Existing snapshot - updates", });
/** * Existing snapshots - reverse order 1 */ const result4 = await runTestWithUpdateFlag(` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - First", async (t) => { await assertSnapshot(t, "FIRST"); });
Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, "SECOND"); }); `);
assertNoError(result4.error); await assertSnapshot(t, formatTestOutput(result4.output), { name: "Snapshot Test - Update - Existing snapshots - reverse order 1", }); await assertSnapshot(t, result4.snapshots, { name: "Snapshot Test - Update - Existing snapshots - reverse order 1", });
/** * Existing snapshots - reverse order 2 */ const result5 = await runTestWithUpdateFlag(` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, "SECOND"); });
Deno.test("Snapshot Test - First", async (t) => { await assertSnapshot(t, "FIRST"); }); `);
assertNoError(result5.error); await assertSnapshot(t, formatTestOutput(result5.output), { name: "Snapshot Test - Update - Existing snapshots - reverse order 2", }); await assertSnapshot(t, result5.snapshots, { name: "Snapshot Test - Update - Existing snapshots - reverse order 2", }); }),);
// Regression test for https://github.com/denoland/deno_std/issues/2140// Long strings should not be truncated with ellipsisDeno.test("Snapshot Test - Regression #2140", async (t) => { await assertSnapshot(t, { title: "Testing a page", content: ` <h1>Testing a page</h1> <p>This is a test</p> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> `, });});
// Regression test for https://github.com/denoland/deno_std/issues/2144// Empty arrays should be compactedDeno.test("Snapshot Test - Regression #2144", async (t) => { const config = { fmt: { files: { exclude: [], include: [], }, options: {}, }, }; await assertSnapshot(t, config);});
Deno.test("Snapshot Test - Empty #2245", async (t) => { await assertSnapshot(t, "", { serializer: (x) => x });});
Deno.test("SnapshotTest - createAssertSnapshot", async (t) => { const assertMonochromeSnapshot = createAssertSnapshot<string>({ serializer: stripColor, });
await t.step("No Options", async (t) => { await assertMonochromeSnapshot( t, "\x1b[32mThis green text has had it's colours stripped\x1b[39m", ); });
await t.step("Options Object", async (t) => { await assertMonochromeSnapshot( t, "\x1b[32mThis green text has had it's colours stripped\x1b[39m", { name: "SnapshotTest - createAssertSnapshot - Options Object - Custom Name", }, ); });
await t.step("Message", async (t) => { const assertMissingSnapshot = createAssertSnapshot<string>({ mode: "assert", name: "[MISSING SNAPSHOT]", });
const err = await assertRejects(async () => { await assertMissingSnapshot( t, null, "This snapshot has failed as expected", ); }, AssertionError);
await assertSnapshot(t, err.message); });
await t.step("Composite", async (t) => { const assertMonochromeSnapshotComposite = createAssertSnapshot<string>({ name: "SnapshotTest - createAssertSnapshot - Composite - Custom Name", }, assertMonochromeSnapshot);
await assertMonochromeSnapshotComposite( t, "\x1b[32mThis green text has had it's colours stripped\x1b[39m", ); });});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2457

Version Info

Tagged at
2 months ago