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

Update @testing-library/react from 13.3.0 to 16.0.1; Fixes #4685 #4686

Merged
merged 7 commits into from
Nov 28, 2024

Conversation

isqua
Copy link
Contributor

@isqua isqua commented Oct 31, 2024

Updated @testing-library/react from 13.3.0 to 16.0.1. Only the tests are affected, there is no need to release a new version.

#4685

Copy link

codesandbox bot commented Oct 31, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

codesandbox-ci bot commented Oct 31, 2024

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:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

Copy link

netlify bot commented Oct 31, 2024

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 23a1c3f
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/6748ef3cf14ca50008536492
😎 Deploy Preview https://deploy-preview-4686--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@EskiMojo14
Copy link
Collaborator

The deprecated import is from react-dom/test-utils, which we're not importing from.
Importing from React Testing Library is fine, because they already handle the deprecation.

@isqua
Copy link
Contributor Author

isqua commented Oct 31, 2024

Ah, thanks for your answer. Probably too old testing-library version is currently used because I see such deprecation warnings:

Screenshot 2024-11-01 at 00 20 16

I’ll try to update it later

@isqua isqua changed the title Import act from react instead of @testing-library/react; Fixes #4685 Update @testing-library/react from 13.3.0 to 16.0.1; Fixes #4685 Nov 1, 2024
Comment on lines -59 to +61
"@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",
Copy link
Contributor Author

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

Comment on lines -1672 to +1867
userEvent.hover(screen.getByTestId('highPriority'))
await userEvent.hover(screen.getByTestId('highPriority'))
Copy link
Contributor Author

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.

Comment on lines 49 to 50
await waitMs(150)
await waitForFakeTimer(10)
Copy link
Contributor Author

@isqua isqua Nov 1, 2024

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:

  1. userEvent.hover
  2. timer in baseQuery starts
  3. render User (isFetching true)
  4. userEvent.hover finishes
  5. timer in baseQuery finishes
  6. render User (isFetching false)

And after updating the packages, the events began to happen in the following order:

  1. userEvent.hover
  2. timer in baseQuery starts
  3. render User (isFetching true)
  4. timer in baseQuery finishes
  5. render User (isFetching false)
  6. 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.

Comment on lines -16 to +17
"@testing-library/react": "^13.3.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.1",
Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

@isqua isqua Nov 2, 2024

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?

@isqua
Copy link
Contributor Author

isqua commented Nov 23, 2024

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 src/query/tests/buildHooks.test.tsx > hooks tests > usePrefetch

  • usePrefetch respects force arg
  • usePrefetch respects ifOlderThan when it evaluates to true
  • usePrefetch executes a query even if conditions fail when the cache is empty

@@ -774,7 +775,7 @@ describe('hooks tests', () => {
resPromise = refetch()
})
expect(resPromise).toBeInstanceOf(Promise)
const res = await resPromise
const res = await act(() => resPromise)
Copy link
Contributor Author

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)

@isqua
Copy link
Contributor Author

isqua commented Nov 23, 2024

Hm, cannot reproduce the failures 🤔 Maybe I'm missing something. What I did:

  1. git clean -xdf
  2. nvm use v20.18.0
  3. yarn install
  4. run this script:
    #!/bin/bash
    
    count=0
    failures=0
    
    for i in {1..50}
    do
        if ! yarn test; then
            ((failures++))
        fi
        ((count++))
    done
    
    echo "Executed: $count times"
    echo "Failures: $failures"

What I got:

Executed: 50 times
Failures: 0

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)
Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor Author

@isqua isqua Nov 25, 2024

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 🤞

Copy link
Contributor Author

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

Copy link
Contributor Author

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)
Copy link
Contributor Author

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

@markerikson
Copy link
Collaborator

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!

@markerikson markerikson merged commit e848a55 into reduxjs:master Nov 28, 2024
40 checks passed
@isqua isqua deleted the act branch November 29, 2024 09:13
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.

4 participants