Karma Plugin for Snapshot Testing
karma-snapshot
provides a communication layer between browser and Karma to store and
retrieve snapshots.
Supported Assertion Libraries
Snapshot Format
Snapshot can be stored in different formats. Right now there are two formats supported: md
and indented-md
.
Markdown Format
This format is preferred when you specify language for code blocks in an assertion plugin. With this format, code editors will automatically highlight syntax of code blocks.
# `src/html.js` ## `Sub Suite` #### `HTML Snapshot` ```html<div> <span /></div>```
Indented Markdown Format
# `src/html.js` ## `Sub Suite` #### `HTML Snapshot` <div> <span /> </div>
Snapshot File Path
Snapshot file path is extracted from the name of the root suit cases and stored alongside with a tested files in a
__snapshots__
directory.
Snapshot file path can be changed by providing a custom pathResolver
in snapshot config.
Usage Example with Mocha and Chai
$ npm install karma karma-webpack karma-sourcemap-loader karma-snapshot karma-mocha \ karma-mocha-snapshot karma-mocha-reporter karma-chrome-launcher mocha \ chai chai-karma-snapshot webpack --save-dev
Karma configuration:
// karma.conf.jsconst webpack = ; module { config;};
Source file:
// src/index.js { return "Snapshot Test";}
Test file:
// __tests__/index.js;;;; ;
Run tests:
$ karma start
Update snapshots:
$ UPDATE=1 karma start --single-run
Prune snapshots:
$ PRUNE=1 karma start --single-run
Config
{ return path;} config;
Custom Snapshot Format
Snapshot config option format
also works with custom serialization formats. Custom snapshot serializer should have
interface:
Internals
Snapshot Data
karma-snapshot
plugin is communicating with a browser by assigning a global variable __snapshot__
on a window
object.
Snapshot data has a simple data structure:
declare global
When SnapshotState.update
variable is true
, it indicates that assertion plugin should run in update mode, and
instead of checking snapshots, it should update all values.
SnapshotState.suite
is a reference to the root suite.
SnapshotSuite
is a tree with snapshots that has a similar structure to test suites. children
property is used to
store references to children suites, and snapshots
is used to store snapshot lists for tests in the current snapshot.
Snapshots are stored as a list because each test can have multiple snapshot checks, and they should be automatically
indexed by their position.
Snapshot
is an object that stores details about snapshot. lang
property indicates which language should be used
in a markdown format to improve readability. code
property stores snapshot value that will be checked by an assertion
plugin.
visited
is a flag that should be marked by an assertion plugin when it visits suites and snapshots. Visited flags are
used to automatically prune removed snapshots.
dirty
is a flag that should be marked by an assertion plugin when it updates or adds a new snapshot.
Interface for Assertion Libraries
To make it easier to add support for assertion libraries, SnapshotState
has two methods that should be used when
creating an API for an assertion library.
get()
method tries to find a Snapshot
in a current snapshot state. It also automatically marks all nodes on the
path
as visited.
set()
method adds or updates an existing Snapshot
.
match()
method checks if two snapshots are matching in normalized form.
Here is an example how it should be used: