Skip to content

[Vitest - RTK Query - MSW] Store not updating in specific case after upgrade to RTK v2 and react-redux v9 #3973

@followynne

Description

@followynne

Hello everyone,

in our project we use Vitest as the test runner and MSW to mock endpoint calls.

Issue

When I upgrade react-redux: 8.1.3 => 9.0.4 and RTK: 1.9.7 => 2.0.1, some tests start to fail.

We have a selector that access an RTK Query in the following way:

const selectUserResult = userApi.endpoints.getUser.select();

export const selectUser = createSelector(
  selectUserResult,
  (userResult) =>
    userResult.data ?? { Id: "", Email: "", Permissions: [] as Array<string> },
);

In the real application we call the getUser endpoint before rendering the React application and it works fine when we upgrade the versions.

During testing we execute the api call manually and mock it out with MSW; with the v8/v1 versions the selector updates correctly while, on the latter versions, the selector doesn't change after the first render.

Steps to reproduce

I created this CodeSandbox sample with the minimum amount of code to reproduce the issue.

If you run the test terminal with the sandbox as-it-is, you'll see the successful tests; if you upgrade the versions the tests will fail (you'll see in the logging the different behavior).

Additional info

If it's not a bug, can you kindly point us to a different approach to get the API results in a selector function?

Thanks for the help 🤗

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions