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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Extend CustomMatcher instead of just functions #14050

Closed
jhwang98 opened this issue Apr 3, 2023 · 7 comments
Closed

[Feature]: Extend CustomMatcher instead of just functions #14050

jhwang98 opened this issue Apr 3, 2023 · 7 comments

Comments

@jhwang98
Copy link
Contributor

jhwang98 commented Apr 3, 2023

馃殌 Feature Proposal

My goal is to create a better Buffer matcher. One that will show the diff by bytes and also diff by the encoded string.

    expect(received).bufferToBeEqualToBuffer(string)

    As buffers
    - Expected
    + Received

    @@ -4,11 +4,11 @@
          115,
          116,
          111,
          114,
          101,
    -     68,
    +     100,
          32,
          115,
          116,
          114,
          105,

    As strings
    - Expected
    + Received

    - +storeD string
    + +stored string

Unfortunately when I use it as an AsymmetricMatcher the format is generic.

Expected: bufferToBeEqualToBuffer<key-name>
Received: {"data": [43, 115, 116, 111, 114, 101, 100, 32, 115, 116, 鈥, "type": "Buffer"}

I investigated but it doesn't seem like I have control of this string. https://github.com/facebook/jest/blob/main/packages/expect/src/jestMatchersObject.ts#L105

I'm not sure if this is the best approach but allowing developers to specify their own AsymmetricMatcher subclass would give us more control.

Motivation

This will allow jest-extended to have nicer messages for their AsymmetricMatchers.

Of course my use case will be improved as well.

Example

No response

Pitch

It doesn't seem possible to get this functionality by expect.extend or other avenues.

@jhwang98
Copy link
Contributor Author

jhwang98 commented Apr 3, 2023

Loosely referenced in #4711 (comment)

@mrazauskas
Copy link
Contributor

I think this is good idea. It should be possible to make expect.extend(matchers) to handle instances of AsymmetricMatcher. The type of its argument could be: Record<string, RawMatcherFn | AsymmetricMatcher>.

Another approach could be adding something like expect.addAsymmetricMatchers(asymmetricMatchers). Not sure if that is good to have two separate methods which do almost the same.

Would you be interested to open a PR to see how actual implementation could look like?

@jhwang98
Copy link
Contributor Author

jhwang98 commented Apr 4, 2023

Yeah why not. My thoughts were to change expect.extend to accept AsymmetricMatchers.

Won't have too much time right now but I'll eventually get around to this.

(Anyone watching at home feel free to take this on)

@github-actions
Copy link

github-actions bot commented May 4, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label May 4, 2023
@github-actions
Copy link

github-actions bot commented Jun 3, 2023

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

1 similar comment
@github-actions
Copy link

github-actions bot commented Jun 3, 2023

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2023
@github-actions
Copy link

github-actions bot commented Jul 4, 2023

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 Jul 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants