Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeError: Cannot read property 'className' of undefined #38

Merged
merged 4 commits into from
Jul 3, 2018

Conversation

yamafaktory
Copy link
Contributor

We are using jest-glamor-react in production in our company and so far, this is a super piece of code. Thanks for your awesome work 👍!

What:

I've recently (first time ever) faced a weird issue in some of our (non open-sourced) components:

TypeError: Cannot read property 'className' of undefined

      221 |     inputAA.simulate('change')
      222 |     wrapper.update()
    > 223 |     expect(wrapper).toMatchSnapshot()
          |                     ^
      224 |     expect(wrapper.find('input#a-property-a').prop('checked')).toBeTruthy()
      225 |     // Check the mock function calls after saving.
      226 |     wrapper

      at getSelectorsFromProps (node_modules/jest-glamor-react/dist/utils.js:44:25)
      at node_modules/jest-glamor-react/dist/utils.js:35:14
          at Array.reduce (<anonymous>)
      at getSelectors (node_modules/jest-glamor-react/dist/utils.js:31:16)
      at Function.print (node_modules/jest-glamor-react/dist/serializer.js:38:23)
      at printPlugin (node_modules/pretty-format/build/index.js:287:16)
      at printer (node_modules/pretty-format/build/index.js:332:12)
      at plugin.serialize.plugin.print.valChild (node_modules/pretty-format/build/index.js:289:23)
      at Object.print (node_modules/enzyme-to-json/createSerializer.js:22:14)
      at printPlugin (node_modules/pretty-format/build/index.js:287:16)
      at prettyFormat (node_modules/pretty-format/build/index.js:485:16)
      at Object.throwingMatcher (node_modules/expect/build/index.js:316:33)
      at Object.<anonymous> (src/components/Channels/ChannelSelector.test.jsx:223:21)

The error is thrown only because of the snapshot creation. Removing the expect(wrapper).toMatchSnapshot() line fixes it (but this obviously not what we want).

We are using Enzyme and all the other tests are passing (i.e. nothing is broken). The issue was triggered after splitting one component in smaller chunks.

Why:

For some reason, the props are undefined in the getSelectorsFromProps method (utils). Consequently, an error is thrown in https://github.com/kentcdodds/jest-glamor-react/blob/master/src/utils.js#L37-L38 when trying to access the className property of the props object.

How:

The changes involved are quite simple, returning an empty array early actually solves it. The unit tests of the repository are passing and our tests are not altered at all (a good sign!).

In some weird corner cases, the following error is thrown: `TypeError: Cannot read property 'className' of undefined`.
@@ -4,6 +4,7 @@ dist
.opt-in
.opt-out
.DS_Store
.eslintcache
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the noise, but it was missing in the .gitignore file 😄.

kentcdodds
kentcdodds previously approved these changes Jul 3, 2018
Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

@kentcdodds
Copy link
Owner

Looks like we need a test for this. Could you add one?

@codecov
Copy link

codecov bot commented Jul 3, 2018

Codecov Report

Merging #38 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #38   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           4      4           
  Lines         158    159    +1     
  Branches       41     42    +1     
=====================================
+ Hits          158    159    +1
Impacted Files Coverage Δ
src/utils.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a2bec88...03b429b. Read the comment docs.

@yamafaktory
Copy link
Contributor Author

@kentcdodds Thanks a lot for the quick feedback 🚀🎉. I've added a small test case for that weird corner case.

Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super-duper. Thanks!

@kentcdodds kentcdodds merged commit 61cb3d0 into kentcdodds:master Jul 3, 2018
@yamafaktory
Copy link
Contributor Author

@kentcdodds Thanks a ton ❤️!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants