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 <select>.options breaks in pretty-format

See original GitHub issue

🐛 Bug Report

Snapshotting the options property on a <select> element breaks in pretty-format.

Jest v22.1.2 on node v7.4.0 (the provided repl.it, see below) fails with:

TypeError: Cannot delete property 'cldr' of #<Object>
      
      at printObjectProperties (../../usr/local/share/.config/yarn/global/node_modules/pretty-format/build/collections.js:140:32)

Jest v23.6.0 on Node 10.11.0 (locally) fails with:

TypeError: Method get TypedArray.prototype.length called on incompatible receiver [object Object]
        at Uint8Array.get length [as length] (<anonymous>)

      at printComplexValue (../../node_modules/pretty-format/build/index.js:200:24)

To Reproduce

Create a <select> with at least one <option>, then attempt to snapshot select.options:

it('snapshots select options', () => {
  const select = document.createElement('select');
  select.innerHTML = '<option value="one">one</option>';

  expect(select.options).toMatchSnapshot();
});

Expected behavior

A snapshot should be created with the content of the options collection.

Link to repl or repo (highly encouraged)

https://repl.it/repls/CurvyKnobbyModel

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 10.11.0 - /usr/local/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrottimarkcommented, Oct 8, 2018

@SimenB Thanks for cc. Notice HTML elements from jsdom instead of React elements 😃

Problem: pretty-format fails to serialize internals of a simulated DOM object instance as a generic object, because built-in snapshot serializers don’t match the class of select.options property:

HTMLOptionsCollection is an interface representing a collection of HTML option elements (in document order) and offers methods and properties for traversing the list as well as optionally altering its items. This type is returned solely by the “options” property of select.

Solution: Explicitly convert options or children property from array-like object to JavaScript array:

it('snapshots select options', () => {
  const select = document.createElement('select');
  select.innerHTML = '<option value="one">one</option>';

  expect(Array.from(select.options)).toMatchSnapshot();
});
it('snapshots select options', () => {
  const select = document.createElement('select');
  select.innerHTML = '<option value="one">one</option>';

  expect(Array.from(select.children)).toMatchSnapshot();
});

Here is the stored snapshot:

exports[`snapshots select options 1`] = `
Array [
  <option
    value="one"
  >
    one
  </option>,
]
`;

Although the thought has crossed my mind whether built-in plugins could support HTMLCollection or NodeList object instances created in browser or by jsdom package, so far I decided not, because everyone pays in every snapshot test for each additional test condition.

@theneva If this change to the assertion works for you, can you please close the issue?

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Snapshot not being properly formatted with pretty format ...
I'm using jest 28.1.1. When I run jest -updateSnapshot it updates them without the pretty format. I tried updating the snapshots but they...
Read more >
How To Write Snapshot Tests For React Components With Jest
In this tutorial, we will be looking at what snapshot tests are and how we ... likelihood that those changes may cause something...
Read more >
pretty-formats Documentation - Git
If the commit is a merge, and if the pretty-format is not oneline, email or raw, an additional line is inserted before the...
Read more >
pretty-format | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
User's Manual - CAMotics
It is possible to save a 3D model of the cut surface, the tool path, or a snapshot of the Simulation View or...
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