Smarter snapshot utility for Mocha and BDD test runners + data-driven testing!
This tool makes snapshot testing for Mocha (and other BDD)
frameworks quick and painless. This module spies on global
which allows it to accurately get test information (beating static code parsing
done in snap-shot); it should work in transpiled code.
This package uses snap-shot-compare to display object and text difference intelligently.
Requires Node version 4 or above.
npm install --save-dev snap-shot-it
Example from spec.js
const snapshot =
Run Mocha tests, then open the created snapshots/spec.js file
exports'example works 1' = 30exports'example works 2' = "a text message"exports'example works 3' = 42
Suppose you change the resolved value from
const snapshot =
The test will fail
1) example works: Error: 42 !== 80
The error message should intelligently handle numbers, objects, arrays, multi-line text, etc.
You can see the saves snapshot values by running with environment variable
SNAPSHOT_SHOW=1 npm test
You can see snapshot values without writing them into the snapshot file
SNAPSHOT_DRY=1 npm test
You can update snapshot values
SNAPSHOT_UPDATE=1 npm test
Renaming tests might lead to confusion and pruning snapshots. You can name the snapshots yourself
const value = 42
The snapshots will be saved as
exports'my name 1' = 42
Writing multiple input / output pairs for a function under test quickly becomes tedious. Luckily, you can test a function by providing multiple inputs and a single snapshot of function's behavior will be saved.
// checks if n is primeconst isPrime = ...
The saved snapshot file will have clear mapping between given input and produced result
// snapshot fileexports'tests prime 1' ="name": "isPrime""behavior":"given": 1"expect": false"given": 2"expect": true"given": 3"expect": true"given": 4"expect": false"given": 5"expect": true...
You can also test functions that expect multiple arguments by providing arrays of inputs.
const add = a + b
Again, the snapshot file gives clear picture of the
// snapshot fileexports'checks behavior of binary function add 1' ="name": "add""behavior":"given":12"expect": 3"given":22"expect": 4"given":-55"expect": 0"given":1011"expect": 21
See src/data-driven-spec.js for more examples.
Run with environment variable
DEBUG=snap-shot-it ... to see log messages.
Because under the hood it uses snap-shot-core you might
want to show messages from both libraries with
CoffeeScript example is in coffee-demo folder. Watch mode is working properly.
Came during WorkBar Cambridge Happy Hour on the terrace as I was thinking about
difficulty of adding CoffeeScript / TypeScript support to
snap-shot project. Got the idea of overriding
snap-shot because a day before I wrote repeat-it
which overrides it and it is very simple repeat/src/index.js.
This NPM module is part of my experiments with snapshot testing. There are lots of other ones, blog posts and slides on this topic.
Author: Gleb Bahmutov <firstname.lastname@example.org> © 2017
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2017 Gleb Bahmutov <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.