Search code examples
reactjstypescriptcode-coverageenzymeistanbul

React, Enzyme and Istanbul - code coverage missing functions being executed by tests


I'm testing a React component with 3 functions, I've written tests that use all 3 of these functions and the test pass however in the code coverage report I'm only getting 33%. The component is below.

const AddWidget = ({ }: Props) => {

    var id = generateGuid().slice(0, 8);

    var showWidget = () => {
        document.getElementById(id).style.zIndex = "10";
    }

    var hideWidget = () => {
        document.getElementById(id).style.zIndex = "200";
    }

    return (
        <div onMouseLeave={hideWidget} onMouseEnter={showWidget} className="addWidget" >
            <div className="divide" />
            <div id={id} className="cover" />
            <div>
                <CircularButton type={CircularButtonType.DarkAdd} small={true} />
                <p>Add a widget here</p>
            </div>
        </div>
    );
}

export default AddWidget;

And my tests...

import * as React from 'react';
import * as Enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
import AddWidget from './AddWidget';

Enzyme.configure({ adapter: new Adapter() });

const addWidget = Enzyme.mount(<AddWidget />, { attachTo: document.body });

describe('AddWidget', () => {
    test('renders without crashing', () => {
        expect(addWidget.find(AddWidget).length).toBe(1);
    });

    test('should render parent element with class "addWidget"', () => {
        expect(addWidget.find('div').at(0).hasClass('addWidget')).toBe(true);
    });

    test('should cover component from view when mouse is not hovering', () => {
        addWidget.simulate('mouseEnter');
        addWidget.simulate('mouseLeave');
        var covers = document.getElementsByClassName('cover');
        for (var n = 0; n < covers.length; n++) {
            expect((covers[n] as HTMLElement).style.zIndex).toBe("200");
        }
    });

    test('should show component from view onMouseEnter', () => {
        addWidget.simulate('mouseEnter');
        var covers = document.getElementsByClassName('cover');
        for (var n = 0; n < covers.length; n++) {
            expect((covers[n] as HTMLElement).style.zIndex).toBe("10");
        }
    });
});

The tests specify it's the showWidget and hideWidget functions that aren't being tested but the last 2 tests definitely run these functions otherwise the tests wouldn't pass.

Is this a code coverage bug? Is it that it doesn't like that I'm using pure Javascript functions or am I fundamentally misunderstanding function code coverage?

EDIT: coverage report images below

Breakdown

Details


Solution

  • I found what the issue was, I was running the tests with the command react-scripts test --coverage --coverageDirectory=output/coverage --coverageReporters text --env=jsdom which was updating the cobertura.xml file but not any of the html. I thought the html read the coberatura file and displayed it but that's not the case. Adding the html flag to coverageReporters fixed the issue.