jest: Invalid toThrow verification
🐛 Bug Report
.toThrow()
fails to check correctly against an empty string or a string with one symbol.
To Reproduce
it('must fail when compared against an empty string', () => {
expect(() => {
throw 'something';
}).toThrow(''); // PASS
});
it('must fail against a string with one letter', () => {
expect(() => {
throw 'something';
}).toThrow('a'); // PASS
});
Expected behavior
The two cases above are supposed to fail. But instead, they both succeed.
Curiously, if in the second case we use more than one letter, only then the test fails.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (10 by maintainers)
Commits related to this issue
- issue #6805 hotfix — committed to diogomotapinto/jest by diogomotapinto 6 years ago
- issue #6805 hotfix — committed to diogomotapinto/jest by diogomotapinto 6 years ago
- issue #6805 hotfix — committed to diogomotapinto/jest by diogomotapinto 6 years ago
Nah, I like the current behavior and wouldn’t change it. It’s been like this forever and users didn’t mind.
The first one passes since we convert it to a regex (empty regexp is the same as
/(?:)/
, so it matches anything)https://github.com/facebook/jest/blob/c9893d2f1cdbc98655ca446d2a49f8c77a42b4be/packages/expect/src/to_throw_matchers.js#L53-L55
@thymikee @rickhanlonii should we drop the regexp conversion for empty strings? My guess the current behaviour exists to allow substring matches, but using
.includes
won’t help for empty strings.