jest: Coverage flag breaks snapshot test

I think it’s because of code instrumentation

jest --coverage
    - Snapshot
    + Received
...
    -   onSelect={[Function onSelectHandler]}
    +   onSelect={[Function anonymous]}

Running with jest is just OK.

Source code and func name for onSelectHandler: jest:

    console.log src/components/pie-chart.js:44
      [Function: onSelectHandler]
    console.log src/components/pie-chart.js:45
      function onSelectHandler(_ref2){var nativeEvent=_ref2.nativeEvent;

      if(!onSelect){
      return;
      }
      onSelect(nativeEvent.index);
      }

And for jest --coverage:

    console.log src/components/pie-chart.js:44
      [Function]
    console.log src/components/pie-chart.js:45
      function (_ref2){var nativeEvent=_ref2.nativeEvent;++__cov_742n7agVtRgakP6RGrGnDGRBizg.f['3'];++__cov_742n7agVtRgakP6RGrGnDGRBizg.s['7'];

      if(!onSelect){++__cov_742n7agVtRgakP6RGrGnDGRBizg.b['1'][0];++__cov_742n7agVtRgakP6RGrGnDGRBizg.s['8'];
      return;
      }else{++__cov_742n7agVtRgakP6RGrGnDGRBizg.b['1'][1];}++__cov_742n7agVtRgakP6RGrGnDGRBizg.s['9'];
      onSelect(nativeEvent.index);
      }

How I can avoid this issue? Force use anonymous functions on every place? 😯

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

+1 @guigrpa screen shot 2016-11-23 at 7 21 02 pm That’s what I get after running jest --watch just after running jest --coverage (and having updated my snapshots with coverage)

In my case I’m using enzyme to snapshot a functional stateless component in React, and I get a failed snapshot assertion with coverage enabled, but it’s not Unknown - for me it renders Component:

-     <ListItem
+     <Component
          active={false}
          item={
            Object {
              "id": "thing",
              "name": "stuff",
            }
          }
          onClick={[Function]} />

I’d be happy to isolate a repro case for this setup if it helps at all. The curious part here is that all my other snapshots are fine, it’s just this instance of snapshotting a functional stateless component that has this differing output.

@CGamesPlay you can use jest@^21.3.0-beta.2 and run yarn jest --clearCache. After this, jest should compile your code with the new istanbul instrument code for no problem (no --no-cache is needed).

For stateless components you need to define displayName prop of the Component. See this thread for more background: https://github.com/facebook/jest/issues/1824#issuecomment-250478026

Hmmm… doesn’t solve the problem completely if for some reason you’re snapshotting a React element. It’s name will turn Unknown in the snapshot when you enable --coverage.

Will be fixed in 16.

yeah this kind of sucks. Istanbul wraps functions with other anonymous functions and we take the function name that node gives to the rendered component 😦 ~ @cpojer

@dotfold if you can isolate the test case, please file a new issue with it.