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.

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:closed
  • Created 5 years ago
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
thymikeecommented, Aug 13, 2018

Nah, I like the current behavior and wouldn’t change it. It’s been like this forever and users didn’t mind.

1reaction
SimenBcommented, Aug 13, 2018

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

'something'.match(new RegExp('')) // returns [''], which is considered a match

@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.

Read more comments on GitHub >

github_iconTop 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 >

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