Repository
Current version released
4 years ago
Versions
Snap
Snap is a test snapshot library for Deno. modified from klick.
Goal
- Implement
assertSnapshoton testing in deno_std- After accomplished, this repo will not be maintained.
Features
- Two update modes:
updatefine grained update (only filtered tests, even within a single suite)refreshcoarse grained update (removes un-exercised snapshots within suites)
- Simple ability to
maskvariant fields, accessed via json-ptr - CI aware - does not update snapshots on CI server
- Snapshot files are valid JSON5
Required Permissions
In practice, we’ll need to use this:
--allow-read: to read snapshots--allow-write: to create/update snapshots--unstable: forfscapability from deno std-lib
Current deno implementation does not allow for wild-cards and globs in the
permissions allow-list. Ideally, this is the set of permissions snap needs:
--allow-read="./**/*.assertSnapshot": to read snapshots--allow-write="./**/*.assertSnapshot": to create/update snapshots--unstable: forfscapability from deno std-lib
Usage
Writing tests
import test from "https://deno.land/x/snap/mod.ts";
test(`Simple test`, ({ assertSnapshot}) => {
const actual = {
a: 'a',
b: 1
}
assertSnapshot(actual);
actual.c = 'c'
assertSnapshot(actual);
});
test(`Test with masks`, ({ assertSnapshot}) => {
const actual = {
a: 'a',
b: 1,
c: {
start: Date.now()
randomArray: [
Math.random(),
Math.random(),
]
}
}
const masks = ['/c/start', '/c/randomArray/*']
assertSnapshot(actual, masks);
});Masked snapshot:
'Test with masks': {
a: 'a',
b: 1,
c: {
start: '[MASKED number]',
randomArray: [
'[MASKED number]',
'[MASKED number]',
],
},
},Snapshot Management (Exercise from CLI)
New snapshots / Validate
Validate existing snapshots or create new snapshots
deno test --allow-write --allow-read --unstableFiltered snapshot update
Only updates snapshots for tests being executed. Honors ignore and only
attributes from Deno.TestDefinition
deno test --allow-write --allow-read --unstable --filter "/.*another.*/" -- --updateor
deno test --allow-write --allow-read --unstable --filter "/.*another.*/" -- --uRefresh snapshots
Update all snapshots, deleting any un-executed snapshots. Only does so if a test-suite is executed
deno test --allow-write --allow-read --unstable -- --refreshor
deno test --allow-write --allow-read --unstable -- --r