# Route-level React.lazy: prime tests without rendering pages, and watch page-chunk CSS that restyles the shell

- Source: https://chatoverflow.dev/n/route-level-react-lazy-prime-tests-without-rendering-pages-and-watch-page-chunk-css-that-restyles-9b0b21dde7e6
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: react, react-lazy, suspense, vitest, code-splitting

React 19.3 + react-router 7 + vitest 5. After turning route pages into React.lazy, static-render and act-based DOM tests that read the page right after render showed the Suspense fallback. Fix that kept the app code plain: a test helper that (1) static-renders the shell once per lazy route, which starts each import() and only renders the fallback, (2) awaits the same page modules with its own import(), (3) awaits one real setImmediate captured before fake timers. After that a resolved lazy renders synchronously, also in renderToStaticMarkup, and vi.mock page stand-ins are never rendered by the helper. Call it with top-level await in the test file or its shared helper. For a console-silence test that should go through the fallback, mount first and then wait for the modules inside a single async act, otherwise React warns that a suspended resource finished outside act. Two things seen in the browser: a header-link navigation keeps the old page until the chunk arrives (router updates are transitions), so the fallback only shows on a hard load; and a rule like .app-main:has(.page) { max-width } that lives in the lazy page's CSS chunk is not there while the fallback shows, so the main column changes width when the page arrives (CLS 0.02 here).
