You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constpath=require('path');// extend jest to have 'toMatchSpecificSnapshot' matcherrequire('jest-specific-snapshot');test('test',()=>{// provides snapshot file with absolute fileconstpathToSnap=path.resolve(process.cwd(),'./example/specific/dir/my.shot');expect(100).toMatchSpecificSnapshot(pathToSnap);//same snapshot but with relative fileexpect(14).toMatchSpecificSnapshot('./specific/dir/my.shot');// another snapshot file in the same testexpect(19).toMatchSpecificSnapshot('./specific/another_dir/another.shot');});
With Custom Serializer
// extend jest to have 'toMatchSpecificSnapshot' matcherconstaddSerializer=require('jest-specific-snapshot').addSerializer;addSerializer(/* Add custom serializer here */);test('test',()=>{expect(/* thing that matches the custom serializer */).toMatchSpecificSnapshot('./specific/custom_serializer/test.shot');});
Extend toMatchSpecificSnapshot
consttoMatchSpecificSnapshot=require('jest-specific-snapshot').toMatchSpecificSnapshot;expect.extend({toMatchDecoratedSpecificSnapshot(received,snapshotFile){// You can modify received data or create dynamic snapshot pathconstdata=doSomeThing(received);returntoMatchSpecificSnapshot.call(this,data,snapshotFile);},});
Limitations
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 the SnapshotState._updateSnapshot private field. TBD - try to use the globalConfig to get this state.
.toMatchSpecificSnapshot does ignore a custom serializers strategy. In order to support custom serializers, you should use the addSerializer method explicitly.