score:0
Adding to @Andreas Köberle's comment since the hasError
state changes on ComponentDidCatch
lifecycle method, you could also use enzymes setState.
You also don't need to mount
the comment, shallow would do
.
it('displays error message on error generated by child', () => {
const wrapper = shallow(
<ErrorBoundary >
<ComponentWithError />
</ErrorBoundary>
);
wrapper.setState({ hasError: true });
wrapper.toMatchSnapshot()
});
score:8
After additional research I found that it is an open issue that has to be solved by Enzyme. https://github.com/airbnb/enzyme/issues/1255
I have implemented it as follows:
function ProblemChild() {
throw new Error('Error thrown from problem child');
return <div>Error</div>; // eslint-disable-line
}
describe('<ErrorBoundary> window',()=> {
it('displays error message on error generated by child', () => {
const spy = sinon.spy(ErrorBoundary.prototype, 'componentDidCatch')
mount(<ErrorBoundary><ProblemChild /></ErrorBoundary>)
chaiExpect(ErrorBoundary.prototype.componentDidCatch).to.have.property('callCount', 1)
})
})
Proposed workaround works anyhow
- it is still not possible to test error message rendered to the app user by
<ErrorBoundary>
test console displays warnings:
PASS src/ErrorBoundary.test.js
● Console
console.error node_modules/react-dom/cjs/react-dom.development.js:9627 The above error occurred in the <ProblemChild> component: in ProblemChild (at ErrorBoundary.test.js:37) in ErrorBoundry (created by WrapperComponent) in WrapperComponent React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundry.
score:20
Enzyme has simulateError
helper now.
So this works very well for me:
const Something = () => null;
describe('ErrorBoundary', () => {
it('should display an ErrorMessage if wrapped component throws', () => {
const wrapper = mount(
<ErrorBoundary>
<Something />
</ErrorBoundary>
);
const error = new Error('test');
wrapper.find(Something).simulateError(error);
/* The rest fo your test */
}
}
Source: stackoverflow.com
Related Query
- How to write test case for ErrorBoundary in React using Jest / Enzyme
- How to write a test case for a simple React component using Jest and Enzyme
- How to write test case by using jest for Increment using React Hooks
- How to write test case coverage in Jest and Enzyme for indexOf used inside React Component
- How to write test case using Jest for Yup.isValid function?
- How to write Test cases for useEffect Hook in React using Jest & Enzyme?
- How do I write state case for the state in react useState using Jest
- How to write test cases for a function using jest and enzyme
- How to write test cases in enzyme and jest for a component using useDropZone?
- write unit test for event handlers that updates some state in react using jest and enzyme
- How to write test case for a component which is calling a method using jest
- How to cover test cases for Svg react component using Jest + Enzyme
- How do I write test case for a function which uses uuid using jest?
- How to write test cases for tabs in material-ui using react testing library
- how to test OnSubmit in React using Jest and Enzyme
- How to test onChange for ui-material TextField using Jest and Enzyme
- How can I check a function for having arguments without checking their values, using jest & enzyme in react component?
- How to write test case for url push in useEffect react hook?
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How to test a method which is in mapStateToProps in React using Jest - Enzyme
- How to write test cases using React Testing Library for useMutation?
- How to test the style inside the CSS class for React component using Jest and Enzyme?
- How to test if a prop is rendered correctly in a Component using Jest and Enzyme in React
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- How to write a Jest-enzyme test case for code in React useEffect whose dependencies are updated in its child component
- How to write unit test case using jest in react?
- How to stub async action in componentDidMount for react redux connected component test using Jest
- How do you test for the non-existence of an element using jest and react-testing-library?
- How to test style for a React component attribute with Enzyme
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
More Query from same tag
- Find all hardcoded strings in React/Node project
- React Redux reducers
- Using a async function in createConnection of promise-mysql does change anything at loading speed?
- style tag completely disappears from ReactJS component on render
- Call react component with string not by its name
- How to see axios error response JSON in React
- coding reactjs with the build in of html
- Proper way to pop from a react component's array-type state attribute?
- How to use <Link /> component inside dangerouslySetInnerHTML
- Adjusted proxy settings in Create-React-App to get around CORS blocking error and now get an undefined response
- popover in jquery needs to be converted to javascript for create react app
- How to remove data:image/png;base64 in base64 before uploading to api server through axios post in ReactJs
- imported action not available in container
- React Router and Switch
- How to get useEffect to only update on changes when the change is from another component
- Hide element on Reactjs
- How can i achieve 100% reusability of that react-bootstrap component?
- How to show a message and delete it each time the route changes
- Getting React Developer Tools to Work with Webpack
- What is the correct way of adding a dependency to react in your package.json for a react component
- how to redirect to homepage after successful login in ReactJS?
- Retrieve data from Firebase with javascript
- How do I get messages to appear right away? It works for new conversations (first block), but not for the existing conversations (second block)
- React click on item to show details
- Merge nested array with unique element in Javascript
- Accessing 'this' context in iffe
- express.json() doens't work on production
- React @material-ui/core - v4.11.0 - "the createMuiTheme function was renamed to createTheme" console error
- React-Bootstrap - importing modules
- React how to make an input field with a fixed value combined with a dynamic value