Invalid toThrow verification
See original GitHub issue🐛 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:19 (5 by maintainers)
Top Results From Across the Web
Invalid toThrow verification #6805 - facebook/jest - GitHub
toThrow() fails to check correctly against an empty string or a string with one symbol. ... Invalid toThrow verification #6805.
Read more >Jest: .toThrow matcher does not pass test on Error
I want to test invalid Inputs of a function and expect the function to throw on that inputs. However the test does not...
Read more >How to throw an error when entering wrong date Year in aura ...
All you need to do to check if the date is invalid is to check if you can construct it ( isNaN returns...
Read more >testing for thrown errors with jasmine - Google Groups
Hi, Is there a matcher in the works that will test for thrown errors? I have a few objects that when constructed with...
Read more >API Reference | Vitest
Type: (name: string, fn: TestFunction, timeout?: number) => void ... invalid number', () => { test('composed of non-numbers to throw error', ...
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 FreeTop 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
Top GitHub Comments
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.