-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Upgrade to React16 and Enzyme3 broke test that use simulate or click event #1201
Comments
I'm also having problems with Enzyme 3 and simulating events. I'm trying to pass a mocked event object like this: checkbox.simulate('change', {stopPropagation: () => {}}); But I'm getting a default React event when simulating event:
I'm using Enzyme 3 with React 15.6.1. |
I have the same Problem with Enzyme 3.0.0 and React 16.0.0. The event is triggered but the state will not be updated (simulate click). |
Just ran into this issue as well. Some of the state is updated but not all of it, here is the code for my test:
So you can see I log the component before click, after click, and after an update. You will see the property
|
@lelandrichardson @ljharb Any idea what could cause this? Upgrading |
We are observing this as well. FWIW, the example in the docs (http://airbnb.io/enzyme/docs/api/ReactWrapper/simulate.html) fails as well |
What about React 15 and enzyme 3? In other words, your tests on React 16 never worked on enzyme 2, so we'd need you to compare between react 15 and 16 on enzyme 3, to know whether the behavior change was caused by enzyme 3, or by the react 16 adapter. |
Yeah, it breaks on react 15 as well, here is the change I made to break it:
Which is:
and now all my tests fail |
I was having the same issue, then I downgraded enzyme to v 2.9.0 and it started working again :). I'll have to read the documentation of v3 to make the changes.. |
Had same issue |
@kopax sorry if you misunderstood it. I didnt solve it |
Also really looking for some help here. |
@HipsterZipster does it work with enzyme 3 and the react 15 adapter? |
@ljharb do you mean converting the actual react / react-dom dependencies to use r15 with the r15 enzyme adapter or just trying the r15 adapter with r16 dependencies? |
I did the conversion of using enzyme3 with react15 and it broke, you can see the proof in my comment here: Also, as the commenter above noted the examples in the documentation don't work with react15 + enzyme3: |
@HipsterZipster i mean getting your codebase passing tests on enzyme 3 with react 15, prior to upgrading to react 16 - upgrading to react 16 and enzyme 3 at the same time is reckless :-) @sontek thanks |
Anyone looking into this? I don't know enough about enzyme to contribute but this is the only dependency preventing us from upgrading. If there is anymore things I can do to help reproduce and move it along, I'm willing to do so. |
@lelandrichardson, any thoughts? |
I am having exactly the same problem, our core team decided to update React from 15.3.2 -> 15.6.2 and we got errors running enzyme 3.1.0. The only tests got broken (broken === doesn't do anything) were related with simulate package.json
|
I believe React 15.6 changed how the "change" event works; it no longer fires when the value hasn't actually changed. I'm not sure if that's your issue or not. |
@ljharb currently the value pass from Perhaps, it's related in how should I target the events, but I am using |
I have the same issue, when I updated My test failed with error it('should set isUserInfoShown to true and userInfoPositionElement from input event when showUserInfo is called', () => {
let currentTarget = {
type: 'button',
}
let spy = sinon.spy()
let event = {
preventDefault: spy,
currentTarget: currentTarget,
}
account.find(AccountButton).props().onClick(event)
spy.calledOnce.should.be.true
account.find(AccountButton).props().isPopoverShown.should.be.true
account.find(AccountButton).props().positionElement.should.be.eql(currentTarget)
}) When I change two lines, test passed Now we should always use expect instead should.be.true? If so, should may be better use some codemode |
The issue with was solved after I use |
I am having the same issue with 3.1 and react 16 adapter.
This no longer works, but did before. |
FYI, if you "refind" the element, it does work. For example
|
@stevenmusumeche do you use mount or shallow ?, what error did you get ? |
In this case, mount. |
I've been doing some experiments with upgrading our dependencies for React and Enzyme, and with R16 and Enzyme 3, these simulate tests have been failing. Strangely, it seems simulating twice lets it show up once. e.g. const component = mount(<TestComponent />);
const spy = jest.spyOn(component.instance(), 'handleClick');
component.update();
component.find('button').simulate('click'); // <-- fails if it is only called once
component.update(); // <-- forcing a second update doesn't do anything
component.find('button').simulate('click'); // <-- succeeds if we simulate a second time
expect(spy).toHaveBeenCalled(); Is there perhaps some kind of async action going on? Edit: To test further, I went back to the Master branch which is still on React 15.6 and upgraded just Enzyme from 2.9.1 to 3.1.0 (adding the React 15 adapter). This above behaviour happens as well. |
Anyone having issues with React 16 and enzyme 3: please first ensure that your tests pass on React 15 and enzyme 3, so that we can narrow down the failure to "upgrading to react 16 and switching to the react 16 adapter". |
@ljharb My example was a pure migration problem the test passed on 15.3.2 but it doesn't on 15.6.2, I will try to figure out the problem check the library internally, but no promises :) |
@EduardoAC thanks, if you could file a new issue for your concern, that might be a difference between the 15.4 and the 15 adapters (this issue is primarily about react 16) |
@ljharb Is it primarily about react 16 if the same error happens on both? My tests fail the same as the issue on both React15 and React16, the only difference being enzyme3 |
@sontek i guess it'd depend on whether it was in the adapter or in enzyme itself; certainly if it's across adapters that suggests it's related to enzyme itself. |
@ljharb Yeah, well I've seen at least @mikehdt [1], @EduardoAC [2], Me [3], and @figalex [4] report that we are experiencing this issue on React 15, so it does seem to be more widespread. Also, I switched the original reproduction code from the creator of the ticket to react15 and their tests still fail, but with enzyme 2 they pass. https://github.com/sontek/enzyme-react16/tree/react15 [1] #1201 (comment) |
with shallow component the simulate works as expected and it update() not necessary with simulate, but with mount it deals with real dom and jsdom so the event system kicks out the real event with real nodes, so current solution with mount for me
With shallow you can use simulate as expected |
I'm not sure if this is the same issue, but I thought it would help with investigation. The below test case fails for me with:
It passes with:
|
For me the following code does not work: const wrapper = mount(<Foo />);
const button = wrapper.find('button');
const span = wrapper.find('span');
button.simulate('click');
expect(span).toHaveText('clicked'); but this does: const wrapper = mount(<Foo />);
const button = wrapper.find('button');
button.simulate('click');
expect(wrapper.find('span')).toHaveText('clicked'); Placing a wrapper.update() somewhere in between has no effect. So I am only able to fix it by calling .find() again on the root element after triggering the event. |
@seb0zz that's intentional in v3; you now always have to re-find from the root every time if there's been changes. |
@ljharb okay why is that? Don't think this is really intuitive 😞 |
It's in the migration guide; it makes found subwrappers immutable, which simplifies the code and makes the code easier to reason about: https://github.com/airbnb/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#element-referential-identity-is-no-longer-preserved |
@ljharb thanks, get the point. Nevertheless, the side effect
is pretty confusing from my point of view. Wrote like a few 100 unit tests with enzyme and found having the reference quit comfortable 😉 |
Any suggestions on what to do with something like this? I've tried every suggestion I can find online and nothing has worked.
Update: I had to change the path to find the correct child to click ( |
Is there a suggested way of writing tests with this update that doesn't require re-writing the code to find elements? In my case, an input element is having its prop changed after a change event fires. To make it pass, I need to |
@cavaloni I'm pretty sure the answer is no. You need to refind the element - see https://github.com/airbnb/enzyme/blob/0fe0d730ef4f27e99144e5be7ec292f3ce014003/docs/guides/migration-from-2-to-3.md#for-mount-updates-are-sometimes-required-when-they-werent-before
|
From all the chatter above, it seems like a fair number of comments are just confusion around the 3 breaking changes? @ljharb to be honest I found that migration guide really hard to follow - it reads more on the implementation detail level - even re-reading it now it's hard for me to pull the message "you now always have to re-find from the root every time if there's been changes" from https://github.com/airbnb/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#element-referential-identity-is-no-longer-preserved which focuses on a discussion around removing duplicates Pulling https://github.com/sontek/enzyme-react16/tree/react15 down and switching to the react15 branch, it did seem like 3.0.0 failed and 2.9.1 succeeded - note that you need to be on the react15 branch since 2.9.1 does not work with v16 (you get I decided to give bisecting (left some comments in the channel) a try following https://web.archive.org/web/20150830120011/https://medium.com/@rasjani/debugging-3rd-party-npm-module-regression-d10530f2b67 but for now I've give up - the steps are tricky (switching node versions, lerna run build, etc) - might circle back around but this bug isn't biting me (yet) |
@jcrben PRs to improve the migration guide are welcome! I’m quite sure it’s not perfect :-) The same commit won’t work with both enzyme 2 and 3; the important part is to get things passing on enzyme 3 before changing your react version. |
I believe this is answered; PRs to the migration guide/docs, and new issues if needed, are both welcome and encouraged. |
I have updated to React16 and Enzyme3
For some reason, I have many test failing with class looking like this :
Test were both using
props
directly andsimulate()
to trigger the state change and the new props of some child components:Example of test now failing :
I can see in both case enzyme log my clicks:
console.log app/components/docs/DropdownsDoc/DropdownColorExample.js:11
clicked
But the next test always fail because the child props have not change:
Reproduction
Repo: https://github.com/kopax/enzyme-react16
Broken React16 and Enzyme3
Working React15 and Enzyme2.8
Any idea on how I should migrate my tests ?
The text was updated successfully, but these errors were encountered: