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

feat: allow extending toEqual (fix #2875) #4880

Merged
merged 3 commits into from
Jan 12, 2024

Conversation

tigranmk
Copy link
Contributor

@tigranmk tigranmk commented Jan 5, 2024

Description

This PR solves issue #2875 .

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Jan 5, 2024

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 7018ef5
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/65a158629bef8c0008661393

@tigranmk tigranmk force-pushed the feat/allow-extending-toEqual branch from a69fae9 to 40fce2e Compare January 5, 2024 09:49
@sheremet-va
Copy link
Member

Is this a duplicate of #4586?

@tigranmk
Copy link
Contributor Author

tigranmk commented Jan 5, 2024

Is this a duplicate of #4586?

Seems, yes. I have not noticed it, as #4586 is not attached to #2875 .

I can see some differences between jest-asymmetric-matchers.ts and jest-asymmetric-matchers.ts files and not only.
I will leave comment in #4586

@tigranmk
Copy link
Contributor Author

tigranmk commented Jan 8, 2024

@sheremet-va As #4586 is closed, maybe you can review this one ?

docs/api/expect.md Outdated Show resolved Hide resolved
@tigranmk tigranmk force-pushed the feat/allow-extending-toEqual branch from ec0b4a9 to a0f502e Compare January 8, 2024 15:49
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏻

@sheremet-va sheremet-va added this to the 1.2.0 milestone Jan 8, 2024
@tigranmk tigranmk force-pushed the feat/allow-extending-toEqual branch from a0f502e to f423825 Compare January 8, 2024 16:14
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! I didn't look into the implementation, but a few wordings in documentation felt off, so I made some suggestions there.

docs/api/expect.md Outdated Show resolved Hide resolved
docs/api/expect.md Outdated Show resolved Hide resolved
docs/api/expect.md Outdated Show resolved Hide resolved
docs/api/expect.md Outdated Show resolved Hide resolved
docs/api/expect.md Outdated Show resolved Hide resolved
@tigranmk tigranmk force-pushed the feat/allow-extending-toEqual branch 4 times, most recently from 4e97ba4 to 2bd4de2 Compare January 10, 2024 13:24
@tigranmk tigranmk force-pushed the feat/allow-extending-toEqual branch 2 times, most recently from 338521c to 422d479 Compare January 10, 2024 15:06
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jest's spy related matchers (e.g. toHaveBeenCalledWith, toHaveReturnedWith) seem to also use custom testers for argument/return values comparison.
https://github.com/jestjs/jest/blob/2178fa2183cb7cb2ac3e262388499bafd032ef03/packages/expect/src/spyMatchers.ts#L64-L65
https://github.com/jestjs/jest/pull/13654/files#diff-9ce99fa9b58d394529495c6d13e759f0a3a6e892420327289ab50aa33f18a4a1

Probably you missed updating these parts of jestEquals?

def(['toHaveBeenCalledWith', 'toBeCalledWith'], function (...args) {
const spy = getSpy(this)
const spyName = spy.getMockName()
const pass = spy.mock.calls.some(callArg => jestEquals(callArg, args, [iterableEquality]))

packages/expect/src/jest-utils.ts Show resolved Hide resolved
@tigranmk
Copy link
Contributor Author

Probably you missed updating these parts of jestEquals?

Yes, thanks for noticing it, already updated.

Comment on lines 176 to 181
const arePersonsEqual = (a: unknown, b: unknown, customTesters: Array<Tester>) => {
const isAPerson = a instanceof Person
const isBPerson = b instanceof Person

if (isAPerson && isBPerson)
return a.name === b.name && equals(a.address, b.address, customTesters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests look good!

Just this one, I think using this.equals reflects API better than using equals export. Maybe you should rewrite it to function arePersonsEqual(this: TesterContext, a: unknown, ...) { ... }?

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

Successfully merging this pull request may close these issues.

None yet

3 participants