Freewalker
Freewalker

Reputation: 7355

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.)

Upvotes: 8

Views: 688

Answers (1)

Tom
Tom

Reputation: 4846

You could do something like:

const r1 = fn1()

expect(r1).toMatchSnapshot()

const r2 = fn1()

expect(r2).toEqual(r1)

Upvotes: 2

Related Questions