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

Enzyme 3 #198

Merged
merged 13 commits into from
Oct 3, 2017
Merged

Enzyme 3 #198

merged 13 commits into from
Oct 3, 2017

Conversation

rylanc
Copy link
Contributor

@rylanc rylanc commented Sep 27, 2017

No description provided.

@itsdouges
Copy link

you have some linting errors.

package.json Outdated
"enzyme": "1.x || ^2.3.0",
"react": "^0.14.0 || ^15.0.0-0",
"react-dom": "^0.14.0 || ^15.0.0-0"
"cheerio": "0.19.x || 0.20.x || 0.22.x || 1.0.0-rc.2",
Copy link
Member

Choose a reason for hiding this comment

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

enzyme 3 requires cheerio v1 or higher - this needs to change to ^1.0.0-rc.2

Copy link
Contributor

Choose a reason for hiding this comment

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

I think #196 has got the dependencies pretty much right

Copy link
Member

Choose a reason for hiding this comment

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

yes as a nonbreaking change #196 does this much better (altho enzyme 2 requires cheerio 0.22 so the 0.19 and 0.20 peer deps aren't helpful)

package.json Outdated
@@ -58,13 +58,14 @@
"babel-register": "^6.5.2",
"chai": "^3.0.0 || ^4.0.0",
"cross-env": "3.1.4",
"enzyme": "^2.3.0",
"enzyme": "3.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

nothing should ever be pinned; these should all use ^.

package.json Outdated
"react": "^0.14.0 || ^15.0.0-0",
"react-addons-test-utils": "^0.14.0 || ^15.0.0-0",
"react-dom": "^0.14.0 || ^15.0.0-0",
"react": "16.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

dev dep ranges should always exactly match peer dep ranges.

@@ -2,6 +2,8 @@ import $ from 'cheerio'

import TestWrapper from './TestWrapper'

const getDisplayName = type => type.displayName || type.name || type
Copy link
Member

Choose a reason for hiding this comment

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

why is this function needed?


const rootInstance = root.instance()
const rootType = rootInstance ? rootInstance.constructor : root.getElement().type
const name = rootType ? getDisplayName(rootType) : '???'
Copy link
Member

Choose a reason for hiding this comment

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

root.name() || '???' should suffice here, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enzyme's ShallowWrapper behaves slightly different from the ReactWrapper when it comes to the name() function. Given the following component:

const Example = () => <table />;

mount(<Example />).name() returns 'Example' while shallow(<Example />).name() returns 'table'.

@marchaos
Copy link

marchaos commented Sep 27, 2017

Is there a way that we can get this expedited? With React 16 and enzyme 3 being released last night, this is the final piece for many upgrading.

Thanks!

