Skip to content

Vitest fails with react-start in a pnpm workspace monorepo #6246

@onlywei

Description

@onlywei

Which project does this relate to?

Start

Describe the bug

When using TanStack Start with Vitest in a pnpm workspace, tests fail with:

TypeError: Cannot read properties of null (reading 'useState')

This occurs when testing components that are imported from workspace packages that have React as a peer dependency.

Root Cause: The TanStack Start Vite plugin applies optimizeDeps configuration unconditionally, including in the Vitest test environment. This interferes with module resolution, causing React to be resolved as null when importing components from workspace packages.

Impact: This bug affects all pnpm workspace configurations (regardless of hoisting settings), making it impossible to use Vitest for testing TanStack Start applications that import components from local workspace packages. This is a common pattern in monorepo setups.

// This errors:
plugins: [tanstackStart(), viteReact()]
// This does not error:
plugins: [
  process.env.VITEST !== 'true' && tanstackStart(), 
  viteReact(),
]

Your Example Website or App

https://github.com/onlywei/repro-tanstack-start-vitest

Steps to Reproduce the Bug or Issue

  1. Clone the repro: https://github.com/onlywei/repro-tanstack-start-vitest
  2. Run pnpm install
  3. Run pnpm test

Expected behavior

Tests should pass without errors. Components imported from workspace packages should be able to use React hooks normally during Vitest test execution.

The TanStack Start plugin should not interfere with Vitest's module resolution. Ideally, the plugin should automatically detect when running in a test environment and skip optimization steps that are not needed for testing.

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.145.0
  • OS: MacOS
  • Vite Version: 7.3.0
  • Vitest version: 4.0.16

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions