jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Arrays with extra properties aren't handled well

Open benblank opened this issue 3 years ago • 12 comments

Version

28.1.2 - 29.3.1

Steps to reproduce

  1. git clone https://github.com/benblank/jest-arrays-with-extra-properties.git
  2. cd jest-arrays-with-extra-properties
  3. npm install
  4. npm test
  5. Observe that all four tests failed.

Expected behavior

Arrays' extra properties should be considered when comparing them and displayed in failure messages when relevant.

Actual behavior

Arrays' extra properties are only compared by some matchers (e.g. toEqual does, but toMatchInlineSnapshot does not) and even those which correctly compare them don't display the differences in failure messages (instead just reporting that the received value "serializes to the same string").

Additional context

The current behavior is unhelpful both because it is inconsistent (e.g. between toEqual and toMatchInlineSnapshot) and because it doesn't give insight into the nature of the failures.

I haven't yet dug too deeply into this behavior, but it appears to be due to arrays' extra properties not being included during serialization (both prior to comparison in failure messages and for snapshots).

(Note: The failure messages in the example can be a bit difficult to decipher, as they are displaying comparisons of failure messages (so you get a failure message embedded in a failure message). There are explanatory comments in each test which hopefully describe the behaviors more clearly.)

Environment

System:
  OS: Linux 5.10 Ubuntu 20.04.4 LTS (Focal Fossa)
  CPU: (16) x64 Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz
Binaries:
  Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
  npm: 8.13.2 - ~/.nvm/versions/node/v16.15.1/bin/npm
npmPackages:
  jest: ^28.1.2 => 28.1.2

benblank avatar Jul 05 '22 04:07 benblank

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[bot] avatar Aug 04 '22 04:08 github-actions[bot]

This issue persists in v28.1.3 (unsurprisingly) as well as v29.0.0-alpha.5 (though the inline snapshot also needs a minor tweak). The repro linked in the original post has been updated to use v29.0.0-alpha.5.

benblank avatar Aug 14 '22 19:08 benblank

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[bot] avatar Sep 13 '22 19:09 github-actions[bot]

The issue persists in v29.0.3. The repro linked in the original post has been updated.

benblank avatar Sep 13 '22 19:09 benblank

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[bot] avatar Oct 13 '22 19:10 github-actions[bot]

The issue persists in v29.1.2. The repro linked in the original post has been updated.

benblank avatar Oct 13 '22 22:10 benblank

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[bot] avatar Nov 12 '22 23:11 github-actions[bot]

The issue persists in v29.3.1. The repro linked in the original post has been updated.

benblank avatar Nov 13 '22 04:11 benblank

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[bot] avatar Dec 13 '22 04:12 github-actions[bot]

There has not been a release in the last 30 days; the repro is still up to date.

benblank avatar Dec 15 '22 18:12 benblank

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[bot] avatar Jan 14 '23 18:01 github-actions[bot]

Still no new release, so the repro is still up to date.

benblank avatar Jan 15 '23 20:01 benblank

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[bot] avatar Feb 14 '23 20:02 github-actions[bot]

The issue persists in v29.4.3. The repro linked in the original post has been updated.

benblank avatar Feb 16 '23 02:02 benblank

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[bot] avatar Mar 18 '23 02:03 github-actions[bot]

The issue persists in v29.5.0. The repro linked in the original post has been updated.

benblank avatar Mar 18 '23 18:03 benblank

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[bot] avatar Apr 17 '23 18:04 github-actions[bot]

There has not been a release in the last 30 days; the repro is still up to date.

benblank avatar Apr 26 '23 16:04 benblank

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[bot] avatar May 26 '23 17:05 github-actions[bot]

There has not been a release in the last 30 days; the repro is still up to date.

benblank avatar May 29 '23 04:05 benblank

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[bot] avatar Jun 28 '23 05:06 github-actions[bot]

There has not been a release in the last 30 days; the repro is still up to date.

benblank avatar Jun 28 '23 23:06 benblank

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[bot] avatar Jul 29 '23 01:07 github-actions[bot]

assert.equal() and assert.strictEqual() are failing with "Compared values have no visual difference" instead of "serializes to the same string".

JSON.stringify() does not show extra props just like toMatchInlineSnapshot().

I can only see the extra property being pick up by console.log(). It prints: [ 1, 2, 3, foo: 'bar' ].

Is this print out clear? Hm.. Looks unusual, but perhaps that’s the best way.

Other question: what to do with extra props on Map or Set?

For a Map with an extra prop, console prints {"foo":"bar"} omitting all the entries; structuredClone() omits extra props; same with console.log([...mapWithFooBar]).

I think the best solution is to avoid adding extra properties on iterable objects. It should be possible to make Jest respect them, but it does not mean that extra props will not cause problems elsewhere. I look just at few API and that already shows how unpredictable the behaviour can become.

mrazauskas avatar Jul 29 '23 05:07 mrazauskas

Ultimately, I think inconsistency is the real enemy, here; I'll update the issue description to that effect.

Unexpected properties are perfectly "reasonable" in a monkey-patchable language like JS, but they're probably still a pretty universally bad practice, due to potential confusion when reading the code. The best solution for Jest may simply be to issue a warning when encountering them, but otherwise completely ignore them for comparison purposes. (Which would both resolve the inconsistency and alert the user to the fact that they're doing something Jest doesn't support.)

You make a good point with regards to Map and Set; there are other classes which should probably be considered, too (Date comes to mind). Basically, a known set of classes for which unexpected own, enumerable properties aren't considered when comparing, because they're fairly uncommon and would complicate an otherwise clean representation.

benblank avatar Jul 31 '23 17:07 benblank

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[bot] avatar Aug 30 '23 18:08 github-actions[bot]

The issue persists in v29.6.4. The repro linked in the original post has been updated.

benblank avatar Sep 06 '23 23:09 benblank

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[bot] avatar Oct 07 '23 00:10 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Nov 06 '23 00:11 github-actions[bot]

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[bot] avatar Dec 07 '23 00:12 github-actions[bot]