-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Update @testing-library/react from 13.3.0 to 16.0.1; Fixes #4685 #4686
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
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 23a1c3f:
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
The deprecated import is from react-dom/test-utils, which we're not importing from. |
"@testing-library/react": "^13.3.0", | ||
"@testing-library/user-event": "^13.1.5", | ||
"@testing-library/dom": "^10.4.0", | ||
"@testing-library/react": "^16.0.1", | ||
"@testing-library/user-event": "^14.5.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated @testing-library/react
to the latest version.
Changelogs for major updates: v14.0.0, v15.0.0, v16.0.0.
Since the version v16.0.0, @testing-library/dom
was moved to a peer dependency, so we need to explicitly install it.
Updated @testing-library/user-event
to the latest version. Major changes: v14.0.0
userEvent.hover(screen.getByTestId('highPriority')) | ||
await userEvent.hover(screen.getByTestId('highPriority')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Release v14.0.0 · testing-library/user-event userEvent.hover
returns a promise, so we need to await here.
await waitMs(150) | ||
await waitForFakeTimer(10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After upgrading dependencies, all the tests for usePrefetch
started to fail. In the master branch, events occur in the following order:
userEvent.hover
- timer in
baseQuery
starts - render User (
isFetching true
) userEvent.hover
finishes- timer in
baseQuery
finishes - render User (
isFetching false
)
And after updating the packages, the events began to happen in the following order:
userEvent.hover
- timer in
baseQuery
starts - render User (
isFetching true
) - timer in
baseQuery
finishes - render User (
isFetching false
) userEvent.hover
finishes
As I noticed, waitMs waits synchronously (while loop), and then resolve the promise in a next tick. So as I understand, after updating libraries, we wait for task queue to be empty after rendering.
So I replaced waitMs
with waitForFakeTimer, and tests for usePrefetch
was fixed.
However, other tests that relied on the waitMs
implementation, failed. So I replaced waitMs
with waitForFakeTimer
there too.
"@testing-library/react": "^13.3.0", | ||
"@testing-library/dom": "^10.4.0", | ||
"@testing-library/react": "^16.0.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I didn’t get how to run tests in examples, and how to check if everything is OK.
When I run yarn test
in the master branch, the tests run only for the following packages:
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
And when I go to examples/query/react/basic
and run npm test
there, I get the following error:
> @examples-query-react/[email protected] test
> react-scripts test --runInBand
TypeError: Cannot read properties of undefined (reading 'EXTENSION')
at Runtime.createHasteMap (rtk/node_modules/jest-runtime/build/index.js:547:43)
at rtk/node_modules/@jest/core/build/cli/index.js:231:55
at Array.map (<anonymous>)
at buildContextsAndHasteMaps (rtk/node_modules/@jest/core/build/cli/index.js:228:13)
at _run10000 (rtk/node_modules/@jest/core/build/cli/index.js:305:47)
at runCLI (rtk/node_modules/@jest/core/build/cli/index.js:173:9)
at async Object.run (rtk/node_modules/jest-cli/build/cli/index.js:155:37)
npm error Lifecycle script `test` failed with error:
npm error code 1
npm error path rtk/examples/query/react/basic
npm error workspace @examples-query-react/[email protected]
npm error location rtk/examples/query/react/basic
npm error command failed
npm error command sh -c react-scripts test --runInBand
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be fixed by #4603.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I correct something in this regard in the current pull request? Do you wanna merge #4603 first?
I rebased the branch from the master. Before the rebase there was a failed CI Job Test against dist (20.x) with following tests failed for
|
@@ -774,7 +775,7 @@ describe('hooks tests', () => { | |||
resPromise = refetch() | |||
}) | |||
expect(resPromise).toBeInstanceOf(Promise) | |||
const res = await resPromise | |||
const res = await act(() => resPromise) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this warning:
Warning: An update to TestComponent inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at TestComponent (redux-toolkit/node_modules/@testing-library/react/dist/pure.js:307:5)
at Provider (redux-toolkit/node_modules/react-redux/dist/react-redux.mjs:1036:3)
at Wrapper (redux-toolkit/packages/toolkit/src/tests/utils/helpers.tsx:39:29)
Hm, cannot reproduce the failures 🤔 Maybe I'm missing something. What I did:
What I got:
It’s on macos, but I don’t think it matters |
@@ -46,7 +47,7 @@ interface Item { | |||
|
|||
const api = createApi({ | |||
baseQuery: async (arg: any) => { | |||
await waitMs(150) | |||
await waitForFakeTimer(20) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, the timeout was 150ms, and I reduced it to 10 to make tests faster, I experimentally chose this value running tests on my laptop. In the latest push I enlarged it to 20, let’s see if it helped with tests in CI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markerikson could you start the CI again please? If it fails, I’ll put the 150ms back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I see. Pushed back the value of 150ms 🤞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markerikson could you please run it again? I hope the problem were in the timing, so I rolled it back to 150ms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks like the check is green now.
act(() => { | ||
fireEvent.focus(window) | ||
}) | ||
fireEvent.focus(window) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fireEvent is already wrapped in the act function: Common mistakes with React Testing Library
Okay, this is green, and I've cleaned up the last couple stray console messages / errors that were getting logged (which already existed prior to this PR). Thank you for working on this! |
Updated
@testing-library/react
from13.3.0
to16.0.1
. Only the tests are affected, there is no need to release a new version.#4685