Skip to content
Source
Sign inSign up
TanStack/router

An update to Transitioner inside a test was not wrapped in act(...) #6320

Closedkimsagro1(GitHub profile, opens in a new tab) opened this issue 6 comments

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

  1. Can you try doing await router.load() before rendering.

     const router = createRouter({
        routeTree,
        defaultPreloadStaleTime: 0,
        defaultPendingMinMs: 0,
        history: memoryHistory,
      });
    
      // Pre-load the router before rendering to handle redirects
      await router.load();
    
      const result = render(<RouterProvider router={router} />);
  2. mkatrenik(GitHub profile, opens in a new tab)commented

    I've similar problem. Here is modified @kimsagro1 's project. I wonder if output from validateSearch methods - 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",
    }
  3. Had the same issue and what worked for me is wrapping render function with act.

    Something like this:

    let result;
    
    await act(async () => {
        result = render(<RouterProvider router={router} />);
    });

    Apparently your route has beforeLoad defined, which blocks router from rendering the route until it resolves the beforeLoad function. The same will be with the loader functions 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. But await before render() is redundant since it is synchronous function.

    Hope this helps :)

  4. @brenelz tried with latest @tanstack/react-router and errors are gone

Sign in to join this conversation.