question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.equals not available in custom matcher

See original GitHub issue

🐛 Bug Report

Depending on how a custom matcher is used, this.equals might not be defined:

To Reproduce

Steps to reproduce the behavior:

expect.extend({
  customMatcher(/* received, expected */) {
    const response = {
      pass: this.equals !== undefined,
      message: `this.equals is ${this.equals ? 'defined' : 'undefined'}`,
    };
    console.debug('response:', response);
    return response;
  },
});

test('called as expect(value).customMatcher()', async () => {
  expect('foo').customMatcher('foo');
});

test('called as expect.customMatcher()', async () => {
  expect({ custom: 'foo' }).toEqual({
    custom: expect.customMatcher('foo'),
  });
});
$ npx jest equals.test.js
 FAIL  ./equals.test.js
  ✓ called as expect(value).customMatcher() (8ms)
  ✕ called as expect.customMatcher() (7ms)

  ● called as expect.customMatcher()

    expect(received).toEqual(expected)

    Difference:

    - Expected
    + Received

      Object {
    -   "custom": customMatcher<foo>,
    +   "custom": "foo",
      }

      15 |
      16 | test('called as expect.customMatcher()', async () => {
    > 17 |   expect({ custom: 'foo' }).toEqual({
         |                             ^
      18 |     custom: expect.customMatcher('foo'),
      19 |   });
      20 | });

      at Object.toEqual (equals.test.js:17:29)

  console.debug equals.test.js:7
    response: { pass: true, message: 'this.equals is defined' }

  console.debug equals.test.js:7
    response: { pass: false, message: 'this.equals is undefined' }

Note that I added a console.debug() statement because the message from the custom matcher is not actually shown (as discussed in #7492).

Expected behavior

I’d expect this.equals to be defined in both invocations.

Run npx envinfo --preset jest

$ npx envinfo --preset jest
npx: installed 1 in 0.974s

  System:
    OS: Linux 5.0 Arch Linux
    CPU: (4) x64 Intel(R) Core(TM) i5-6360U CPU @ 2.00GHz
  Binaries:
    Node: 11.13.0 - /usr/bin/node
    Yarn: 1.15.2 - /usr/bin/yarn
    npm: 6.9.0 - /usr/bin/npm
  npmPackages:
    jest: ^24.7.1 => 24.7.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Oct 24, 2022

This was fixed in #11926 and #11930

0reactions
github-actions[bot]commented, Nov 24, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Matcher vs Object.equal? - Stack Overflow
equal (). I have a problem that Object. equal() solve easily while passing parameters in when() but some time I need to use...
Read more >
How To Improve Your Tests with Custom Matchers
Let's start with the actual test case before we go into the implementation. This should make it clearer what we are trying to...
Read more >
Equality matchers - RSpec Expectations 3.12 - Relish
RSpec.describe "a string" do it "is equal to another string of the same value" do expect("this string").to eq("this string") end it "is not...
Read more >
Practical Guide to Custom Jest Matchers - Redd Developer
There are two types of matchers in Jest: symmetric and asymmetric. ... In this assertion, the myObject must equal to the { id:...
Read more >
Expect - Jest
objectContaining(object); expect.not. ... There are a lot of different matcher functions, documented below, ... Custom Matchers API​.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found