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.

Do not highlight passing asymmetric matchers in diffs

See original GitHub issue

🚀 Feature Proposal

This is another idea related to more helpful failure diffs in the same vein as #6170. The diff of a failing expectation currently highlights nested usages of expect.objectContaining and expect.anything because they are not “equal” to whatever was received. I propose only printing out a diff if the matcher fails.

Motivation

Both this issue and #6170 are the product of a pairing session where we had a hard time tracking down what the actual error was when an existing test started failing. I think that test could have been broken up into several more specific tests or used chained expectations, but it would have been nice to pinpoint the failing property right away.

Example

Here’s an example of a failing test that uses a combination of matchers:

https://repl.it/repls/DifficultSvelteMonitors image

The only property that doesn’t match is “rank” due to a type mismatch, but it’s hard to see that when scanning the failure diff because the nested matchers are displaying.

The desired approach would look like this: image

Pitch

Why does this feature belong in the Jest core platform?

This is a change to the default reporter that will help identify the cause of failures faster.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
grostocommented, Feb 9, 2019

This one sounds like fun. I can give it a try if nobody is actively working on it.

3reactions
markmsmithcommented, Jul 17, 2019

I can offer another use case in support of the output being more specific.
When using expect.objectContaining it can be really hard to see what’s changed when the structure is really large, such as when checking the headers of a Restify Request or Response object.
It would be much easier to see the mismatch if the output only printed the fields that you’re attempting to partially match on.
For example:

 expect(myFnSpy).toHaveBeenLastCalledWith(
  expect.objectContaining({
    env: 'test',
    req: expect.objectContaining({
      method: 'POST',
      headers: expect.objectContaining({
        'x-amzn-trace-id': expect.any(String),
        'x-forwarded-proto': 'https',
        'x-forwarded-for': 'some_client_ip'
      })
    })
  })
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

The hidden power of Jest matchers | by Boris - Medium
Putting everything together. Here I tried to highlight some use cases, where asymmetric matches may be useful. But this is not the full...
Read more >
Asymmetric vs Symmetric Encryption: Definitions & Differences
Authentication —Asymmetric encryption helps you to verify identities in a way that no one can fake or contest (this is known as non-repudiation) ......
Read more >
Extending Matchers | Guide - Vitest
This guide will explore extending matchers with expect.extend . ... automatically appear inside a diff when // the matcher does not pass, ...
Read more >
Jest 25: Laying foundations for the future · Jest
Jest 25 is laying the groundwork for many major changes in the future. ... Jest will no longer highlight passing asymmetric matchers in...
Read more >
Assessing the Theory and Practice of Criminal Sentencing
Not all sentencing variation is unwarranted; sentences properly reflect differences in the seriousness of the offense and/or the criminal history of the ...
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