Jest matcher for multiple snapshot files per test
npm i -D jest-specific-snapshot
const path = require('path');
// extend jest to have 'toMatchSpecificSnapshot' matcher
require('jest-specifics-snapshot');
test('test', () => {
// provides snapshot file with absolute file
const pathToSnap = path.resolve(process.cwd(), './example/specific/dir/my.shot');
expect(100).toMatchSpecificSnapshot(pathToSnap);
//same snapshot but with relative file
expect(14).toMatchSpecificSnapshot('./specific/dir/my.shot');
// another snapshot file in the same test
expect(19).toMatchSpecificSnapshot('./specific/another_dir/another.shot');
});
const path = require('path');
// extend jest to have 'toMatchSpecificSnapshot' matcher
const addSerializer = require('jest-specifics-snapshot').addSerializer;
addSerializer(/* Add custom serializer here */);
test('test', () => {
// another snapshot file in the same test
expect(/* thing that matches the custom serializer */).toMatchSpecificSnapshot('./specific/custom_serializer/test.shot');
});
- Snapshot files should have an extension other than
.snap
, since it conflicts with jest. - In order to handle the
--updateSnapshot
(-u
) parameter provided from CLI, there is an abuse of theSnapshotState._updateSnapshot
private field. TBD - try to use theglobalConfig
to get this state. .toMatchSpecificSnapshot
does ignore a custom serializers strategy. In order to support custom serializers, you should use theaddSerializer
method explicitly.- TBD