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

Can only call expect.extend(..) once as it overwrites the proxy's custom extend #72

Open
sb3700 opened this issue Jan 30, 2024 · 1 comment · May be fixed by #73
Open

Can only call expect.extend(..) once as it overwrites the proxy's custom extend #72

sb3700 opened this issue Jan 30, 2024 · 1 comment · May be fixed by #73

Comments

@sb3700
Copy link

sb3700 commented Jan 30, 2024

Bug

  • package version: v1.1.3
  • node version: 14.21.3
  • npm (or yarn) version: 6.14.18
  • jest version: 26.6.2

Relevant code or config

expect.extend({
  toBe1(received) {
    const pass = received === 1;
    return pass
      ? { pass, message: () => `expected ${received} to be 1` }
      : { pass, message: () => `expected ${received} not to be 1` };
  }
});

expect.extend({
  toBe2(received) {
    const pass = received === 2;
    return pass
      ? { pass, message: () => `expected ${received} to be 2` }
      : { pass, message: () => `expected ${received} not to be 2` };
  }
});

console.log(expect['toBe1'], expect['toBe2']);
// [Function (anonymous)] undefined

What happened (please provide anything you think will help):
Thanks for the library!

The problem is at https://github.com/mattphillips/jest-expect-message/blob/main/src/withMessage.js#L92.

expectProxy = Object.assign(expectProxy, expect); overwrites expectProxy.extend with expect.extend.

It could be fixed by changing the line to:

for (const key of expect) {
  if (key !== 'extend') {    // or !(key in eventProxy)
    expectProxy[key] = expect[key];
  }
}
@seggewiss
Copy link

seggewiss commented Jul 17, 2024

Actually expect is not iterable but this would work:

expectProxy.extend = o => {
    expect.extend(o); // add new matchers to expect

    Object.keys(expect).forEach((key) => {
        if (key !== 'extend') {    // or !(key in eventProxy)
            expectProxy[key] = expect[key];
        }
    })
};

I used patch packages locally but will open a MR here hopefully it will be merged 😊

@seggewiss seggewiss linked a pull request Jul 17, 2024 that will close this issue
3 tasks
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 a pull request may close this issue.

2 participants