Reputation: 6379
When snapshot testing a component I want to only test the component itself, not the actions, state or the connecting function that is in Redux. This is because I have tests for these functions in other places.
This messes up your code coverage because it expects that you test all functions.
For example:
export const EarningsInfo = ({ close }) => ( /* ... */ );
const mapStateToProps = _ => ({});
const mapActionsToProps = dispatch => ({
close: _ => dispatch(closeModal()),
});
export default connect(mapStateToProps, mapActionsToProps)(EarningsInfo);
Jest is expecting that you test
earningsInfo
mapStateToProps
mapActionsToProps
connect
So if you have a simple test like this:
import { EarningsInfo } from '../components/EarningsInfo';
it('renders correctly', () => {
const tree = renderer.create(
<EarningsInfo close={ () => null } />
).toJSON();
expect(tree).toMatchSnapshot();
});
Code coverage reports that you are only testing 25% of the file. I am sure this is working as intended.
My question is two fold
mapStateToProps
, mapActionsToProps
and connect
shouldn't need to be tested.Upvotes: 2
Views: 1171
Reputation: 2021
connect
should not be tested as react-redux
's own tests already cover this. If mapStateToProps
or mapActionsToProps
have any non-standard / complicated / business logic then it may make sense to test them.
Yes, but it can be a bit fragile - http://blog.dmbcllc.com/es2015-code-coverage-and-jest-react-js-unit-testing/
Don't worry about 100% code coverage, focus on what is critical AND can be tested well/easily with unit testing.
Upvotes: 1