'How can I share snapshots between Jest tests?

Using TDD I'd like to write some new tests that create data in slightly different ways, and verify that that test data gets sanitized down to the same data as a previous test.

So after writing Test 1 and generating a snapshot, Test 2/3/4 should generate the same snapshot as Test 1.

How can I make that happen? Jest appears to prepend the test name to custom snapshot names so I can't use .match(test1name).

(Using all-new identical snapshots for each test bloats the snapshots file and seems far from ideal.)



Solution 1:[1]

You could do something like:

const r1 = fn1()

expect(r1).toMatchSnapshot()

const r2 = fn1()

expect(r2).toEqual(r1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom