question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to trigger useEffect from tests

See original GitHub issue

Hello,

I tried testing components that use the cool new hooks API, but useEffect doesn’t seem to work with the test renderer.

Here’s a small failling Jest test:

import React, { useEffect } from "react";
import { create as render } from "react-test-renderer";

it("calls effect", () => {
  return new Promise(resolve => {
    render(<EffectfulComponent effect={resolve} />);
  });
});

function EffectfulComponent({ effect }) {
  useEffect(effect);

  return null;
}

And here’s a minimal reproducing repo: https://github.com/skidding/react-test-useeffect

Note that other use APIs seemed to work (eg. useContext).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

70reactions
gaearoncommented, Nov 2, 2018

We should have something to trigger them.

54reactions
danielkczcommented, Nov 13, 2018

I think you don’t need such a hassle for this. Jest would automock if you create a file <root>/__mocks__/react.js and in there you can just…

const React = require.actual('react')
module.exports = { ...React, useEffect: React.useLayoutEffect }

This is a great workaround as you don’t need to touch any code when this is somehow fixed, you will just remove the mock.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trigger useEffect in Jest and Enzyme testing - Stack Overflow
We want to get the test block to await the next tick, that is, wait for the call stack and pending promises to...
Read more >
useEffect – How to test React Effect Hooks - cultivate
The rest of the test is straightforward: Render the component. Get the list of students and verify that there are no list items...
Read more >
Testing useEffect and Redux Hooks using Enzyme - Medium
To test the component update useEffect hook you'd simply trigger state updates and check for effects in rendered elements. Redux hooks can be ......
Read more >
A Complete Guide to Testing React Hooks - Toptal
This article explores how to test React Hooks and outlines an eight-step testing plan you could employ to test your own projects.
Read more >
How to Test React.useEffect | Epic React by Kent C. Dodds
How does the user make that code run? Make your test do that. That's it. That's the secret. The trick is discovering what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found