chai-snap
v1.2.4
Published
Snapshot testing plugin for Chai
Maintainers
Readme
chai-snap
Built on snap-shot-it, chai-snap extends Chai with a fluent assertion API for snapshot testing.
Instead of using snap-shot-it's native assertions:
snapshot('test');you can just say:
expect('test').to.snapshot();
'test'.should.snapshot();Features ✨
- Intuitive assertion syntax:
expect(value).to.snapshot() - Simple snapshot testing integration for Chai
- Chainable assertions: Seamlessly combine with other Chai methods
- Type safety
- Compatible with Mocha and other test runners
Installation 📦
npm install chai-snap --save-devUsage 🚀
- Add
chai-snapto your test setup:
use(chaiSnap);- Use in your tests:
expect('test').to.snapshot();Or if you prefer should:
'test'.should.snapshot();NEW in v1.2.0: Snapshots can be named for better test clarity:
expect('test').to.snapshot('test-snapshot');Basic Example
import { expect, use } from 'chai';
import chaiSnap from 'chai-snap';
// Add snapshot support to Chai
use(chaiSnap);
describe('Snapshot Testing', () => {
it('should match snapshot', () => {
expect('test').to.snapshot().and.be.a('string');
});
});Updating Snapshots
Set environment variable to update snapshots:
SNAPSHOT_UPDATE=1 npm testAPI Documentation 📖
expect(value).to.snapshot(name?)
value: Any value to compare against stored snapshotname: Optional name for the snapshot, generated from the name from the test title and snapshot index if not provided- Behavior:
- Creates new snapshot on first run
- Compares against stored snapshot in subsequent runs
- Updates snapshot when
SNAPSHOT_UPDATE=1is set
