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.

[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:open
  • Created a year ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

7reactions
terencetcfcommented, Oct 27, 2022

A quick workaround is to wrap the array into an object. For example:

expect({ theArray }).toMatchSnapshot({ theArray: [...] });
6reactions
JamieMageecommented, Oct 6, 2022

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:

expect(
  getLockedDependencies(bundledPackageLockJson, 'ansi-regex', '3.0.0')
).toMatchSnapshot([
  {
    bundled: true,
    dev: true,
    integrity: 'sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=',
    resolved:
      'https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz',
    version: '3.0.0',
  },
]);

which errors with

Error: expect(received).toMatchSnapshot(properties)

Matcher error: Expected properties must be an object

Expected properties has type:  array
Expected properties has value: [{"bundled": true, "dev": true, "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", "version": "3.0.0"}]

    at Object.toMatchSnapshot (C:\src\renovate\node_modules\jest-snapshot\build\index.js:246:13)
    at __EXTERNAL_MATCHER_TRAP__ (C:\src\renovate\node_modules\expect\build\index.js:346:30)
    at Object.throwingMatcher (C:\src\renovate\node_modules\expect\build\index.js:347:15)
    at Object.<anonymous> (C:\src\renovate\lib\modules\manager\npm\update\locked-dependency\package-lock\get-locked.spec.ts:52:9)
    at Promise.then.completed (C:\src\renovate\node_modules\jest-circus\build\utils.js:333:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (C:\src\renovate\node_modules\jest-circus\build\utils.js:259:10)
    at _callCircusTest (C:\src\renovate\node_modules\jest-circus\build\run.js:277:40)
    at async _runTest (C:\src\renovate\node_modules\jest-circus\build\run.js:209:3)
    at async _runTestsForDescribeBlock (C:\src\renovate\node_modules\jest-circus\build\run.js:97:9)
    at async _runTestsForDescribeBlock (C:\src\renovate\node_modules\jest-circus\build\run.js:91:9)
    at async _runTestsForDescribeBlock (C:\src\renovate\node_modules\jest-circus\build\run.js:91:9)
    at async run (C:\src\renovate\node_modules\jest-circus\build\run.js:31:3)
    at async runAndTransformResultsToJestFormat (C:\src\renovate\node_modules\jest-circus\build\legacy-code-todo-rewrite\jestAdapterInit.js:136:21)
    at async jestAdapter (C:\src\renovate\node_modules\jest-circus\build\legacy-code-todo-rewrite\jestAdapter.js:92:19)
    at async runTestInternal (C:\src\renovate\node_modules\jest-runner\build\runTest.js:407:16)
    at async runTest (C:\src\renovate\node_modules\jest-runner\build\runTest.js:491:34)
Read more comments on GitHub >

github_iconTop 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 >
API Reference | Vitest
A blazing fast unit test framework powered by Vite.
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 >

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