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

Not possible to write a custom matcher for undefined #11677

Closed
marchaos opened this issue Jul 19, 2021 · 3 comments
Closed

Not possible to write a custom matcher for undefined #11677

marchaos opened this issue Jul 19, 2021 · 3 comments

Comments

@marchaos
Copy link

馃悰 Bug Report

To Reproduce

When we define this custom matcher:

class CustomAnythingMatcher extends AsymmetricMatcher<any> {
    asymmetricMatch(other: unknown) {
        return true;
    }

    toString() {
        return 'Anything';
    }

    toAsymmetricMatcher() {
        return 'Anything';
    }
}
export const customAnything = (): CustomAnythingMatcher => new CustomAnythingMatcher();

and you attempt to use this like so

const fn = jest.fn();
fn(undefined);
expect(fn).toHaveBeenCalledWith(customAnything());

It doesn't pass. This is because when we get to JasmineUtils.eq(), where we are checking the expected vs actual arguments array, it passes in a hasKey implementation of hasDefinedKey. JasmineUtils.eq() invokes this block, which returns false in the case of above.

  if (keys(b, hasKey).length !== size) {
    return false;
  }

The main cause is the implementation of hasDefinedKey, which isn't really suitable for an arguments value list

function hasDefinedKey(obj, key) {
  return hasKey(obj, key) && obj[key] !== undefined;
}

Expected behavior

the internal jest code should delegate the equality check to the custom matcher.

@github-actions
Copy link

This issue is stale because it has been open for 1 year 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 Feb 17, 2023
@github-actions
Copy link

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 Mar 19, 2023
@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 Apr 27, 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