Skip to content
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

[Best practices] setImmediate/ setTimeout is a good way to fix async work within IT case ? #11514

Closed
sanriqing opened this issue Jun 3, 2021 · 7 comments

Comments

@sanriqing
Copy link

💬 Questions and Help

It's ok to use setTimeout or setImmediate to wait some async work done ?

here is a example

// Component file::
class Demo extends React.Component {
   state ={data:''}
    async componentDidMount(){
        const data = await api.getData()
        this.setData({data})
    }
    render() {
        const {data} = this.state;
        return <div data-test-automation-id="test">{data}</div>;
    }
}
// test file
describe('Demo', () => {
    it('should render correct data', async () => {
         jest.spyOn(api,'getData').mockResolvedValue('testData');
        const {getByTestId} = render(<Demo/>)
        // we directly use getByTestId will be fail . you see , data still is ''
        // it's a good way to setImmediate to wait api called done?
        // or should we use rerender  / unmount to fix it ?
        await new Promise(setImmediate)
        expect(getByTestId('test')).toHaveTextContent('testData');
    });
});

we met many many scenarios like this.
like: show alert after save done

Looking forward to answers , thank you .

@sanriqing
Copy link
Author

yeh, waitFor api called is fine in this case , but sometime we didn't export the async work out to check weather it have been called.

@Smrtnyk
Copy link

Smrtnyk commented Jun 3, 2021

I don't get how can you pass in setImmediate as executor for a Promise?
Shouldn't it be

await new Promise((resolve) => {
                setImmediate(resolve);
            });

Also setImmediate is removed from a global in jsdom in jest 27, so you would want to import setImmediate from "timers"

@sanriqing
Copy link
Author

I don't get how can you pass in setImmediate as executor for a Promise?
Shouldn't it be

await new Promise((resolve) => {
                setImmediate(resolve);
            });

Also setImmediate is removed from a global in jsdom in jest 27, so you would want to import setImmediate from "timers"

yeh.... What's i want to say is set a timer to wait for microtasks have been done . setImmediate is not necessary ,setTimeout is ok too. Much thanks for answer.

@sanriqing
Copy link
Author

ref: #2157

@github-actions
Copy link

github-actions bot commented Jun 3, 2022

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions
Copy link

github-actions bot commented Jun 3, 2022

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot closed this as completed Jun 3, 2022
@github-actions
Copy link

github-actions bot commented Jul 4, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants