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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messages from custom AssymetricMatchers are ignored #7492

Closed
jayarjo opened this issue Dec 10, 2018 · 7 comments
Closed

Messages from custom AssymetricMatchers are ignored #7492

jayarjo opened this issue Dec 10, 2018 · 7 comments
Labels

Comments

@jayarjo
Copy link

jayarjo commented Dec 10, 2018

🐛 Bug Report

Messages from custom AssymetricMatchers are ignored, while according to the following illustration, they were meant to be taken into account.

image

To Reproduce

Simply try to run the example from illustration, but force it to fail (the one above succeeds so no error messages have to be shown):

expect.extend({
    toBeDivisibleBy(actual, expected) {
        const pass = actual % expected === 0;
        const message = pass
            ? () => `expected ${actual} not to be divisible by ${expected}`
            : () => `expected ${actual} to be divisible by ${expected}`

        return { message, pass }
    }
})

test("example asymetric matchers", () => {
    expect({ value: 3 }).toEqual({ value: expect.toBeDivisibleBy(2) })
})

Expected behavior

As illustration shows, one would expect a proper error message to be displayed.

Run npx envinfo --preset jest

Paste the results here:

$ npx envinfo --preset jest
npx: installed 1 in 2.228s

  System:
    OS: Windows 10
    CPU: (4) x64 Intel(R) Core(TM) i7-6560U CPU @ 2.20GHz
  Binaries:
    Node: 10.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.9.4 - ...\YARN\BIN\yarn.CMD
    npm: 6.4.1 - ...\nodejs\npm.CMD
@jayarjo jayarjo changed the title Messages from custom AssymetricMatchers are ignored Messages from custom AssymetricMatchers are ignored Dec 10, 2018
@rickhanlonii
Copy link
Member

@jayarjo thanks to submitting

I ran the above code and I see the expected failure:

screenshot 2018-12-10 21 45 10

@jayarjo
Copy link
Author

jayarjo commented Dec 11, 2018

Is this expected? Looks more like generic default. What about expected ${actual} not to be divisible by ${expected} part?

In my case AssymetricMatcher is matching two URLs and I was looking for a way to output detailed information about what parts of the URLs actually differ. Query string especially, since the order of the fields can be arbitrary and also I wanted to have a partial matcher (when one query string contains another, but is not exactly equivalent).

@rickhanlonii
Copy link
Member

rickhanlonii commented Dec 11, 2018

Yeah this is expected, the matcher is toEqual so we show it's error message

Here's what toBeDivisible outputs when called as a matcher:

screenshot 2018-12-11 09 48 46

In the original case, it's being called as a asymmetric matcher so we only use the pass/fail indicator when comparing equality and don't use the message

@jayarjo
Copy link
Author

jayarjo commented Dec 13, 2018

:/

But won't it be a great feature, if nested asymmetric matchers would be able to show full messages? In the case above matcher is trivial, so it's self-explanatory even with generic message what exactly went wrong. Potentially they might be (and actually are more useful if they are) much more complex than that. Take objectContaining for example - at the moment it is not able to show exactly what props didn't match, it just spits out whole stringified object and then one has to look up the diff in the wrapped lines (sometimes huge) manually.

@rickhanlonii
Copy link
Member

Yes, we could definitely improve here, just closed this because it's not a bug

Would you be interested in submitting a feature proposal with a suggestion on how we could improve common use cases?

@SimenB
Copy link
Member

SimenB commented Dec 13, 2018

Heh, sorry about the churn...

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants