tapshot
v2.2.0
Published
Snapshot assertions for TAP
Downloads
10
Readme
tapshot
Tapshot is a helper module for snapshot testing when you are using tools like tap and tape. Tapshot can be used to test any serializable object.
Install
$ npm install --save-dev tapshot
Usage
As of V2, tapshot will not automatically add new snapshots or snapshot files. You'll need to pass the 'update' option to save them. See Updating Snapshots for how to do this.
API
tapshot(tap, object[, options])
tap
object or function: A tap class that has theequal
andpass
methods. If you're not settingname
inoptions
, this also needs to contain a name, which will be provided if you use it within a named subtestobject
any: The object that you want to snapshot. This can be any serializable object. If it is a non-standard object, aserializer
must be provided inoptions
.options
object: The options to run the test with. These are all optional:name
string: The name for the snapshotfile
string: The location of the snapshot file you wish to useserializer
string or function: The serializer for the object provided if it is a non-standard object. If passed as a string, it will call that method on the object, if passed as a function, the funtion will be called with the object as the only argument.update
boolean: If set to true, the snapshot for this test will be updated
tapshot.configure(options)
This will return a tapshot instance that is configured to use the options passed in as defaults. This instance can be used the same as above.
options
object: The options to configure the tapshot instance with. These are optional:file
string: The location of the snapshot file you wish to useupdate
boolean: If set to true, the snapshots will be updated
Updating Snapshots
If you change the structure of the object being snapshotted, you will need to update your snapshots. There are 3 ways to do this:
update
Option
Set the update
option when calling tapshot. Like so:
tapshot(t, `console.log('this is a string of text')`, {update: true});
This will only update the snapshot for that specific test.
update
Configuration Option
Set the update
option when calling tapshot.configure()
. Like so:
tapshot.configure({update: true});
This will update the snapshots for any tests that use that instance.
Examples
const tap = require(`tap`);
const tapshot = require(`tapshot`);
tap.test(`Generated code is as expected`, (t) => {
tapshot(t, `console.log('this is a string of text')`);
t.end();
});
const tap = require(`tap`);
const tapshot = require(`tapshot`);
tap.test(`Generated code is as expected`, (t) => {
tapshot(t, `console.log('this is a string of text')`, {name: 'generated-code'});
t.end();
});
const tap = require(`tap`);
const tapshot = require(`tapshot`).configure({update: true});
tap.test(`Generated code is as expected`, (t) => {
tapshot(t, `console.log('this is a string of text')`);
t.end();
});
License
MIT © Luke Bonaccorsi