Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest tests failing only with coverage

I have the issue that if I run my test without the --coverage option everything passes, but as soon as I run jest with the --coverage option some of my test fail.

Here is the log:

Button › Primary button › should render a primary button

  undefined:3:457: property missing ':'

  at Object.it (src/components/Buttons/Button/Button.test.js:13:24)
  at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)

And the test:

it('should render a primary button', () => {
  const props = { primary: true };

  const rendered = renderer.create(<Button { ...props }>Some Button</Button>).toJSON();
  expect(rendered).toMatchSnapshot();
});

The failing line is expect(rendered).toMatchSnapshot();

Anyone any ideas?

like image 403
Jonathan Avatar asked Sep 26 '17 18:09

Jonathan


People also ask

How do I ignore test coverage in Jest?

To ignore a file pattern for Jest code coverage, we can add the option in the Jest config. to calculate the code coverage from the paths that match patterns listed in collectCoverageFrom that don't have the exclamation mark before it.

What does coverage do in Jest?

Code coverage makes possible to spot untested paths in our code. It is an important metric for determining the health of a project. I've seen enough misconfigured JavaScript projects where Jest coverage were reported incorrectly.

Does Jest have code coverage?

Jest, a flexible, easy-to-use testing framework, will be used to test a simple Typescript application. Codecov, a tool for monitoring code coverage, will be used to measure the test coverage for your Jest unit tests.


1 Answers

I fixed this issue. But I think it was a quite specific one.

We are using styled components and I did something like this:

const BaseButton = styled.button`
  ${ noTouchDevice && `
    :hover {
      cursor: ${ ({ disabled }) => (disabled ? 'not-allowed' : 'initial') };
    }
  ` }
`;

Which is not working since the nested template literal is not handled by styled-component like the outer one. Which means if styled-components finds a function in the outer string literal it invokes it and passes the component props as parameters. But a nested template literal will not be handled like this.

This is why in the snapshots I got something like this:

.c0:hover {
  cursor: ({ disabled }) => disabled ? 'not-allowed' :'initial';
}

instead of

.c0:hover {
  cursor: not-allowed;
}

Just running the tests worked fine, but with coverage collection it failed there comparing the snapshots.

So I simply changed my code to just using the ternary operator like

const BaseButton = styled.button`
  ${ noTouchDevice && `
    :hover {
      cursor: ${ disabled ? 'not-allowed' : 'initial' };
    }
  ` }
`;

and it worked fine.

What I still do not know, is why the error only occurred when collection the coverage. To be honest I did not wanted to invest more time yet but I assume it has to do with how the test are invoked when jest/istanbul is collection the coverage.

like image 170
Jonathan Avatar answered Sep 19 '22 22:09

Jonathan