An update to Transitioner inside a test was not wrapped in act(...) #6320
Which project does this relate to?#
Router
Describe the bug#
I have a route that throws a redirect
import { createFileRoute, redirect } from '@tanstack/react-router';
export const Route = createFileRoute('/restricted')({
beforeLoad: () => {
throw redirect({ to: '/' });
},
component: RouteComponent,
});
function RouteComponent() {
return <div>Hello "/restricted"!</div>;
}
And then testing that the redirect occurs using vitest browser mode
test('restricted page redirects', async () => {
const { router } = await renderRoute('/restricted');
expect(router.history.location.pathname).toBe('/');
});
async function renderRoute(path: keyof FileRoutesByFullPath) {
const memoryHistory = createMemoryHistory({
initialEntries: [path],
});
const router = createRouter({
routeTree,
defaultPreloadStaleTime: 0,
defaultPendingMinMs: 0,
history: memoryHistory,
});
const result = await render(<RouterProvider router={router} />);
return {
...result,
router,
};
}
The test succeeds but logs the following to the console
An update to Transitioner 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://react.dev/link/wrap-tests-with-act
This only happens when the route throws a redirect, if I test another route without the redirect, then the test passes without any console logs
test('about page renders', async () => {
await renderRoute('/about');
await expect.element(page.getByText('Hello from About!')).toBeVisible();
});
I have also tried wrapping the test codes in an act and various combination of wrapping the code in waitUntil etc, but without success.
I have included a stackblitz that reproduced the issue.
Your Example Website or App#
https://stackblitz.com/edit/vitejs-vite-o3senmym
Steps to Reproduce the Bug or Issue#
Launch the example stackblitz, you will see the logs in the terminal
Expected behavior#
The tests passing without the act warnings
Screenshots or Videos#
No response
Platform#
See example stackblitz
Additional context#
I'm not exactly sure whether the issue lies with tanstack router or vitest
@brenelz I've added, but still get the same issue
see: https://stackblitz.com/edit/vitejs-vite-cxx37rgz?file=src%2F__tests__%2Fhelpers.tsx
I've similar problem. Here is modified @kimsagro1 's project. I wonder if output from
validateSearchmethods - particularly that empty{}on line 11 for no reason, have something to to do with it? (triggering redirect?)stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", } stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", } stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: {} stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", } stderr | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects An update to Transitioner 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://react.dev/link/wrap-tests-with-act stderr | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects An update to Transitioner 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://react.dev/link/wrap-tests-with-act stderr | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects An update to Transitioner 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://react.dev/link/wrap-tests-with-act stderr | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects An update to Transitioner 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://react.dev/link/wrap-tests-with-act stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", } stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", } stdout | src/__tests__/restricted.test.tsx:4:1 > restricted page redirects Validating search params for /restricted: { "q": "hello", }Had the same issue and what worked for me is wrapping
renderfunction withact.Something like this:
let result; await act(async () => { result = render(<RouterProvider router={router} />); });Apparently your route has
beforeLoaddefined, which blocks router from rendering the route until it resolves thebeforeLoadfunction. The same will be with theloaderfunctions if there are any. So in such cases you just need to wrap the render call with act().Calling
await render.load();before such render is also a good idea. Butawaitbefore render() is redundant since it is synchronous function.Hope this helps :)
is this still an issue?
@brenelz tried with latest @tanstack/react-router and errors are gone
brenelz(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Can you try doing
await router.load()before rendering.