deno.land / std@0.224.0 / testing / snapshot_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { stripAnsiCode } from "../fmt/colors.ts";import { dirname, fromFileUrl, join, toFileUrl } from "../path/mod.ts";import { assert, assertInstanceOf, AssertionError, assertRejects, fail,} from "../assert/mod.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 stripAnsiCode(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 stripAnsiCode(string).replace(/^Check file:\/\/(.+)\n/gm, "");}
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 }); } };}
function testFnWithDifferentTempDir( fn: ( t: Deno.TestContext, tempDir1: string, tempDir2: string, ) => Promise<void>,) { return async (t: Deno.TestContext) => { const tempDir1 = await Deno.makeTempDir(); const tempDir2 = await Deno.makeTempDir(); try { await fn(t, tempDir1, tempDir2); } finally { await Deno.remove(tempDir1, { recursive: true }); await Deno.remove(tempDir2, { 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("assertSnapshot()", 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("assertSnapshot() - 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("double-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("assertSnapshot() - adverse string \\ ` ${}", async (t) => { await assertSnapshot(t, "\\ ` ${}");});
Deno.test("assertSnapshot() - default serializer", async (t) => { await assertSnapshot(t, "a\nb\tc");});
Deno.test("assertSnapshot() - 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( "assertSnapshot() - 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, stripAnsiCode(error.message)); });
await t.step("string", async (t) => { const error = await testFailedAssertion("Hello World!", "Hello!"); await assertSnapshot(t, stripAnsiCode(error.message)); }); }),);
Deno.test("assertSnapshot() - 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 = new Deno.Command(Deno.execPath(), { args: [ "test", "--no-lock", "--allow-all", tempTestFilePath, "--", "-u", ], stdout: "piped", stderr: "piped", }); const { stdout, stderr } = await process.output();
return { output: new TextDecoder().decode(stdout), error: new TextDecoder().decode(stderr), }; }
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( "assertSnapshot() - 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", "--no-lock", "--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("assertSnapshot() - update", async (t) => { await assertSnapshot(t, [ 1, 2, ]); });`, );
assertNoError(result1.error); await assertSnapshot(t, formatTestOutput(result1.output), { name: "assertSnapshot() - update - New snapshot", }); await assertSnapshot(t, result1.snapshots, { name: "assertSnapshot() - update - New snapshot", });
/** * Existing snapshot - no changes */ const result2 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("assertSnapshot() - update", async (t) => { await assertSnapshot(t, [ 1, 2, ]); });`, );
assertNoError(result2.error); await assertSnapshot(t, formatTestOutput(result2.output), { name: "assertSnapshot() - update - Existing snapshot - no changes", }); await assertSnapshot(t, result2.snapshots, { name: "assertSnapshot() - update - Existing snapshot - no changes", });
/** * Existing snapshot - updates */ const result3 = await runTestWithUpdateFlag(` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("assertSnapshot() - update", async (t) => { await assertSnapshot(t, [ 1, 2, 3, 5, ]); }); `);
assertNoError(result3.error); await assertSnapshot(t, formatTestOutput(result3.output), { name: "assertSnapshot() - update - Existing snapshot - updates", }); await assertSnapshot(t, result3.snapshots, { name: "assertSnapshot() - 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: "assertSnapshot() - update - Existing snapshots - reverse order 1", }); await assertSnapshot(t, result4.snapshots, { name: "assertSnapshot() - 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: "assertSnapshot() - update - Existing snapshots - reverse order 2", }); await assertSnapshot(t, result5.snapshots, { name: "assertSnapshot() - update - Existing snapshots - reverse order 2", }); }),);
Deno.test( "assertSnapshot() - remove", testFnWithTempDir(async (t, tempDir) => { const tempTestFileName = "test.ts"; const tempTestFilePath = join(tempDir, tempTestFileName);
async function runTestWithUpdateFlag(test: string) { await Deno.writeTextFile(tempTestFilePath, test);
const command = new Deno.Command(Deno.execPath(), { args: [ "test", "--no-lock", "--allow-all", tempTestFilePath, "--", "-u", ], }); const { stdout, stderr } = await command.output();
return { output: new TextDecoder().decode(stdout), error: new TextDecoder().decode(stderr), }; }
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("assertSnapshot() - remove - First", async (t) => { await assertSnapshot(t, { a: 1, b: 2 }); });
Deno.test("assertSnapshot() - remove - Second", async (t) => { await assertSnapshot(t, { c: 3, d: 4 }); });
Deno.test("assertSnapshot() - remove - Third", async (t) => { await assertSnapshot(t, { e: 5, f: 6 }); });
Deno.test("assertSnapshot() - remove - Fourth", async (t) => { await assertSnapshot(t, { g: 7, h: 8 }); });
Deno.test("assertSnapshot() - remove - Fifth", async (t) => { await assertSnapshot(t, { i: 9, j: 10 }); }); `, );
assertNoError(result1.error); await assertSnapshot(t, formatTestOutput(result1.output), { name: "assertSnapshot() - remove - New snapshot", });
/** * Existing snapshot - removes one */ const result2 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("assertSnapshot() - remove - First", async (t) => { await assertSnapshot(t, { a: 1, b: 2 }); });
Deno.test("assertSnapshot() - remove - Second", async (t) => { await assertSnapshot(t, { c: 3, d: 4 }); });
Deno.test("assertSnapshot() - remove - Fourth", async (t) => { await assertSnapshot(t, { g: 7, h: 8 }); });
Deno.test("assertSnapshot() - remove - Fifth", async (t) => { await assertSnapshot(t, { i: 9, j: 10 }); }); `, );
assertNoError(result2.error); await assertSnapshot(t, formatTestOutput(result2.output), { name: "assertSnapshot() - remove - Existing snapshot - removed one", });
/** * Existing snapshot - removes several */ const result3 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("assertSnapshot() - remove - First", async (t) => { await assertSnapshot(t, { a: 1, b: 2 }); }); `, );
assertNoError(result3.error); await assertSnapshot(t, formatTestOutput(result3.output), { name: "assertSnapshot() - remove - Existing snapshot - removed several", }); }),);
Deno.test( "assertSnapshot() - different directory", testFnWithDifferentTempDir(async (t, tempDir1, tempDir2) => { const tempTestFileName = "test.ts"; const tempTestFilePath1 = join(tempDir1, tempTestFileName); const tempTestFilePath2 = join(tempDir2, tempTestFileName);
async function runTestWithUpdateFlag(test1: string, test2: string) { await Deno.writeTextFile(tempTestFilePath1, test1); await Deno.writeTextFile(tempTestFilePath2, test2);
const command = new Deno.Command(Deno.execPath(), { args: [ "test", "--no-lock", "--allow-all", tempTestFilePath1, tempTestFilePath2, "--", "-u", ], }); const { stdout, stderr } = await command.output();
return { output: new TextDecoder().decode(stdout), error: new TextDecoder().decode(stderr), }; }
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 - First", async (t) => { await assertSnapshot(t, [ 1, 2, ]); }); Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, [ 3, 4, ]); });`, ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - First", async (t) => { await assertSnapshot(t, [ 1, 2, ]); }); Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, [ 3, 4, ]); });`, );
assertNoError(result1.error); await assertSnapshot(t, formatTestOutput(result1.output), { name: "assertSnapshot() - different directory - New snapshot", });
/** * Existing snapshot - updates */ const result2 = await runTestWithUpdateFlag( ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - First", async (t) => { await assertSnapshot(t, [ 1, 2, ]); }); Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, [ 3, 5, ]); });`, ` import { assertSnapshot } from "${SNAPSHOT_MODULE_URL}";
Deno.test("Snapshot Test - First", async (t) => { await assertSnapshot(t, [ 6, 7, ]); }); Deno.test("Snapshot Test - Second", async (t) => { await assertSnapshot(t, [ 8, 9, ]); });`, ); assertNoError(result2.error); await assertSnapshot(t, formatTestOutput(result2.output), { name: "assertSnapshot() - different directory - Existing snapshot - update", }); }),);
// Regression test for https://github.com/denoland/deno_std/issues/2140// Long strings should not be truncated with ellipsisDeno.test("assertSnapshot() - 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("assertSnapshot() - regression #2144", async (t) => { const config = { fmt: { files: { exclude: [], include: [], }, options: {}, }, }; await assertSnapshot(t, config);});
Deno.test("assertSnapshot() - empty #2245", async (t) => { await assertSnapshot(t, "", { serializer: (x) => x });});
Deno.test("createAssertSnapshot()", async (t) => { const assertMonochromeSnapshot = createAssertSnapshot<string>({ serializer: stripAnsiCode, });
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: "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: "createAssertSnapshot() - composite - custom Name", }, assertMonochromeSnapshot);
await assertMonochromeSnapshotComposite( t, "\x1b[32mThis green text has had it's colours stripped\x1b[39m", ); });});
Version Info