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

detectHostComponentNames: render test tree inside act to avoid timer leaks #1371

Merged
merged 4 commits into from
Mar 23, 2023

Conversation

jsnajdr
Copy link
Contributor

@jsnajdr jsnajdr commented Mar 17, 2023

Fixes a bug that I discovered while working on #1366: when first calling render in a test suite, the render function calls detectHostComponentNames, which mounts a testing tree with a few components. This tree is rendered outside the act environment, and therefore schedules a setImmediate callback to process effects. This scheduled callback then has an unwanted interaction with my actual testing code. If there is a waitFor inside my test, the effects scheduled by my testing code will not be processed by my setImmediate, but by the one scheduled by detectHostComponentNames. That makes the precise order of events inside my test non-deterministic and unpredictable.

Look at this call stack:

Screenshot 2023-03-17 at 11 55 24

and note how it starts in my waitFor.test.ts test, in the render function, then calls detectHostComponentNames, schedules setImmediate callback, and how that setImmediate later processes an update scheduled by my test code (the Apple component).

This PR avoids that by wrapping the render with act. That way, all effects scheduled by the render will come into the actQueue, and will be flushed before returning. No leaks.

In the code, I had to do some silly IIFE magic to make TypeScript happy: it doesn't know that act will synchronously and reliably initialize the result value.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

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

@jsnajdr Looks good! Again thanks for submitting this non-trivial fix!

@mdjastrzebski mdjastrzebski merged commit 6bf8d24 into callstack:main Mar 23, 2023
@codecov
Copy link

codecov bot commented Mar 23, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (3eebab2) 96.13% compared to head (fd1396c) 96.14%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1371   +/-   ##
=======================================
  Coverage   96.13%   96.14%           
=======================================
  Files          49       50    +1     
  Lines        3314     3318    +4     
  Branches      491      492    +1     
=======================================
+ Hits         3186     3190    +4     
  Misses        128      128           
Impacted Files Coverage Δ
src/flushMicroTasks.ts 100.00% <100.00%> (ø)
src/helpers/host-component-names.tsx 100.00% <100.00%> (ø)
src/render-act.ts 100.00% <100.00%> (ø)
src/render.tsx 100.00% <100.00%> (ø)
src/waitFor.ts 76.47% <100.00%> (+0.43%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mdjastrzebski
Copy link
Member

Released in v12.0.1 🎉

hyochan pushed a commit to dooboolab-community/react-native-iap that referenced this pull request Mar 26, 2023
….1 (#2377)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[@testing-library/react-native](https://callstack.github.io/react-native-testing-library)
([source](https://togithub.com/callstack/react-native-testing-library))
| [`12.0.0` ->
`12.0.1`](https://renovatebot.com/diffs/npm/@testing-library%2freact-native/12.0.0/12.0.1)
|
[![age](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/compatibility-slim/12.0.0)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/confidence-slim/12.0.0)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>callstack/react-native-testing-library</summary>

###
[`v12.0.1`](https://togithub.com/callstack/react-native-testing-library/releases/tag/v12.0.1)

[Compare
Source](https://togithub.com/callstack/react-native-testing-library/compare/v12.0.0...v12.0.1)

#### What's Changed

##### Bugfixes

- Flush all pending microtasks and updates before returning from waitFor
by [@&#8203;jsnajdr](https://togithub.com/jsnajdr) in
[callstack/react-native-testing-library#1366
- Render host component name detection tree inside act to avoid timer
leaks by [@&#8203;jsnajdr](https://togithub.com/jsnajdr) in
[callstack/react-native-testing-library#1371

#### New Contributors 👏

- [@&#8203;jsnajdr](https://togithub.com/jsnajdr) made their first
contributions in
[callstack/react-native-testing-library#1366,
[callstack/react-native-testing-library#1371

**Full Changelog**:
callstack/react-native-testing-library@v12.0.0...v12.0.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/dooboolab/react-native-iap).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTQuMiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
marzuq-adebayo-dev added a commit to marzuq-adebayo-dev/React-Native-iap that referenced this pull request May 20, 2023
….1 (#2377)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[@testing-library/react-native](https://callstack.github.io/react-native-testing-library)
([source](https://togithub.com/callstack/react-native-testing-library))
| [`12.0.0` ->
`12.0.1`](https://renovatebot.com/diffs/npm/@testing-library%2freact-native/12.0.0/12.0.1)
|
[![age](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/compatibility-slim/12.0.0)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/@testing-library%2freact-native/12.0.1/confidence-slim/12.0.0)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>callstack/react-native-testing-library</summary>

###
[`v12.0.1`](https://togithub.com/callstack/react-native-testing-library/releases/tag/v12.0.1)

[Compare
Source](https://togithub.com/callstack/react-native-testing-library/compare/v12.0.0...v12.0.1)

#### What's Changed

##### Bugfixes

- Flush all pending microtasks and updates before returning from waitFor
by [@&#8203;jsnajdr](https://togithub.com/jsnajdr) in
[callstack/react-native-testing-library#1366
- Render host component name detection tree inside act to avoid timer
leaks by [@&#8203;jsnajdr](https://togithub.com/jsnajdr) in
[callstack/react-native-testing-library#1371

#### New Contributors 👏

- [@&#8203;jsnajdr](https://togithub.com/jsnajdr) made their first
contributions in
[callstack/react-native-testing-library#1366,
[callstack/react-native-testing-library#1371

**Full Changelog**:
callstack/react-native-testing-library@v12.0.0...v12.0.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/dooboolab/react-native-iap).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTQuMiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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

2 participants