snap-shot-it is a snapshot testing utility designed for Mocha and other BDD-style JavaScript test runners. It provides a robust mechanism to capture the output of functions or data structures during test execution and compare them against previously saved snapshots, making it easier to track unintended changes. The current stable version is 7.9.10, released in late 2022, and its release cadence primarily focuses on dependency updates and minor bug fixes, indicating a maintenance-oriented development. A key differentiator is its approach of spying on the global `it` function to precisely determine test context, offering better reliability than static code parsing methods. It integrates `snap-shot-compare` for intelligent, human-readable diffs and supports a data-driven testing mode, which sets it apart from simpler snapshot solutions by offering more advanced testing patterns within the Mocha ecosystem.
npm install snap-shot-itVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `snap-shot-it` with Mocha, showing basic snapshot creation for synchronous values, strings, asynchronous results, and an example of data-driven testing. It includes instructions for running tests and updating snapshots.
Upgrade your Node.js environment to version 8.x or newer.
Ensure each named snapshot has a unique name per spec file. If intentional sharing is required, pass `{ allowSharedSnapshot: true }` as an option: `snapshot('my shared snapshot', value, { allowSharedSnapshot: true })`.Prefix your test command with the desired environment variable, e.g., `SNAPSHOT_UPDATE=1 npm test` to update snapshots.
If conflicts arise, review your test runner configuration and other test utilities to identify potential interactions with global `it` modifications. Consider isolating tests or using alternative snapshot solutions if deep customization is unavoidable.
Verify if the change is intentional. If it is, run your tests with `SNAPSHOT_UPDATE=1 npm test` to update the snapshot file. Otherwise, fix the code causing the unexpected output.
Upgrade to `snap-shot-it@7.9.2` or newer to resolve the `afterAll` hook crash. Alternatively, ensure at least one test is not skipped.
Ensure your project's module system is correctly configured. For CommonJS, use `const snapshot = require('snap-shot-it')`. For ES Modules, use `import snapshot from 'snap-shot-it'` and verify `package.json` specifies `"type": "module"` or files use `.mjs` extensions.