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:
- Created 4 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This was fixed in #11926 and #11930
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.