-
Notifications
You must be signed in to change notification settings - Fork 558
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
Testing functional components using state hooks with Enzyme #71
Comments
@dankreiger I had the same problem, and investigated a little; |
Enzyme currently does not support hooks. |
In order to support Hooks in enzyme's |
@milesj is this fixed now? |
@itaditya No. Just take a look at Enzyme itself. https://github.com/airbnb/enzyme/blob/master/CHANGELOG.md |
Wasn't able to find it in the changelog. Thanks for helping though. I tried to use enzyme yesterday, it said can't call hook outside component body. |
@itaditya that is normal. Nor React nor Enzyme did fix the bug yet. And that is not a bug, it's just not implemented yet :) |
For anyone here Kent Dodd's react-testing-library has got ya covered ( demo ) EDIT: As mentioned in the original issue |
@Dane456 nope it can't shallow render. Besides, kent created this lib because Hé don't really like the shallow approach. (render instead of shallow, and Enzyme can do it) |
@ChibiBlasphem yeah you're right, that was already mentioned in the original issue. Didn't see that. I wonder if there are any performance differences between enzyme's mount and react-testing-library's render |
React now officially supports hooks as of today 🙂 https://reactjs.org/blog/2019/02/06/react-v16.8.0.html. Is there an eta when enzyme will support them? Thanks 🙏 |
I hope it will. Enzyme is pretty known as a testing library for React but it misses support for some features (memo, lazy, hooks). |
Enzyme / shallow rendering wants information about things that not even React knows about unless the component is fully mounted. Perhaps it needs its own Babel plugin to add metadata to elements. |
This is good to know. I've been sitting here trying to get |
@geoffdavis92 You don't need either lib. You can just use act() from react-dom. |
Workaround with react-test-renderer: enzymejs/enzyme#1938 (comment) This is my final working test: import React from 'react'
import { shallow } from 'enzyme'
import { OverlayComponent } from './../index'
import ShallowRenderer from 'react-test-renderer/shallow'
describe('<OverlayComponent />', () => {
it("doesn't explode with a workaround", () => {
const renderer = new ShallowRenderer()
renderer.render(<OverlayComponent show closeFn={jest.fn()} t={key => key} />)
const output = renderer.getRenderOutput()
const wrapper = shallow(<div>{output}</div>) // Have to wrap it, otherwise you get: TypeError: ShallowWrapper can only wrap valid elements
expect(wrapper).toMatchSnapshot()
})
}) |
@maciossek I haven't seen issues with rendering, it's mainly the hooks not doing anything at all. A simpler workaround might just be using |
I might try it with the next test, but I got the same |
I've managed to test a custom hook using @maciossek's workaround along with |
This thread is full of confusion because:
Ultimately this is a repository for proposing API changes and not for bug reports. This is why your messages don't receive a response, and why this thread is so confused with half-broken workarounds. I will lock it to prevent further confusion. If something doesn't work in React please file an issue in the React repository instead. There we can discuss it and suggest the correct fix. |
The error looks like this if I
shallow
render:The component looks like this:
Is this an issue Enzyme will have to deal with or is there some other underlying issue?
The test passes with a
mount
render, but in a simple component test like this I would usually do ashallow
render.I know this might be outside of the scope of this thread, but at the same time I love using the
Enzyme
test utilities, and I thought I should bring this up.Originally posted by @dankreiger in #68
The text was updated successfully, but these errors were encountered: