Jest: Snapshot mock

Created on 2 Nov 2017  Â·  9Comments  Â·  Source: facebook/jest

I just updated to [email protected] and found what appears to be wrong snapshotting for mocked functions:

    @@ -54,13 +54,22 @@
          />
        }
        refreshing={false}
        relay={
          Object {
    -       "hasMore": [Function],
    -       "loadMore": [Function],
    -       "refetchConnection": [Function],
    +       "hasMore": Object {
    +         "calls": Array [],
    +         "name": "jest.fn()",
    +       },
    +       "loadMore": Object {
    +         "calls": Array [],
    +         "name": "jest.fn()",
    +       },
    +       "refetchConnection": Object {
    +         "calls": Array [],
    +         "name": "jest.fn()",
    +       },
          }
        }

Is this expected (i.e. should I run with -u?)

Question

Most helpful comment

@SimenB yeah, I think it makes sense to change it from Object to "MockFunction { … }"?

All 9 comments

I think this was an intentional change. cc @pedrottimark

This is #4668. Did not think of this case... IMO it's fine :D

@SimenB You were thinking snapshot of mock function itself to see how it was called?

Thinking of possible objections in this context, the snapshot changes:

  • if you change from ordinary function to mock function, or the opposite
  • if you change when you take snapshot compared to when the mock function is called

if you change from ordinary function to mock function, or the opposite

This is fine. If you really want a stub, pass () => {}.

if you change when you take snapshot compared to when the mock function is called

Not as clear, but if I'm moving where the snapshot is taken, I'd not be too surprised at having to update the snapshots (and the invocations of that function does change, so, if anything, it's more accurate than just function). That said, I'm not sure what's the most ergonomic for users here

Yeah, I think it is fine to keep it.

OK; I have two things then:

  • This is probably a breaking change, so should release be jest@22?
  • Is it OK that an object with the same structure will match?

This is probably a breaking change, so should release be jest@22?

Current master has a few breaking changes (this should also be marked as one), so I think the next release will be 22 anyways.

Is it OK that an object with the same structure will match?

I'm inclined to say yes, but maybe we could somehow change the header from Object to (Jest)Spy?

@SimenB yeah, I think it makes sense to change it from Object to "MockFunction { … }"?

Love it. +1 to MockFunction. I created #4835 for that.

Was this page helpful?
0 / 5 - 0 ratings