question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Snapshotting Arrays with Matchers

See original GitHub issue

🐛 Bug Report

Creating a snapshot with array + matchers is not possible without wrapping array into an object.

To Reproduce

expect(['foo']).toMatchInlineSnapshot([expect.any(String)]);

Expected behavior

expect(['foo']).toMatchInlineSnapshot([expect.any(String)], `
  Array [
    Any<String>,
  ]
`);

Actual behavior

expect(['foo']).toMatchInlineSnapshot([expect.any(String)], `
  Object {
    "0": "foo",
  }
`);

I’m able to get by by taking my result and placing it in an object then doing the snapshot, but I feel like I should be able to snapshot an actual array with matchers.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:8

github_iconTop GitHub Comments

13reactions
rmclaughlin-nelnetcommented, Mar 25, 2021

Just ran into this issue, would love to see a fix.

10reactions
falkenhawkcommented, Mar 4, 2020

Sadly, snapshot testing with property matchers is unusable with arrays at the moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Jest property matchers on arrays of objects
To test an array of objects using property matchers use forEach to loop over the array and snapshot test each object individually:
Read more >
Expect - Jest
arrayContaining(array); expect.assertions(number); expect. ... Here's a snapshot matcher that trims a string to store for a given length, .
Read more >
Taking Advantage of Jest Matchers (Part 2) - Herding Lions
Snapshot Matchers ​​ Snapshot testing is one of Jest's signature features. It's been described in depth in many other places, including on this ......
Read more >
Using Snapshots - Testing React Apps with Jest ... - Educative.io
Arrays can be snapshot tested using the toMatchSnapshot matcher in the same manner that we test an object. As an exercise, change the...
Read more >
Matchers - typescript
Note that the type must be either an array or a tuple. ... This ensures that a value matches the most recent snapshot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found