-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enzyme 3 couldn't test component when ComponentDidCatch happend #1255
Comments
I'm having similar issues. Please see https://twitter.com/nickytonline/status/924697867750699010. Basically this is my HOC, https://gist.github.com/nickytonline/c758bcbc79e3ca435310bb847fae2c88 and here is the skipped test that I can't get working, https://gist.github.com/nickytonline/637580f12f14595f7954ecdaf5c8cd88 |
I've also tried my errored component as a class instead of an SFC,
but it results in the same thing. |
Any suggested best practice on this? Has anyone found a solution they're happy with? I'm working on a similar problem. |
You can test import Login from './index';
import expect from 'expect';
import React from 'react';
import { mount } from 'utils/enzyme';
function ProblemChild() {
throw new Error('Error thrown from problem child');
return <div>Error</div>; // eslint-disable-line
}
describe('<Login />', () => {
it('should catch errors with componentDidCatch', () => {
const spy = expect.spyOn(Login.prototype, 'componentDidCatch');
mount(<Login><ProblemChild /></Login>);
expect(Login.prototype.componentDidCatch).toHaveBeenCalled();
});
}); |
@realseanp Any idea how to remove the annoying console logging? I was able to mute react's by mocking |
Possibly related issue on Jasmine: jasmine/jasmine#1453 IMO Jasmine should probably be agnostic of the infrastructure to properly catch errors thrown by React components. Enzyme seems like the more natural place to solve this. |
@realseanp Nice. I did the same with jest.spyOn() and that got my coverage up to 100%. Thank you! |
This workaround only tests to make sure that @ljharb is there a milestone/checklist anywhere of react features not yet supported? |
@brucewpaul filed #1553 to track everything. |
@QuentinRoy I had the same issue and was able to mute jsdom's console by doing: |
@jessicarobins That's a good tip! For others reading this, if you want to mute all error output, silence |
I'd argue that muting errors isn't necessarily the best course of action, error messages are there for a reason, no? |
@lesbaa Yes in general I entirely agree. Though in this case this is a message from React warning about the exact behaviour being under scrutiny. So it is really just confusing noise in the test output. I wish react gave a way to disable this warning so that we do not have to mute all output, but it does not seem to be a way to do that yet. |
Following up on @pizza-r0b's technique, as mentioned here:
Instead, I've found that the following technique appears to work with Spoiler: the technique involves calling Enzyme's With
|
@kohlmannj thanks , but when i use
code is here.
|
When using #1797 will add proper support for this. |
🙇 thank you! |
My Component:
My test:
When
app
is mount,app
throws the error and I couldn't get theapp
to test the error view. How do I solve it?The text was updated successfully, but these errors were encountered: