How to get full test coverage using Jest and ReactJS, currently makes up 90.48% of applications, 58.06% of branches

With the simplest component, ReactJS Jest does not report full testing. How to get approval and branch to show 100%.

Jest currently shows 90.48% statements, 58.06% branch . Run using jest --coverage .

MyThing.js

 import React from 'react'; export default class MyThing extends React.Component { render() { return ( <div> Stuff </div> ); } } 

MyThing-test.js

 // __tests__/MyThing-test.js jest.unmock('../app/views/static/MyThing'); import React from 'react'; import ReactDOM from 'react-dom'; import TestUtils from 'react-addons-test-utils'; import MyThing from '../app/views/static/MyThing'; describe('MyThing', () => { const instance = TestUtils.renderIntoDocument( <MyThing /> ); it('gets rendered', () => { expect( TestUtils.isCompositeComponent(instance) ).toBeTruthy(); }); it('is not DOM component', () => { // checks if is a standard DOM element, ie <div> expect( TestUtils.isDOMComponent(instance) ).not.toEqual(true); }); it('isElementOfType is React element', () => { expect( TestUtils.isElementOfType(<MyThing />, MyThing) ).toEqual(true); }); it('render()', () => { const retVal = instance.render(); expect( retVal.type ).toEqual("div"); }); }); 
+6
source share
1 answer

This is most likely due to the passed code from babel. See issue # 817 for where this problem was handled and just resolved.

This must be fixed when upgrading to jest@15 .

Although some people mentioned the need to add source code to their .babelrc , for example:

 { "env": { "test": { "sourceMaps": "both" } } } 

and update your joke explicitly using "coverageCollector": "jest-babel-istanbul"

+1
source

All Articles