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

feat(waitFor): Support async unstable_advanceTimersWrapper #1229

Merged
merged 1 commit into from May 12, 2023

Conversation

eps1lon
Copy link
Member

@eps1lon eps1lon commented Apr 30, 2023

unstable_advanceTimersWrapper now receives an async function. If your wrapper just returns the given callback, nothing should break (e.g. this is not a breaking change for React Testing Library. But if you call it first with the expectation that it resolved synchronously, you may observe changed behavior. This method is marked as unstable_ and not subject to semantic versioning. Still releasing as a feature for safe measure.

What:

Add support for having an async unstable_advanceTimersWrapper

Why:

To support experimentation with async act

How:

await advanceTimersWrapper. This also enables us to flush the microtask queue that exist after the macrotask.

This changes the observed semantics in callback a bit since callback now observes macrotask+microtask instead of microtask+macrotask.
I don't think that matters though. We definitely have no test in this library and RTL tests passed using this version of waitFor.

Checklist:

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c699137:

Sandbox Source
react-testing-library-examples Configuration

@codecov
Copy link

codecov bot commented Apr 30, 2023

Codecov Report

Merging #1229 (c699137) into main (39a64d4) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main     #1229   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines         1042      1038    -4     
  Branches       351       347    -4     
=========================================
- Hits          1042      1038    -4     
Flag Coverage Δ
node-14 100.00% <100.00%> (ø)
node-16 100.00% <100.00%> (ø)
node-18 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/wait-for.js 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected a breaking change with this, but I tested this on a small project and everything continues to work fine 👍

@eps1lon eps1lon merged commit d09b3c2 into testing-library:main May 12, 2023
7 checks passed
@smarlhens
Copy link

FYI: This change breaks a lot of tests in my Vue project.

I haven't taken the time to make a reproduction repo yet. Downgrading to 9.2.0 fixes the issue, or increasing the timeout on waitFor also seems to work around the problem.

// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
// eslint-disable-next-line no-await-in-loop
await advanceTimersWrapper(async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious as an outsider, why is await and async needed here when we don't explicitly await anything? Are we trying to wait a tick?

Comment on lines -96 to -97
setTimeout(r, 0)
jest.advanceTimersByTime(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if this was a bug but shouldn't the order of these 2 statement been reversed? ie. the promise should resolve after advancing timers?

@eps1lon eps1lon deleted the feat/await-advance branch June 2, 2023 22:13
eps1lon pushed a commit to eps1lon/web-testing-library that referenced this pull request Oct 3, 2023
eps1lon pushed a commit to eps1lon/web-testing-library that referenced this pull request Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants