router icon indicating copy to clipboard operation
router copied to clipboard

The test does not work when rendering the router

Open dmitriybo opened this issue 1 year ago • 0 comments

Describe the bug

import {
  createRootRoute,
  createRouter,
  RouterProvider,
} from "@tanstack/react-router";
import { render, act } from "@testing-library/react";
import { test } from "vitest";

test("router", async () => {
  const router = createRouter({ routeTree: createRootRoute() });
  act(() => render(<RouterProvider router={router} />));
});

An error occurs when running the test

Warning: 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 */

How to properly test a router?

Your Example Website or App

https://codesandbox.io/p/devbox/magical-joana-mmtmlc?file=%2Ftest.test.tsx%3A13%2C1

Steps to Reproduce the Bug or Issue

  1. Go to https://codesandbox.io/p/devbox/magical-joana-mmtmlc?file=%2Ftest.test.tsx%3A13%2C1
  2. Run command yarn test (most likely you need to make a fork)

Expected behavior

No warning will occur

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

dmitriybo avatar May 23 '24 12:05 dmitriybo