Created
April 13, 2022 05:13
-
-
Save tera-ny/c9ba78a9aaad23b0ef75c7456744a838 to your computer and use it in GitHub Desktop.
Assertion function to perform SnapshotTest in Deno with update option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
deno test test.ts --allow-read --allow-write | |
# or | |
deno test test.ts --allow-read --allow-write -- update_snapshot # with update snapshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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