snapshot-context
snapshot-context
is Context For Snapshot Testing In Zoroaster.
yarn add -DE snapshot-context
Since Zoroaster 3.8.5, the snapshot functionality is enabled by returning the result of the test, there's no need to install this package manually.
Table Of Contents
SnapshotContext
class The snapshot context should be passed in to a Zoroaster
test suite in the context
property.
/** * @type {Object.<string, (c: Context, s: SnapshotContext)>} */const T = context: context snapshotContext async 'produces correct output' TEST_SUITE_PATH test setDir const promise = const stdout = await promise await
API
There is a set of methods made available by the API.
setDir(path:string)
Sets the root directory to save to and read snapshots from. Otherwise, an absolute path to the file can be passed. By default, the test/snapshot
directory is used.
const SNAPSHOT_DIR = const stringContext = /** * A string with 1 new line */ s: 'I am all in a sea of wonders.\nI doubt;\nI fear;\n' /** * A string with 2 new lines */ t: 'I am all in a sea of wonders.\n\nI doubt;\n\nI fear;\n\n' /** @type {Object.<string, (ctxString: stringContext ctx: SnapshotContext)>} */const T = context: { Object } snapshotContext async 'replaces new lines' s t setDir test const res = await // absolute path without set-dir async 'replaces reverse new lines' s t test const res = const path = await
async test(path:string, actual: string|object)
Test whether a snapshot matches the one saved in the path. An equality of strings is asserted, and objects are deep-equal tested. Objects are serialised as JSON
for writing, and back when reading.
- If there's no snapshot file existing, the user is prompted to answer the
save snapshot
question with ay
to confirm new snapshot. - The path to the file will be ensured, so that all directories in the path are made.
- The error stack will start at the point where
test
is called.
The difference between objects will be shown using deepEqual
from the assert-diff
The difference between strings will be highlighted with erte
.
Copyright & License
GNU Affero General Public License v3.0
© Art Deco™ for ContextTesting 2020 |
---|