Create-react-app: Functional components lose displayName when running coverage

Created on 4 Nov 2016  路  7Comments  路  Source: facebook/create-react-app

Description

Coverage fails to run tests correctly, resulting in failed tests that are actually passing when run without --coverage.

Expected behavior

Passing tests should still pass when i add --coverage

Actual behavior

Tests that are passing without --coverage are failing when i add --coverage.

Environment

Run these commands in the project folder and fill in their results:

  1. npm ls react-scripts (if you haven鈥檛 ejected): UNMET PEER DEPENDENCY [email protected]
  2. node -v: v6.9.1
  3. npm -v: 3.10.9

Then, specify:

  1. Operating system: OSX Sierra 10.12
  2. Browser and version: Safari 10.0
bug underlying tools

Most helpful comment

This is fixed as of [email protected]. I just did a fresh project with create-react-app and was able to use npm test -- --coverage successfully.

Note that displayName is only set when you define your component and use that variable as the default export. If you follow the 2nd example, the displayName isn't set and defaults to Component, but this still passes when checking coverage.

// Works
const Hello = () => <div>Hello</div>

export default Hello

// Doesn't work
export default () => <div>Hello</div>

All 7 comments

any progress on this?

Hey, sorry for the long response!

It's npm test -- --coverage, not npm test --coverage.
Is this what you were typing?

i was indeed running it correctly, and it was generating coverage reports, just not actually passing some tests. but i figured out what the problem was, when i ran tests with --coverage my functional components' displayName property wasn't set, so when i rendered them with shallow and tried to do wrapper.find(...) it was failing to find the functional child nodes, even though it was finding them successfully when running the tests without --coverage. i had to go through all my functional components adding the displayName property. Components created with React.createClass were unaffected... perhaps this would be something to mention in the docs or investigate in your instrumentation tool?

Oh this is interesting, thanks for the link.
Definitely let's track it.

This is fixed as of [email protected]. I just did a fresh project with create-react-app and was able to use npm test -- --coverage successfully.

Note that displayName is only set when you define your component and use that variable as the default export. If you follow the 2nd example, the displayName isn't set and defaults to Component, but this still passes when checking coverage.

// Works
const Hello = () => <div>Hello</div>

export default Hello

// Doesn't work
export default () => <div>Hello</div>

Awesome, thanks for reporting back.
Closing this.

I sent a PR to Jest to enforce a minimal version in the future, but for now existing users can get the fix by removing node_modules and running npm install again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rdamian3 picture rdamian3  路  3Comments

wereHamster picture wereHamster  路  3Comments

fson picture fson  路  3Comments

fson picture fson  路  3Comments

barcher picture barcher  路  3Comments