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.

Warn about assertion name collisions between 2 libs extending jest

See original GitHub issue

🚀 Feature Proposal

Warn users when they have two matchers with colliding names. (and one is being overwritten silently)

Motivation

At the moment there is the case with jest-extended and testing-lib/dom. Both libraries have an assertion called toBeEmpty.

In jest-extended, it checks for emptiness in data, in testing-lib/dom, it checks for emptiness in the DOM.

https://github.com/testing-library/jest-dom#tobeempty https://github.com/jest-community/jest-extended#tobeempty

The situation is that as developers add both libraries in the project, one of the toBeEmpty will be silently overwritten, and when they start using them in their unit tests, and the test does not pass, regardless of being empty, they will not know what the problem is.

As there is no mechanism to decide which assertion names are already taken by libraries extending jest, an option to handle this, would be to warn users, as they install or run their tests, with 2 assertion names colliding, that the collision is happening, so that they can rename one of both in their projects

In my case I did by doing this as a very quick and dirty fix:

import {
  toBeEmpty as toBeEmptyNode,
} from '@testing-library/jest-dom/matchers';

expect.extend({ jestDomToBeEmpty });

Idea

I dont know the codebase in Jest to give more specific ideas, what I am thinking is, as they run their tests, to show a warning telling them that these 2 libs are using the same name for a matcher, and give them a link to find a solution to rename one in their configs.

For reference

I reported this in the author’s libs, before deciding how to address this, would be helpful to read your thoughts on it.

https://github.com/testing-library/jest-dom/issues/216

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
darekkaycommented, May 18, 2020

Yes, both issues are highly related, but one may handle them separately. While #6243 addresses possible solutions, this issue talks about adding a warning (which might still be a good idea even after #6243 is implemented).

1reaction
gnapsecommented, Mar 17, 2020

Related to (or duplicate of) #6243

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress causing type errors in jest assertions - Stack Overflow
I ran into this problem yesterday. It seems that what you are saying is correct, cypress and jest both declares types for expect...
Read more >
Configuring Jest
The file will be discovered automatically, if it is named ... You can retrieve Jest's defaults from jest-config to extend them if needed:....
Read more >
testing-library/jest-dom v5.16.5 - npm.io
You want to use jest to write tests that assert various things about the state ... written using matchers from @testing-library 's suite...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
unexpected - “the extensible BDD assertion toolkit”. # Asynchronous Code. By adding an argument (usually named done ) to it() to a test...
Read more >
rollup.js
exports , you should change the file extension to .cjs . You can also use other languages for your configuration files like TypeScript....
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