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.

withHooks and withoutHooks do not support async tests / promises

See original GitHub issue

Thanks for this package. I’m currently trying to get this to work with an async test, but that doesn’t seem to be supported yet. Could this be added?

Example usage:

it('does something async', async () => {
    await withHooks(async () => {
        const wrapper = shallow(<App />);

        await something();
    });
});

I’m currently using this as a workaround:

export default function withHooksAsync(test: () => Promise<void>): Promise<void> {
    return new Promise<void>((resolve, reject) => {
        withHooks(() => {
            test().then(resolve).catch(reject);
        });
    });
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mikeborozdincommented, Aug 1, 2020

Hi @d-luk ,

Thanks for reporting this. I’ll have a look!

1reaction
dunklesToastcommented, Oct 7, 2020

So let’s say you’ve got this component:

import React, { useEffect, useState } from 'react';

function someAsyncCode() {
    return new Promise((resolve) =>
        setTimeout(() => {
            console.log('Resolving');
            resolve(true);
        }, 3000)
    );
}

export default function Test(): JSX.Element {
    const [text, setText] = useState('Not Effected');

    useEffect(() => {
        someAsyncCode().then(() => {
            console.log('Hooked');
            setText('Effected');
        });
    }, []);

    return <p>{text}</p>;
}

This will show the Text “Effected” after three seconds (i sped up the video):

example

If I use this test-suite: Test Setup

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import enableHooks from 'jest-react-hooks-shallow';

configure({ adapter: new Adapter() });
enableHooks(jest);

Component.test.tsx

import * as React from 'react';
import { mount } from 'enzyme';
import Test from './Test';

describe('Test Test', () => {
    const wrapper = mount(<Test />);
    it('Should render private route', async () => {
        console.log(wrapper.html());
        expect(wrapper..text).toBe("Effected");
    });
});

The expected result is that the test succeeds and it’ll wait for the useEffect to run through. Unfortunately, it does not.

Could you have a look at this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

For async tests and hooks, ensure "done()" is called
For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. My test code is, I have the...
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 >
Analytics for React Native 2.0 | Segment Documentation
On May 15, 2023, Segment will end support for Analytics React Native Classic, ... See how to use Analytics React Native 2.0 with...
Read more >
Hooks are the worst thing to ever happen to React. They're so ...
Most React components that use hooks end up having several chains of promises inside them but no way to await the final promise...
Read more >
Using Async Await Inside React's useEffect() Hook
In this post you'll learn how to use an async function inside your React useEffect hook. Perhaps you've been using the good old...
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