Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support array inputs to toMatchSnapshot() matcher #13352

Closed
bjoluc opened this issue Oct 1, 2022 · 11 comments 路 Fixed by #14025
Closed

[Feature]: Support array inputs to toMatchSnapshot() matcher #13352

bjoluc opened this issue Oct 1, 2022 · 11 comments 路 Fixed by #14025

Comments

@bjoluc
Copy link

bjoluc commented Oct 1, 2022

馃殌 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.

@SimenB
Copy link
Member

SimenB commented Oct 3, 2022

Happy to take a PR adding support for this. 馃憤

If it used to work in Jest 27, it should be possible to go back and bisect to see what broke it.

@JamieMagee
Copy link

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)

@bjoluc
Copy link
Author

bjoluc commented Oct 7, 2022

@JamieMagee I previously opened #13134. Interestingly, my repro doesn't work with "jest": "29.0.3" neither 馃槙

@terencetcf
Copy link

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

expect({ theArray }).toMatchSnapshot({ theArray: [...] });

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Nov 26, 2022
@rarkins
Copy link

rarkins commented Nov 26, 2022

Not stale

@bryanjtc
Copy link

Any update on this?

@bakasmarius
Copy link
Contributor

Happy to take a PR adding support for this. 馃憤

If it used to work in Jest 27, it should be possible to go back and bisect to see what broke it.

I created a PR (#14025) that undoes the changes that broke the functionality.

@bakasmarius
Copy link
Contributor

I updated my PR (#14025) and now it also supports expect.anything() and expect.any() inside an array.
Hopefully, I'll get any feedback soon as anyone who is using arrays as snapshots are stuck with Jest v29.0.3 for ~8 months now 馃

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2023
@SimenB
Copy link
Member

SimenB commented Jul 4, 2023

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants