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

Snapshotting Arrays with Matchers #9079

Open
j opened this issue Oct 22, 2019 · 10 comments
Open

Snapshotting Arrays with Matchers #9079

j opened this issue Oct 22, 2019 · 10 comments

Comments

@j
Copy link

j commented Oct 22, 2019

馃悰 Bug Report

Creating a snapshot with array + matchers is not possible without wrapping array into an object.

To Reproduce

expect(['foo']).toMatchInlineSnapshot([expect.any(String)]);

Expected behavior

expect(['foo']).toMatchInlineSnapshot([expect.any(String)], `
  Array [
    Any<String>,
  ]
`);

Actual behavior

expect(['foo']).toMatchInlineSnapshot([expect.any(String)], `
  Object {
    "0": "foo",
  }
`);

I'm able to get by by taking my result and placing it in an object then doing the snapshot, but I feel like I should be able to snapshot an actual array with matchers.

@jeysal
Copy link
Contributor

jeysal commented Nov 6, 2019

cc @pedrottimark noticed something when dealing with propertyMatchers in #9049?

@falkenhawk
Copy link

Sadly, snapshot testing with property matchers is unusable with arrays at the moment.

@lewchuk-span
Copy link

This also tripped me up, and I've confirmed it is still an issue on 26.2.2. My use case involves nested objects in arrays. Specifically rows that come out of a testing database where there are columns that default to the current time (and are thus outside the control of mocking).

A simple way to replicate the error:

expect([
      { id: 1, createdAt: new Date() },
      { id: 2, createdAt: new Date() },
    ]).toMatchSnapshot([
      { createdAt: new Date() },
      { createdAt: new Date() },
    ]);

The snapshot I'd expect:

Object {
  "0": Object {
    "createdAt": Any<Date>,
    "id": 1,
  },
  "1": Object {
    "createdAt": Any<Date>,
    "id": 2,
  },
}

The snapshot I get:

Object {
  "0": Object {
    "createdAt": 2020-08-06T23:54:53.133Z,
    "id": 1,
  },
  "1": Object {
    "createdAt": 2020-08-06T23:54:53.133Z,
    "id": 2,
  },
}

The object wrapping approach @j suggested has worked:

expect({ data: [
    { id: 1, createdAt: new Date() },
    { id: 2, createdAt: new Date() },
  ]}).toMatchSnapshot({ data: [
    { createdAt: expect.any(Date) },
    { createdAt: expect.any(Date) },
  ]});

Produces a snapshot of:

Object {
  "data": Array [
    Object {
      "createdAt": Any<Date>,
      "id": 1,
    },
    Object {
      "createdAt": Any<Date>,
      "id": 2,
    },
  ],
}

@ktutnik

This comment has been minimized.

@rmclaughlin-nelnet
Copy link

Just ran into this issue, would love to see a fix.

@bodokaiser

This comment has been minimized.

1 similar comment
@chadmg

This comment has been minimized.

@dospunk
Copy link

dospunk commented Nov 3, 2022

Would love to see this feature implemented, just wanted to bump to keep the issue from getting stale

@voaharybeneva
Copy link

Any update?

@Bast1onCZ
Copy link

Any updates on this?

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

No branches or pull requests