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.

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.

cc @rickhanlonii

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mattphillipscommented, Oct 9, 2018

I agree this is odd! I wonder if this makes more sense as an eslint rule?

1reaction
rickhanloniicommented, Oct 11, 2018

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 👌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reusing property matchers causes incorrect matching failure
Bug Report Reusing the same objects with property matchers results in unsuccessful validation of the object towards it.
Read more >
Mockito: InvalidUseOfMatchersException - java
This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all ...
Read more >
Using Matchers - Jest
Jest uses "matchers" to let you test values in different ways. ... throw new Error('you are using the wrong JDK!');
Read more >
4. Matchers in Depth - JavaScript Testing with Jasmine [Book]
Perhaps the simplest matcher in Jasmine is toEqual . It simply checks if two things are equal (and not necessarily the same exact...
Read more >
Argument matchers - NSubstitute
Returns(TestWidget); /* ACT */ // NOT OK: arg matcher used with a real call: // subject.StartWithWidget(Arg.Any<WidgetInfo>()); // Use a real argument instead: ...
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