Skip to content

Commit 583554d

Browse files
authored
feat: add createAssertSnapshot (#2403)
1 parent 15f9450 commit 583554d

File tree

4 files changed

+156
-3
lines changed

4 files changed

+156
-3
lines changed

testing/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,71 @@ The `assertSnapshot` function will only attempt to read from and write to
235235
snapshot files. As such, the allow list for `--allow-read` and `--allow-write`
236236
can be limited to only include existing snapshot files, if so desired.
237237

238+
### Options:
239+
240+
The `assertSnapshot` function optionally accepts an options object.
241+
242+
```ts
243+
// example_test.ts
244+
import { assertSnapshot } from "https://deno.land/std@$STD_VERSION/testing/snapshot.ts";
245+
246+
Deno.test("isSnapshotMatch", async function (t): Promise<void> {
247+
const a = {
248+
hello: "world!",
249+
example: 123,
250+
};
251+
await assertSnapshot(t, a, {
252+
// options
253+
});
254+
});
255+
```
256+
257+
You can also configure default options for `assertSnapshot`.
258+
259+
```ts
260+
// example_test.ts
261+
import { createAssertSnapshot } from "https://deno.land/std@$STD_VERSION/testing/snapshot.ts";
262+
263+
const assertSnapshot = createAssertSnapshot({
264+
// options
265+
});
266+
```
267+
268+
When configuring default options like this, the resulting `assertSnapshot`
269+
function will function the same as the default function exported from the
270+
snapshot module. If passed an optional options object, this will take precedence
271+
over the default options, where the value provded for an option differs.
272+
273+
It is possible to "extend" an `assertSnapshot` function which has been
274+
configured with default options.
275+
276+
```ts
277+
// example_test.ts
278+
import { createAssertSnapshot } from "https://deno.land/std@$STD_VERSION/testing/snapshot.ts";
279+
import { stripColor } from "https://deno.land/std@$STD_VERSION/fmt/colors.ts";
280+
281+
const assertSnapshot = createAssertSnapshot({
282+
dir: ".snaps",
283+
});
284+
285+
const assertMonochromeSnapshot = createAssertSnapshot<string>(
286+
{ serializer: stripColor },
287+
assertSnapshot,
288+
);
289+
290+
Deno.test("isSnapshotMatch", async function (t): Promise<void> {
291+
const a = "\x1b[32mThis green text has had it's colours stripped\x1b[39m";
292+
await assertMonochromeSnapshot(t, a);
293+
});
294+
```
295+
296+
```js
297+
// .snaps/example_test.ts.snap
298+
export const snapshot = {};
299+
300+
snapshot[`isSnapshotMatch 1`] = `This green text has had it's colours stripped`;
301+
```
302+
238303
### Version Control:
239304

240305
Snapshot testing works best when changes to snapshot files are comitted

testing/__snapshots__/snapshot_test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,11 @@ snapshot[`Snapshot Test - Regression #2144 1`] = `
364364
`;
365365
366366
snapshot[`Snapshot Test - Empty #2245 1`] = ``;
367+
368+
snapshot[`SnapshotTest - createAssertSnapshot > No Options 1`] = `This green text has had it's colours stripped`;
369+
370+
snapshot[`SnapshotTest - createAssertSnapshot - Options Object - Custom Name 1`] = `This green text has had it's colours stripped`;
371+
372+
snapshot[`SnapshotTest - createAssertSnapshot > Message 1`] = `"This snapshot has failed as expected"`;
373+
374+
snapshot[`SnapshotTest - createAssertSnapshot - Composite - Custom Name 1`] = `This green text has had it's colours stripped`;

testing/snapshot.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class AssertSnapshotContext {
358358
* ```
359359
*/
360360
export async function assertSnapshot<T>(
361-
t: Deno.TestContext,
361+
context: Deno.TestContext,
362362
actual: T,
363363
options: SnapshotOptions<T>,
364364
): Promise<void>;
@@ -442,3 +442,25 @@ export async function assertSnapshot(
442442
return context.name;
443443
}
444444
}
445+
446+
export function createAssertSnapshot<T>(
447+
options: SnapshotOptions<T>,
448+
baseAssertSnapshot: typeof assertSnapshot = assertSnapshot,
449+
): typeof assertSnapshot {
450+
return async function _assertSnapshot(
451+
context: Deno.TestContext,
452+
actual: T,
453+
messageOrOptions?: string | SnapshotOptions<T>,
454+
): Promise<void> {
455+
const mergedOptions: SnapshotOptions<T> = {
456+
...options,
457+
...(typeof messageOrOptions === "string"
458+
? {
459+
msg: messageOrOptions,
460+
}
461+
: messageOrOptions),
462+
};
463+
464+
await baseAssertSnapshot(context, actual, mergedOptions);
465+
};
466+
}

testing/snapshot_test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
22
import { stripColor } from "../fmt/colors.ts";
33
import { dirname, fromFileUrl, join, toFileUrl } from "../path/mod.ts";
4-
import { assert, assertInstanceOf, AssertionError, fail } from "./asserts.ts";
5-
import { assertSnapshot, serialize } from "./snapshot.ts";
4+
import {
5+
assert,
6+
assertInstanceOf,
7+
AssertionError,
8+
assertRejects,
9+
fail,
10+
} from "./asserts.ts";
11+
import { assertSnapshot, createAssertSnapshot, serialize } from "./snapshot.ts";
612

713
const SNAPSHOT_MODULE_URL = toFileUrl(join(
814
dirname(fromFileUrl(import.meta.url)),
@@ -491,3 +497,55 @@ Deno.test("Snapshot Test - Regression #2144", async (t) => {
491497
Deno.test("Snapshot Test - Empty #2245", async (t) => {
492498
await assertSnapshot(t, "", { serializer: (x) => x });
493499
});
500+
501+
Deno.test("SnapshotTest - createAssertSnapshot", async (t) => {
502+
const assertMonochromeSnapshot = createAssertSnapshot<string>({
503+
serializer: stripColor,
504+
});
505+
506+
await t.step("No Options", async (t) => {
507+
await assertMonochromeSnapshot(
508+
t,
509+
"\x1b[32mThis green text has had it's colours stripped\x1b[39m",
510+
);
511+
});
512+
513+
await t.step("Options Object", async (t) => {
514+
await assertMonochromeSnapshot(
515+
t,
516+
"\x1b[32mThis green text has had it's colours stripped\x1b[39m",
517+
{
518+
name:
519+
"SnapshotTest - createAssertSnapshot - Options Object - Custom Name",
520+
},
521+
);
522+
});
523+
524+
await t.step("Message", async (t) => {
525+
const assertMissingSnapshot = createAssertSnapshot<string>({
526+
mode: "assert",
527+
name: "[MISSING SNAPSHOT]",
528+
});
529+
530+
const err = await assertRejects(async () => {
531+
await assertMissingSnapshot(
532+
t,
533+
null,
534+
"This snapshot has failed as expected",
535+
);
536+
}, AssertionError);
537+
538+
await assertSnapshot(t, err.message);
539+
});
540+
541+
await t.step("Composite", async (t) => {
542+
const assertMonochromeSnapshotComposite = createAssertSnapshot<string>({
543+
name: "SnapshotTest - createAssertSnapshot - Composite - Custom Name",
544+
}, assertMonochromeSnapshot);
545+
546+
await assertMonochromeSnapshotComposite(
547+
t,
548+
"\x1b[32mThis green text has had it's colours stripped\x1b[39m",
549+
);
550+
});
551+
});

0 commit comments

Comments
 (0)