-
Notifications
You must be signed in to change notification settings - Fork 47k
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
[Shallow] Implement callEffects option to call effects from shallow renderer (#15275) #16168
Conversation
Details of bundled changes.Comparing: 606f76b...9e6ad0a react-test-renderer
Generated by 🚫 dangerJS |
051d7d0
to
a16136a
Compare
thanks for figuring this out! If you want to copy the logic for componentDidMount and componentDidUpdate, you can see it at #15589 |
ab98815
to
64fc88a
Compare
@bdwain I thought it would be good to handle the life-cycle events in a different PR. Since all my code is using hooks now, I'm not so interested in them. Plus it's easy to call them from outside if you need them, I see that enzyme already does that. |
c61ee64
to
b029466
Compare
Lol. I need this in my testing life. |
Closed by accident, sorry. |
@ohjames Thank you so much for doing this! I'm not as familiar with the react code base (clearly from my comments) but if there's anything you need help on to get this going let me know 😬So eager to have this functionality in place. |
@ssunday Nothing more we can really do for this until the maintainers have a look. They haven't commented on any of the related issues and they've been open for months. It's possible to get this working in your own extension of this library by hooking into one private method on ReactShallowRenderer, that's what I'm doing for now. Enzyme could possibly do the same. |
@ohjames Yeah, it'll take a while. With a draftjs change I was following, took a decent amount of time. But it happened! For the hooking into private methods, could you link a gist or some examples of how to do that you could add to your PR comment? I think that helped with this draftjs PR— provide a way for people to rest in the wild and confirm? Or post to the Enzyme issue related to this. If it's not too difficult? |
This addition would help immensely when testing hooks with enzyme. Hopefully this gets added soon. @ohjames what do you mean by “hooking into private methods”? Right now I am trying to find a good way to test private functions under a functional component and I’m not having any luck with it. |
Functional components can't have "methods"; that's a term that applies only to classes. If you mean, functions created inside the function, or only accessible via closure, you can't and shouldn't directly test those. |
yea i think @ohjames was referring to private methods on the shallow renderer itself |
@ljharb I mean you can make the react test renderer support effects if you override one of its "private" (underscore prefixed) methods and then flush the hooks after each call to render. |
What API do you expect to need? Shallow renderer doesn't depend on any other code so I don't expect it to need private APIs. The only one I can think of is the way Hooks Dispatcher is exposed from the |
We'd be happy to run some tests against a separate package fwiw. Similar to how we have integration tests with |
The shallow renderer uses the following modules from "shared" that I don't think are externally reachable: import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
import shallowEqual from 'shared/shallowEqual';
import invariant from 'shared/invariant';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import warning from 'shared/warning';
import is from 'shared/objectIs';
import type { ReactContext, ReactEventResponderListener } from 'shared/ReactTypes';
import type {ReactElement} from 'shared/ReactElementType'; |
Note that I've previously manually extracted Also, |
What do you recommend using instead? |
I believe part of the mismatch here is that Facebook largely/historically doesn't do serverside rendering, which is where enzyme (and the shallow renderer) is critical. I'm not aware of any scalable alternatives. |
@ljharb @gaearon How about just renaming In the long term, to extract the shallow renderer, couldn't the But please consider implementing my first proposal. In the react-native world the shallow renderer is very popular, not being able to use (test) hooks makes us very sad. |
This would be quite useful to me and to other experienced TDD practitioners who work with react. In addition to being on the order of 100x faster to run as compared to tests that require a stubbed DOM (i.e. JSDOM), shallow rendering allows for test-driving the designs of components much more efficiently/directly than the equivalent virtual DOM tests. Certain classes of components (for example: Providers that encapsulate state machines and provide to their children via context) are much, much easier to test-drive with shallow approaches. Please consider merging this or otherwise exposing hooks to easily integrate effects into shallow test tools like enzyme in a way that is reliable and relatively future-proof. |
@gaearon just curious, is that because those specific components don't have static typing (like Flow) to enforce that nested components get instantiated correctly? Even with Flow, it's sometimes tricky to truly enforce component signatures, e.g. when using poorly typed HOCs. It's true that shallow rendering won't expose problems like passing invalid props but static typing can solve this when done correctly.
Huh. To me, it seems way more flexible to rely on shallow rendering for this because the test doesn't have to know which dependencies to mock. This is important for refactoring; a test should not break when you add/remove nested components since they are not relevant to the main component under test -- nested components already have their own responsibilities. I also like how shallow rendering encourages the test author to only consider functionality that the component under test is responsible for.
That makes sense, I was just curious to understand why, in case I'm missing some flaws in shallow rendering. |
Any updates on this idea of creating a |
@gaearon btw I'm happy to donate the package name when you guys are ready. |
This will make unit tests with React.16 easier |
I'd also love to keep using enzyme to test my stateful functional components |
Following up on this on a weekly basis to make sure things don't go stale. Any updates? |
I'm late to the party but would like to say testing is important hence this pull request is important. Please merge. Thx 😊 |
This comment has been minimized.
This comment has been minimized.
@ohjames, to be honest, this response is borderline unacceptable and I don't even know what to reply. Wow. Can someone reopen the issue? |
@ohjames I recognize you're frustrated with the lack of movement on this but doing personal attacks on people working on this repository is not acceptable. As I said in my earlier comment:
There is literally nothing that would prevent you from doing so and collaborating with e.g. Enzyme on it. The shallow renderer can be copy pasted, published in its own package, and be developed there. I'm sorry that you were waiting for us to merge this PR, but I thought we set the expectation clear right above by saying we don't intend to invest more into shallow renderer and expanding its API surface. I don't see what kind of action you were expecting on our side. |
This is not correct btw; we do rely heavily on server rendering for the new Facebook. |
I'm going to lock this PR because the discussion has turned in an unconstructive direction. Additionally, @ljharb said he can't comment on the PR for some reason (maybe a GitHub bug). I opened a new issue to discuss this in #17321. I welcome everyone from this thread (but please let's keep this one civil). |
Fixes the effects related part of #15275 by allowing the user to tell the shallow renderer to call effect hooks.
It could do with a couple more tests but I wanted to get feedback on the approach and was also worried about putting any more time into it given that the maintainers have not responded to the corresponding issue in the months it's been open.Added a full set of tests.