Skip to content

Snapshots for strings are sometimes being transformed into objects/arrays #8059

@morungos

Description

@morungos

🐛 Bug Report

There's a strange and subtle change in the way snapshots are handled, showing some counter-intuitive displays when there is a difference. The issue manifests with string values in .toMatchSnapshot() when a snapshot name is passed as an argument.

It feels as if strings, are sometimes being incorrectly turned into arrays. The issue was not observed in Jest 23.

To Reproduce

Use the following test

describe('basic', () => {
  it('should match', () => {
    expect("Wibble wibble").toMatchSnapshot({}, "base");
  });
});

Note that without the additional arguments to .toMatchSnapshot(), this breaking behaviour is not seen. Using .toMatchSnapshot("base") (to specify a name) also works. However, the docs (https://jestjs.io/docs/en/expect#tomatchsnapshotpropertymatchers-snapshotname) do not imply that the propertyMatchers argument is optional.

Expected behavior

The stored snapshot would be the string "Wibble wibble". Instead, it is stored -- and matched -- as an object like this:

exports[`basic should match: base 1`] = `
Object {
  "0": "W",
  "1": "i",
  "10": "b",
  "11": "l",
  "12": "e",
  "2": "b",
  "3": "b",
  "4": "l",
  "5": "e",
  "6": " ",
  "7": "w",
  "8": "i",
  "9": "b",
}
`; 

Note that during testing, when there is a snapshot mismatch, even in the string, the reverse happens. A simple string like "Wobble wobble" shows as an object, with characters out of order. This is applied to the received value, making debugging with longer strings very hard.

Link to repl or repo (highly encouraged)

The file above is enough, in a Jest 24.1.0 environment.

The workaround of .toMatchSnapshot("base") is enough, and maybe this is better handled as documentation fix, but there's definitely been some change since Jest 23 in this part of the code base.

Run npx envinfo --preset jest

  System:
    OS: macOS 10.14.3
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Binaries:
    Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
    Yarn: 1.2.1 - /usr/local/bin/yarn
    npm: 6.8.0 - ~/.nvm/versions/node/v8.10.0/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0 

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions