react-testing-library version: 5.0.0react version: 16.4.2node version: 8.11.3npm (or yarn) version: yarn 1.9.4const Component = props => {
if (props.throw) throw new Error('!!')
return <div>馃榾</div>
}
it('Throws an error when `props.throw === true`', () => {
const renderComponent = () => render(<Component throw />)
expect(renderComponent).toThrow('!!')
})
I'm making a jest assertion that a component will throw an error under certain circumstances.
While the test itself passes, the entire error shows up in the Jest output. Under other circumstances, jest suppresses errors that are thrown within a .toThrow() assertion. (there's a test in the repo that shows this)
https://github.com/good-idea/rtl-throws-error
Lots of unnecessary console output!
I haven't looked into how jest / RTL handles all of this and how it might be happening, but I'm happy to put together a PR. Just point me in the right direction!
This actually is not something that has anything to do with react-testing-library. It's how react and jsdom work. You can silence these logs using mock functions. That's what I do.
Oh, got it ~ I hadn't come across this until I started using react-testing-library so I thought it was a problem on that side.
Thanks for the quick response!
For anyone else coming here with the same problem, putting this in my setup file has it work exactly as I'd expect:
console.error = err => { throw new Error(err); };
console.warn = warning => { throw new Error(warning); };
This actually is not something that has anything to do with react-testing-library. It's how react and jsdom work. You can silence these logs using mock functions. That's what I do.
For everyone wondering: This is an easy way to do it: https://til.hashrocket.com/posts/hrhejhqg2n-turn-off-console-error-messages-in-a-test
Most helpful comment
For anyone else coming here with the same problem, putting this in my setup file has it work exactly as I'd expect: