router icon indicating copy to clipboard operation
router copied to clipboard

fix(react-start): Do not optimizeDeps in VITEST environment

Open onlywei opened this issue 1 month ago β€’ 3 comments

Fix: Disable forced optimizeDeps in Vitest to prevent duplicate React instances

The Issue

When using @tanstack/react-start in a monorepo setup with Vitest, the plugin unconditionally adds react and react-dom to optimizeDeps.include. This forces Vite to pre-bundle React into an ESM wrapper (e.g., .vite/deps/react.js).

This causes a "Dual React Instance" conflict (leading to Invalid hook call errors) under the following common testing scenario:

  1. Workspace Packages: Code transformed by Vitest (e.g., source code in a monorepo package) resolves react to the pre-bundled ESM version forced by the plugin.
  2. External Packages: Dependencies in node_modules (e.g., @testing-library/react or UI libraries) are externalized by Vitest for performance. They use Node's native resolution, which resolves react to the standard CJS/ESM build in node_modules/react.

Because the pre-bundled React and the native React are different physical files/modules, they do not share the global ReactCurrentDispatcher. When a workspace component (using Pre-bundled React) is rendered by a test utility (using Native React), hooks fail.

The Fix

This PR updates the tanstackStart Vite plugin to skip the aggressive optimizeDeps configuration when running in Vitest (process.env.VITEST === 'true').

By disabling this enforced optimization during tests:

  1. Vitest is free to manage dependency resolution via its own defaults.
  2. Both workspace code and external dependencies resolve to the same native node_modules/react.
  3. The React instances match, and tests pass.

Verification

I verified this fix in a pnpm monorepo using set to hoist: false (strict isolation) and vitest.

  • Before: Component tests failed with TypeError: Cannot read properties of null (reading 'useState').
  • After: Tests pass successfully with no configuration workarounds needed in the consuming app.

Summary by CodeRabbit

  • Tests
    • Updated dependency pre-bundling configuration for Vitest test environments. The system now conditionally skips pre-bundling optimization when running tests, improving test execution efficiency and dependency resolution behavior. This enhances test environment handling while preserving existing configuration logic for client and server environments outside of test scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

onlywei avatar Dec 12 '25 02:12 onlywei

Walkthrough

Adds a Vitest-aware guard condition to the Vite plugin's optimizeDeps configuration in packages/react-start/src/plugin/vite.ts. When running under Vitest, optimizeDeps is conditionally skipped while preserving existing client/server-based logic for non-test environments.

Changes

Cohort / File(s) Summary
Vite plugin configuration
packages/react-start/src/plugin/vite.ts
Added Vitest-aware guard to conditionally disable optimizeDeps when VITEST environment variable is 'true'

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • TanStack/router#5650: Modifies the same optimizeDeps logic in the Vite plugin, adjusting server-side exclude list configuration
  • TanStack/router#5726: Adjusts optimizeDeps include list for react-form/react-store packages in the same Vite plugin file

Suggested labels

package: react-start

Suggested reviewers

  • schiller-manuel

Poem

🐰 A Vitest guard hops into place,
Skipping deps with grace and pace,
Tests run swift when bundling's bent,
Conditional logic, time well spent!
✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
βœ… Passed checks (2 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title accurately describes the main change: adding a Vitest-aware condition to skip optimizeDeps configuration, which directly addresses the PR's core objective of preventing dual React instances in Vitest environments.
✨ Finishing touches
  • [ ] πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Dec 12 '25 02:12 coderabbitai[bot]

View your CI Pipeline Execution β†— for commit b025a578e6b18424367eee1db7b0ad9cdc8b40a8

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... βœ… Succeeded 5m 59s View β†—
nx run-many --target=build --exclude=examples/*... βœ… Succeeded 11s View β†—

☁️ Nx Cloud last updated this comment at 2025-12-12 02:42:40 UTC

nx-cloud[bot] avatar Dec 12 '25 02:12 nx-cloud[bot]

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6074
@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@6074
@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6074
@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6074
@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6074
@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6074
@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6074
@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6074
@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6074
@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6074
@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6074
@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6074
@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6074
@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6074
@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6074
@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6074
@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6074
@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6074
@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6074
@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6074
@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@6074
@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6074
@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6074
@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6074
@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6074
@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6074
@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6074
@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6074
@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6074
@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6074
@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6074
@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6074
@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6074
@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6074
@tanstack/vue-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6074
@tanstack/vue-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6074
@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6074
@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6074
@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6074
@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6074
@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6074

commit: b025a57

pkg-pr-new[bot] avatar Dec 12 '25 02:12 pkg-pr-new[bot]