[Feature]: Support array inputs to `toMatchSnapshot()` matcher
See original GitHub issue🚀 Feature Proposal
Currently,
expect([{}]).toMatchSnapshot([expect.anything()]);
fails with
expect(received).toMatchSnapshot(properties)
Matcher error: Expected properties must be an object
Expected properties has type: array
Expected properties has value: [Anything]
(#13134, example repo here).
Motivation
This used to work in Jest v27 and would allow test cases like this again. It would help to avoid wrapping arrays in objects solely for the sake of snapshot testing.
Example
Something like
expect([{}, "foo"]).toMatchSnapshot([expect.anything(), "foo"]);
Pitch
This was supported in Jest v27 and the wording toMatchSnapshot
feels very intuitive as opposed to some custom toMatchArraySnapshot
(or the like) matcher. As a neat side effect, this would also allow to snapshot-test function parameter arrays.
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:7
Top Results From Across the Web
Using Jest property matchers on arrays of objects
My first attempt to test an array was to create the following test: test('sportsBallPeople as array', () => { expect(sportsBallPeople).
Read more >Jest Snapshots: The “Joker” Among Today's JavaScript ...
Throughout this article, I am going to share my personal experience with snapshot tests while taking a closer look at its up- and...
Read more >Making the Most of Snapshot Testing - Sam Hogarth
toMatchSnapshot() . The first time that a test with this assertion is run, Jest will generate a snapshot file, that's stored in a...
Read more >React Unit Testing Using Enzyme and Jest - Toptal
You may be tempted to execute the toMatchSnapshot function first and see the result in the ... const form = component.find('input'); // when...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A quick workaround is to wrap the array into an object. For example:
I think this should be changed to a bug report instead of a feature request. This was working for us in jest v29.0.3, but is broken when attempting to upgrade to v29.1.2.
Here’s a sample reproduction from our repo:
which errors with