Skip to content

Instantly share code, notes, and snippets.

@tera-ny
Created April 13, 2022 05:13
Show Gist options
  • Save tera-ny/c9ba78a9aaad23b0ef75c7456744a838 to your computer and use it in GitHub Desktop.
Save tera-ny/c9ba78a9aaad23b0ef75c7456744a838 to your computer and use it in GitHub Desktop.
Assertion function to perform SnapshotTest in Deno with update option
import {
assertEquals,
equal,
} from "https://deno.land/std@0.132.0/testing/asserts.ts";
import { copy } from "https://deno.land/std@0.132.0/fs/mod.ts";
export const assertSnapshot = async (
receivedPath: string,
expectedPath: string
) => {
const [received, expected] = await Promise.all([
Deno.readTextFile(receivedPath),
Deno.readTextFile(expectedPath),
]);
if (Deno.args.includes("update_snapshot")) {
if (equal(received, expected)) return;
await copy(receivedPath, expectedPath, { overwrite: true });
} else {
assertEquals(received, expected);
}
};
#!/bin/bash
deno test test.ts --allow-read --allow-write
# or
deno test test.ts --allow-read --allow-write -- update_snapshot # with update snapshots
Deno.test(async()=>{
const tmp = await Deno.makeTempFile();
//write tmp file
...
await assertSnapshot(tmp, "test.snapshot");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment