testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.25k stars 463 forks source link

feat(waitFor): Support async `unstable_advanceTimersWrapper` #1229

Closed eps1lon closed 1 year ago

eps1lon commented 1 year ago

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[bot] commented 1 year ago

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 c699137a0a850965afb8b547e7d3f724a0174ccd:

Sandbox Source
react-testing-library-examples Configuration
codecov[bot] commented 1 year ago

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%> (ø)

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

smarlhens commented 1 year ago

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.