@@ -1,8 +1,10 @@
class Fixture extends React.Component {
render () {
return (
<div id='parent'>
<div id='child' />
<div>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are all these necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Enzyme 3 changes the way it wraps HTML when returning from the render function. The new version returns a cheerio wrapper with a type of tag that IS the parent element (instead of a wrapper with a type root that contains the parent element).

For example:

<div id='parent'>
  <div id='child' />
<div>

With the above code in Enzyme 3, calling wrapper.find('#parent').length returns 0 while calling wrapper.is('#parent') returns true. With Enzyme 2, wrapper.find('#parent').length returns 1 and wrapper.is('#parent') returns false.

Copy link
Member

Choose a reason for hiding this comment

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

(this is as a result of a breaking change in cheerio v1)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining, we'll definitely have to document this in our CHANGELOG.md to educate people on how to upgrade

Copy link
Contributor

Choose a reason for hiding this comment

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

Crazy 3AM idea (jetlag), can we add the extra <div /> in chai-enzyme when using render?

Copy link
Member

Choose a reason for hiding this comment

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

You could wrap it, sure, but then anyone who didn't understand this, and was checking any other method besides .html(), would have the wrong assertions.

In other words, it's better for wrapper.is('#parent') === true and wrapper.html() not to contain the parent div, then for the reverse to be true.

@ayrton
Copy link
Contributor

ayrton commented Sep 27, 2017

Can you get rid of the yarn.lock file, if we include this we should do it in a separate PR

@ljharb
Copy link
Member

ljharb commented Sep 27, 2017

I'd suggest adding package-lock=false to .npmrc, and yarn.lock, package-lock.json, and npm-shrinkwrap.json to .gitignore :-)

@ayrton
Copy link
Contributor

ayrton commented Sep 27, 2017

@marchaos we're trying to release a react 16 compatible version as soon as possible. Any help is appreciated :)

@jugglinmike
Copy link

@jugglinmike, any idea on these cheerio test failures and how to fix them?

@ljharb I just pulled this branch down and ran it, but the tests are passing on my end. It's difficult to see the problem on TravisCI because the jobs are halting on the previously-identified linting error. It doesn't look like any new commits have been push up, so I think I may be doing something wrong here. Should I be using some command other than npm test?

@ljharb
Copy link
Member

ljharb commented Sep 27, 2017

@jugglinmike travis only runs https://github.com/producthunt/chai-enzyme/blob/master/.travis.yml#L12-L14 - that should be sufficient.npm run test:unit by itself should also skip the linter.

@jugglinmike
Copy link

Still no dice, I'm afraid. The command

$ npm run test:unit

Finishes with:

  431 passing (2s)
$ echo $?
0

For context:

$ node --version
v6.10.3
$ npm ls | grep cheerio
│ ├─┬ cheerio@1.0.0-rc.2
$ git log --oneline -n4
1afa0ca Fix peerDependencies
b954dd5 :shirt: Fix linter warnings
174bcef Fix to work with enzyme 3
c985924 Support an array of nodes in `contain`

@jugglinmike
Copy link

Uhg. @ljharb I somehow confused this branch with gh-196--that's where you actually asked for my input. Sorry for the noise, folks.

@itsdouges
Copy link

itsdouges commented Sep 27, 2017 via email

@rylanc
Copy link
Contributor Author

rylanc commented Sep 27, 2017

Bah. Sorry, I didn't realize there was an existing PR. @jugglinmike, I'm happy to concede the honor to you. Feel free to use anything in this one that helps.

@jugglinmike
Copy link

Actually, that one isn't mine. I'm just trying to help with integration issues (though it seems you've solved those here).

@rylanc
Copy link
Contributor Author

rylanc commented Sep 27, 2017

Oh OK. Well, @ljharb, @ayrton: should the goal be to maintain backwards compatibility with enzyme 2? If so, I should probably add a test config for both.

@ljharb
Copy link
Member

ljharb commented Sep 27, 2017

@rylanc it's always ideal to avoid breaking changes if possible :-)

@rylanc
Copy link
Contributor Author

rylanc commented Sep 27, 2017

In order to satisfy the peerDependencies of react-element-to-jsx-string and add compatibility with React 16, we would have to upgrade react-element-to-jsx-string to version 12.0.0. Unfortunately, in version 6.0.0 of react-element-to-jsx-string, it started requiring an ES2015 environment (or polyfill): https://github.com/algolia/react-element-to-jsx-string/blob/master/CHANGELOG.md#600-2017-01-03. It seems like the options are:

  1. Start requiring an ES2015 environment in chai-enzyme
  2. Use an alternative way of converting elements to strings. It looks like we can leverage enyme's ShallowWrapper.debug to achieve a similar output (probably not identical):
function reactElementToJSXString(node) {
  const Wrapper = () => node
  return shallow(<Wrapper />).debug()
}

I'd lean towards the latter, if it were up to me. Let me know what you think

@rylanc
Copy link
Contributor Author

rylanc commented Sep 28, 2017

@ljharb The tests are passing for both enzyme 2 and 3 now if you have time to take another look.

@ayrton
Copy link
Contributor

ayrton commented Sep 28, 2017

@rylanc I'm ok dropping react-element-to-jsx-string if the alternative is good enough. How would the output look like?

@rylanc
Copy link
Contributor Author

rylanc commented Sep 28, 2017

@ayrton It's mostly the same. It looks like react-element-to-jsx-string does some fancy formatting of props (function names, deep serialization of objects, etc.) while enzyme's debug() does not.

@ljharb
Copy link
Member

ljharb commented Sep 29, 2017

@rylanc improving debug in enzyme itself is always possible if it doesn't add too much complexity, fwiw

package.json Outdated
"react": "^0.14.0 || ^15.0.0-0",
"react-dom": "^0.14.0 || ^15.0.0-0"
"cheerio": "0.19.x || 0.20.x || 0.22.x || ^1.0.0-0",
"enzyme": "^2.9.1 || ^3.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, is it possible to support "^2.7.0 || ^3.0.0"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup. Done!

@enzymejs enzymejs deleted a comment from dschinkel Sep 29, 2017
@@ -13,7 +13,7 @@ export default function wrap (el) {
return new ReactTestWrapper(el)
}

if (el && el._root && el.options) {
if (el && el.cheerio && el.options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need a fallback (to _root for old cheerio versions)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks Cheerio.prototype.cheerio has been around since 0.19.0: https://github.com/cheeriojs/cheerio/blob/0.19.0/lib/cheerio.js#L99

"react": "^0.14.0 || ^15.0.0-0",
"react-dom": "^0.14.0 || ^15.0.0-0"
"cheerio": "0.19.x || 0.20.x || 0.22.x || ^1.0.0-0",
"enzyme": "^2.7.0 || ^3.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

this is semver-major since it's dropping v1, but i don't think it's worth retaining v1 support either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Since the current version is still < 1.0, does this make it 0.9.0?

Copy link
Contributor

@ayrton ayrton Oct 2, 2017

Choose a reason for hiding this comment

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

IMO we release a 1.0 when this PR is merged in so I wouldn't worry about it

Copy link
Member

Choose a reason for hiding this comment

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

1.0 is always good :-) if it remained sub-1.0 for some reason then it'd be 0.9.0, yes

@rylanc
Copy link
Contributor Author

rylanc commented Oct 3, 2017

@ljharb @ayrton Is there anything still missing from this PR?

@ayrton ayrton merged commit 8920067 into enzymejs:master Oct 3, 2017

function reactElementToJSXString (node) {
const Wrapper = () => node
return shallow(<Wrapper />).debug()
Copy link

@VidyullathaKandipati VidyullathaKandipati Oct 3, 2017

Choose a reason for hiding this comment

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

Hey @rylanc I am not sure, if I can comment on this, but I found this while using your changes. Hence, bring to your notice. We need import React from 'react' before creating a react element here.

Copy link
Member

Choose a reason for hiding this comment

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

eslint-plugin-react should be used to lint for this, fwiw.

Copy link
Member

Choose a reason for hiding this comment

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

(and the airbnb eslint config has it all set up for you ;-) )

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.

None yet

7 participants