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

description field for Custom Matcher should be optional #103

Open
blzsaa opened this issue Sep 27, 2022 · 2 comments
Open

description field for Custom Matcher should be optional #103

blzsaa opened this issue Sep 27, 2022 · 2 comments

Comments

@blzsaa
Copy link

blzsaa commented Sep 27, 2022

The custom matcher part in read me would throw a compile error if used.

  • According to readme we can create a custom matcher with the following code:
import { MatcherCreator, Matcher } from 'jest-mock-extended';

// expectedValue is optional
export const myMatcher: MatcherCreator<MyType> = (expectedValue) => new Matcher((actualValue) => {
    return (expectedValue === actualValue && actualValue.isSpecial);
});
  • while currently cunstuctor of Matcher requires string description field as well:
    • constructor(readonly asymmetricMatch: MatcherFn, private readonly description: string)
@marchaos
Copy link
Owner

Hmm, when the matcher fails, surely the error is indecipherable without a description though?

@blzsaa
Copy link
Author

blzsaa commented Sep 28, 2022

I am not sure if I understand the question correctly but according to my understanding:

  • matchers are only used by calledWith which defines behavior and not asserts them, therefore the matchers cannot really fail.
  • if no matcher found the mock will return the default behavior a.k.a undefined and not some kind of message with the description of that matcher.

E.g. the following code on both the current master branch and on my PR will print out the default jest failure message for toEqual without mentioning anything about description of the matcher.

const stringMatcher = new Matcher((actualValue) => {
    return (actualValue === "value");
}, "description");
const provider = mock<PartyProvider>();
provider.getSongs.calledWith(stringMatcher).mockReturnValue(['Dance the night away', 'Stayin Alive']);
expect(provider.getSongs('disco party')).toEqual(['Dance the night away', 'Stayin Alive']);

will print out:

Error: expect(received).toEqual(expected) // deep equality

Expected: ["Dance the night away", "Stayin Alive"]
Received: undefined

p.s.
I hope I did not misunderstand the question or the production code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants