enzyme: Mount find() Does Not Find Rendered Marker
This one is pretty frustrating. I mount this container. It does hit the code I expect that contains my css feature marker (ft-playback-error
). But in the end I end up with not found.
screencast of this issue: https://youtu.be/kh-UJTig3Qg
it.only('renders a coordinates message in the player when no coordinates exist', () => {
const store = createStore(reducers, {}),
liveScreen = mount(
<Provider store={createStore(reducers, {})}>
<Live
breakpoint="site-web"
coordinates={null}
renderInfoInDetail={() => {}}
setOverlay={() => {}}
store={store}
AccessEnablerUrl=""
getUseUtagGlobal={() => {}}
pageViewEvent={() => {}}
/>
</Provider>),
message = liveScreen.find('.ft-playback-error');
expect(message).to.have.length(1);
});
message.nodes ends up being 0 (or aka not found). Why?
I assumed when you mounted, it runs every child’s render() (every child that’s being hit down the codepath of container…obviously depending on whatever logic is in its render will determine which children are being rendered) method of this container.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 58 (14 by maintainers)
Commits related to this issue
- test: refactor github organism test code with `.update()` This is a better workaround as opposed to comparing html output. https://github.com/airbnb/enzyme/issues/1233 — committed to byCedric/GitHub-Website-legacy by byCedric 6 years ago
- test: clean up dirty tests and add app page component test (#71) * test: refactor github organism test code with `.update()` This is a better workaround as opposed to comparing html output. htt... — committed to byCedric/GitHub-Website-legacy by byCedric 6 years ago
@StefanoSega I had a similar problem. I have a component written in pure JS that inserts html inside React component. After component mount, I’m using render method before find and it works as expected. Example:
@dmccown1500 I also had a similar issue, seems like redux and enzyme not working properly.
.html()
was giving proper markup but.find('selector')
wasn’t working (enzyme v3.1.0
)wrapper.update()
after modifying the redux state fixedwrapper.find('.deep-nested-component')
returning anempty set
👍I’m having this issue. Enzyme 3.3.0 with MobX. HTML is correct but
find()
doesn’t work. Theupdate()
trick didn’t helpWORK AROUND FOUND: So at least for us this was an issue where Redux (or maybe Redux-Saga/Thunk?) and Enzyme are not playing well together. Enzyme does not auto-update the wrapper when some of the external updates are happening. They talk about in the migration guide but I guess I just missed it? In any case you can call
.update()
on the root component.Using
.render().find()
did the trick for me, as in @d-e-p’s example. Thanks @d-e-pCan confirm with versions:
when the component being tested must be wrapped by a redux Provider:
then
wrapper.html()
always shows the child nodes, butwrapper.find(...)
will not find them UNLESSwrapper.update()
is called firstUsing
.render().find()
doesn’t allow me to simulate a click for a button. This is still happening.meet with the same problem. html() is right but find couldn’t find the dom. update() doesn’t help.
@sadu thanks for the tip. Had similar issue -
wrapper.html()
returned correct tree but when I was doingwrapper.find(...)
it seemed like it was searching in the tree of the previous state.wrapper.update()
helped@aweary I’m using
"enzyme": "^3.1.0"
and adapter is"enzyme-adapter-react-16": "^1.0.1"
and I have the same problemI’ve just tried upgrading a small project to Enzyme 3 and think I’m running into the same (or similar-looking) issue – using React 15.6.1 with enzyme-adapter-react-15 1.0.2 and enzyme 3.1.0.
It appears that Enzyme isn’t updating it’s internal understanding of the state of the vdom, even though the actual host nodes are updated as expected.
In this test, I’m using
react-apollo
and a mocked API render a component which goes from a loading state to a loaded state after a 0ms sleep. In enzyme 2, everything works. In enzyme 3, I get inconsistent results betweenpage.html()
andpage.debug()
. (And my test fails in the same way thatdebug
is wrong.)Enzyme thinks the page is still in a loading state, even though the
html()
call returns the loaded state. (I also instrumented the test and see render being called with the ‘loaded’ props).Output
Expecting to see"Foo" and “Bar” listed in both the
html
anddebug
output, but only seeing them in thehtml
.debug
still returns the loading state.html
outputdebug
output:I just encountered this issue with redux and enzyme 3.3.0, the
.update()
workaround does work.after upgrading to enzyme 3 this problem went way. It’s either that or my component wasn’t rendering what I thought it was and the marker wasn’t there. Closing this.
if noone if seeing the same results from
wrapper.debug()
andwrapper.html()
after calling update, usewrapper = wrapper.update()
. In enzyme 3, the wrapper is immutable.@kaiyoma
find
finding more than it used to is an expected change; use.hostNodes()
if you want to filter it down to DOM elements.I did this today and I’m also running into the issues mentioned in this thread. Tests that were previously passing are now failing. The tests in question fail because they either:
Usually the test fails because
find
fails to find anything, but sometimesfind
finds more than it should! Something is not working right here.@EvgenyW3 see #1213; fragments aren’t currently supported.
For me the problem with .find() was because of wrapping a component with <React.Fragment>. Changed it back to <div> and that did the trick. Seems like Enzyme 3.3.0 doesn’t like React.Fragment
@dmccown1500 The update works great!
Confirmed again:
I’m not using Redux.
wrapper.html()
was correct butwrapper.find()
couldn’t find anything. Added awrapper.update()
before thefind
and it worked.Just tried with Enzyme 3. This is no longer an issue. Thank you!
@3axap4eHko which,
shallow
? The entire point of shallow rendering is to never traverse subcomponents.@ljharb
.feature__cover
is parsed correctly, so I don’t think so, but it could be related to the traversal logic. @dschinkel @3axap4eHko if you usefindWhere
does it work?If it doesn’t, can you verify if
findWhere
traverses the node you’re targeting? A reduced example that we could reproduce the issue with would be useful, it’s hard to say whats going on with a 800+ line example that imports from other unknown files.the same for me:
I’m getting
but
.feature__cover
found well