-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ErrorBoundry reconfigured Jest Testing
- Setup an error boundry component based on [React Documentation](https://reactjs.org/docs/error-boundaries.html) - Renamed `.babelrc` to `.babel.config.js` based on [GitHub issues](jestjs/jest#6053 (comment)) - Added `babel-plugin-dynamic-import-node` because of Jest errors regarding `import` syntax - Added `react-dom` to devDependencies as it's used in `@testing-library/jest-dom`
- Loading branch information
1 parent
d8070dd
commit 8b497d6
Showing
8 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// The babel file needs to be babel.config.js | ||
// https://github.com/facebook/jest/issues/6053#issuecomment-383632515 | ||
module.exports = { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
env: { | ||
test: { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
plugins: [ | ||
'@babel/plugin-proposal-class-properties', | ||
'transform-es2015-modules-commonjs', | ||
'babel-plugin-dynamic-import-node', | ||
], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render } from '@testing-library/react'; | ||
import ErrorBoundry from '../_helpers/Error'; | ||
|
||
const TestComponent = () => { | ||
return this.methodDoesntExist.value; | ||
}; | ||
|
||
describe('Error', () => { | ||
beforeEach(() => { | ||
jest.spyOn(console, 'error'); | ||
console.error.mockImplementation(() => {}); | ||
}); | ||
it('renders that there was a problem', () => { | ||
const { container } = render( | ||
<ErrorBoundry> | ||
<TestComponent /> | ||
</ErrorBoundry>, | ||
); | ||
|
||
expect(container).toHaveTextContent('Something went wrong.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
class ErrorBoundry extends Component { | ||
state = { hasError: false }; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
// TODO: hookup to error service | ||
console.error(error, errorInfo); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
const { hasError } = this.state; | ||
if (hasError) { | ||
return <h1>Something went wrong.</h1>; | ||
} | ||
|
||
return children; | ||
} | ||
} | ||
|
||
export default ErrorBoundry; | ||
|
||
ErrorBoundry.propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.node, | ||
]), | ||
}; |