Throw on incorrect property matchers usage
See original GitHub issue🚀 Feature Proposal
I’d like to propose a new behavior to recently introduced “snapshot property matchers” – throwing when the object values are not asymmetric matchers.
We should also link to the guide from expect.toMatchSnapshot
docs to avoid confusion (regardless this request is accepted or not).
Motivation
I’ve seen this code in the wild:
expect(user).toMatchSnapshot({
createdAt: 'mocked-date',
id: 'gen-id',
});
Which doesn’t make much sense (maybe it is sometimes, but not really), because the snapshot file is going to have this very information anyway, resulting in duplicating code and reviewing effort. Such code can easily be replaced with toMatchObject
or toMatchSnapshot
without property matcher.
Example
We could throw something like:
Snapshot property matchers are intended to use with asymmetric matchers. For details on how to use it properly, see: https://jestjs.io/docs/en/snapshot-testing#property-matchers
Pitch
It’s an update to core matcher.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top GitHub Comments
I agree this is odd! I wonder if this makes more sense as an eslint rule?
FWIW the current behavior was what was proposed but I agree that it makes sense to only allow asymmetric matchers now that it’s better understood 👌