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.

Chainable asymmetric matchers

See original GitHub issue

Do you want to request a feature or report a bug?

feature

What is the current behavior?

expect({ foo: new Error("failure") }).toEqual({
  foo: expect.any(Error).objectContaining({ message: "failure" }),
})

outputs

    TypeError: expect.any(...).objectContaining is not a function

      548 |           foo: new Error("failure"),
      549 |         }).toEqual({
    > 550 |           foo: expect.any(Error).objectContaining({ message: "failure" }),
      551 |         })
      552 |       })
      553 | 

What is the expected behavior?

The syntax above should not throw.

The following test should throw a descriptive error message:

expect({ foo: new Error("failure") }).toEqual({
  foo: expect.any(Error).objectContaining({ message: "success" }),
})

Please provide your exact Jest configuration

{
  "testMatch": [
    "<rootDir>/imports/**/*.test.js"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/",
    "/.meteor"
  ],
  "setupTestFrameworkScriptFile": "./imports/test-helpers/setup.js",
  "coverageDirectory": "imports/coverage"
}

Run npx envinfo --preset jest in your project directory and paste the results here

  System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  Binaries:
    Node: 8.9.1
    Yarn: 1.5.1
    npm: 5.5.1
  npmPackages:
    jest:
      wanted: ^22.4.2
      installed: 22.4.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
aymericbouzycommented, Mar 29, 2018

A few things I would like to confirm before coding:

Implementation

In order to have a nice implementation, I suggest:

1/ creating a “combine” operation for asymmetric matchers:

expect("Hello World").toEqual(combinedAssertions([
	expect.stringContaining("Hello"),
	expect.stringContaining("World!"),
]))

2/ adding the chaining API itself

Descriptive error message

I imagined printing the first failing assertion : what do you think?

3reactions
cpojercommented, Mar 29, 2018

That’s a separate conversation entirely. I don’t think it provides a lot of value for the added complexity and it can make assertions harder to read rather than easier.

The nice thing about the extension to asymmetric matchers here is that it allows to create a richer description of what you’d like to match. However, I think we need to be careful if we add this to Jest: expect.arrayContaining(…).stringMatching() and similar combinations do not make any sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Practical Guide to Custom Jest Matchers - Redd Developer
There are two types of matchers in Jest: symmetric and asymmetric. ... But it's the .not. chain that makes this assertion inverted, ...
Read more >
Chain Jest Matchers together To Create one Powerful Assertion
Chain core and custom matchers together. ... Note: jest-chain does not currently support asymmetric matcher chaining, if you want this please send a...
Read more >
Your first suite - Jasmine Documentation
Any matcher can evaluate to a negative assertion by chaining the call to expect with a not ... Jasmine provides a number of...
Read more >
The hidden power of Jest matchers | by Boris - Medium
Basically, it means that if the right-side object has an asymmetricMatch method, the result if its call will be used instead of an...
Read more >
2D Side‐Chain Engineered Asymmetric Acceptors ...
Abstract The charge transport and morphology of active layers are key considerations for device performance and stability in organic solar ...
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