To use snapshot testing inside of your custom matcher you can import jest-snapshot and use it from within your matcher.

Here’s a snapshot matcher that trims a string to store for a given length, .toMatchTrimmedSnapshot(length):

const {toMatchSnapshot} = require('jest-snapshot'); expect.extend({ toMatchTrimmedSnapshot(received, length) { return toMatchSnapshot.call( this, received.substring(0, length), 'toMatchTrimmedSnapshot', ); }, }); it('stores only 10 characters', () => { expect('extra long string oh my gerd').toMatchTrimmedSnapshot(10); }); /* Stored snapshot will look like: exports[`stores only 10 characters: toMatchTrimmedSnapshot 1`] = `"extra long"`; */

It’s also possible to create custom matchers for inline snapshots, the snapshots will be correctly added to the custom matchers. However, inline snapshot will always try to append to the first argument or the second when the first argument is the property matcher, so it’s not possible to accept custom arguments in the custom matchers.

const {toMatchInlineSnapshot} = require('jest-snapshot'); expect.extend({ toMatchTrimmedInlineSnapshot(received) { return toMatchInlineSnapshot.call(this, received.substring(0, 10)); }, }); it('stores only 10 characters', () => { expect('extra long string oh my gerd').toMatchTrimmedInlineSnapshot(); /* The snapshot will be added inline like expect('extra long string oh my gerd').toMatchTrimmedInlineSnapshot( `"extra long"` ); */ });