Code Monkey home page Code Monkey logo

Comments (20)

danielcondemarin avatar danielcondemarin commented on September 28, 2024 29

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 🙏

from rfcs.

danejordan avatar danejordan commented on September 28, 2024 8

For anyone here Kent Dodd's react-testing-library has got ya covered ( demo )

EDIT: As mentioned in the original issue mount works, just not shallow

from rfcs.

ChibiBlasphem avatar ChibiBlasphem commented on September 28, 2024 3

I hope it will. Enzyme is pretty known as a testing library for React but it misses support for some features (memo, lazy, hooks).

from rfcs.

milesj avatar milesj commented on September 28, 2024 2

@geoffdavis92 You don't need either lib. You can just use act() from react-dom.

from rfcs.

Jessidhia avatar Jessidhia commented on September 28, 2024 1

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.

from rfcs.

ChibiBlasphem avatar ChibiBlasphem commented on September 28, 2024

@dankreiger I had the same problem, and investigated a little;
The problem is that for using hook react need to have a "Dispatcher" which is not declared on shallow renderer (even for 'react-test-renderer/shallow' 16.7.0)

from rfcs.

milesj avatar milesj commented on September 28, 2024

Enzyme currently does not support hooks.

from rfcs.

koba04 avatar koba04 commented on September 28, 2024

In order to support Hooks in enzyme's shallow, we have to add Hooks support into ShallowRenderer(react-test-renderer/shallow).

from rfcs.

itaditya avatar itaditya commented on September 28, 2024

@milesj is this fixed now?

from rfcs.

milesj avatar milesj commented on September 28, 2024

@itaditya No. Just take a look at Enzyme itself. https://github.com/airbnb/enzyme/blob/master/CHANGELOG.md

from rfcs.

itaditya avatar itaditya commented on September 28, 2024

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.

from rfcs.

ChibiBlasphem avatar ChibiBlasphem commented on September 28, 2024

@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 :)

from rfcs.

ChibiBlasphem avatar ChibiBlasphem commented on September 28, 2024

@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)

from rfcs.

danejordan avatar danejordan commented on September 28, 2024

@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

from rfcs.

geoffdavis92 avatar geoffdavis92 commented on September 28, 2024

This is good to know. I've been sitting here trying to get useEffect to fire for like an hour. Guess I'll migrate to r-t-l until Enzyme catches up.

from rfcs.

maciossek avatar maciossek commented on September 28, 2024

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()
  })
})

from rfcs.

vpicone avatar vpicone commented on September 28, 2024

@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 mount and unmount instead of shallow.

from rfcs.

maciossek avatar maciossek commented on September 28, 2024

I might try it with the next test, but I got the same Invariant Violation: Hooks ... error: It didn't render at all.

from rfcs.

goncalobo avatar goncalobo commented on September 28, 2024

I've managed to test a custom hook using @maciossek's workaround along with act().
https://gist.github.com/goncalobo/ace875af564d4fd585651abd86f65cd2

from rfcs.

gaearon avatar gaearon commented on September 28, 2024

This thread is full of confusion because:

  • It references missing support for Hooks in shallow renderer which has been fixed in a stable release
  • There is however a bug that causes state to not update in shallow renderer which has not been fixed yet
  • There's a whole separate issue of which APIs are provided by enzyme

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.

from rfcs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.