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

[8.x] [React18] Migrate test suites to account for testing library upgrades security-threat-hunting-investigations (#201145) #201859

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

eokoneyo
Copy link
Contributor

Backport

This will backport the following commits from main to 8.x:

Questions ?

Please refer to the Backport tool documentation

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-threat-hunting-investigations (Team:Threat Hunting:Investigations)

@eokoneyo
Copy link
Contributor Author

@elasticmachine merge upstream

1 similar comment
@eokoneyo
Copy link
Contributor Author

eokoneyo commented Dec 2, 2024

@elasticmachine merge upstream

… security-threat-hunting-investigations (elastic#201145)

This PR migrates test suites that use `renderHook` from the library
`@testing-library/react-hooks` to adopt the equivalent and replacement
of `renderHook` from the export that is now available from
`@testing-library/react`. This work is required for the planned
migration to react18.

In this PR, usages of `waitForNextUpdate` that previously could have
been destructured from `renderHook` are now been replaced with `waitFor`
exported from `@testing-library/react`, furthermore `waitFor`
that would also have been destructured from the same renderHook result
is now been replaced with `waitFor` from the export of
`@testing-library/react`.

***Why is `waitFor` a sufficient enough replacement for
`waitForNextUpdate`, and better for testing values subject to async
computations?***

WaitFor will retry the provided callback if an error is returned, till
the configured timeout elapses. By default the retry interval is `50ms`
with a timeout value of `1000ms` that
effectively translates to at least 20 retries for assertions placed
within waitFor. See
https://testing-library.com/docs/dom-testing-library/api-async/#waitfor
for more information.
This however means that for person's writing tests, said person has to
be explicit about expectations that describe the internal state of the
hook being tested.
This implies checking for instance when a react query hook is being
rendered, there's an assertion that said hook isn't loading anymore.

In this PR you'd notice that this pattern has been adopted, with most
existing assertions following an invocation of `waitForNextUpdate` being
placed within a `waitFor`
invocation. In some cases the replacement is simply a `waitFor(() => new
Promise((resolve) => resolve(null)))` (many thanks to @kapral18, for
point out exactly why this works),
where this suffices the assertions that follow aren't placed within a
waitFor so this PR doesn't get larger than it needs to be.

It's also worth pointing out this PR might also contain changes to test
and application code to improve said existing test.

1. Review the changes in this PR.
2. If you think the changes are correct, approve the PR.

If you have any questions or need help with this PR, please leave
comments in this PR.

(cherry picked from commit 06de5f3)
@eokoneyo eokoneyo force-pushed the backport/8.x/pr-201145 branch from 44d4f22 to a779c61 Compare December 5, 2024 13:16
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

cc @eokoneyo

Copy link
Member

@tsullivan tsullivan left a comment

Choose a reason for hiding this comment

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

LGTM

compared the diff to #201145 and see nothing significant

@eokoneyo eokoneyo merged commit 62f28c4 into elastic:8.x Dec 5, 2024
8 checks passed
@eokoneyo eokoneyo deleted the backport/8.x/pr-201145 branch December 5, 2024 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport Team:Threat Hunting:Investigations Security Solution Investigations Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants