Files
dcadb11cc9 chore: add shared vitest configs and exclude react-native from default runs (#38038)
## What

Adds shared vitest configs under `common/vitestconfigs/` and excludes
react-native test directories from default test runs.

## Why

### Platform-specific testing without building

Vitest uses Vite's module resolution under the hood, which supports the
`resolve.conditions` option. By setting conditions like `["browser"]` or
`["react-native"]`, Vite resolves `#platform/*` subpath imports (from
`package.json` `imports`) to the correct platform variant **at transform
time** — no separate `tsc` build step needed.

This means:
- **No per-platform builds for testing**: Unlike `tsc`, which requires
separate tsconfig files and build passes for each platform, Vite handles
the resolution and transpilation in a single step.
- **Tests run directly against source**: Vite transforms TypeScript
source on the fly with the correct platform conditions, so tests import
the right platform code without needing `dist/` output.
- **Simpler CI**: One vitest invocation per platform (node, browser,
react-native) replaces the build-then-test dance.

### Root excludes

The root vitest excludes prevent react-native tests from accidentally
running during default node/browser test runs, which would fail since
they require RN-specific resolve conditions.

## Changes

### New shared configs (`common/vitestconfigs/`)
- `browser.config.ts` — browser vitest config with `conditions:
["browser"]`, excludes node and react-native test directories
- `react-native.config.ts` — RN vitest config with `conditions:
["react-native"]`, a `stubReactNative()` Vite plugin to shim the
`react-native` import, and excludes node and browser test directories

### Root vitest excludes
- `vitest.shared.config.ts` — added `test/**/react-native/**` to
excludes
- `vitest.browser.base.config.ts` — added `dist-test/**/react-native/**`
to excludes

## Usage

A package re-exports the shared config in its own vitest config:
```ts
export { default } from "../../../common/vitestconfigs/browser.config.ts";
```

No packages consume these configs yet — this PR just adds the
infrastructure.

Co-authored-by: Copilot <[email protected]>
2026-04-08 20:50:21 -07:00

45 lines
1.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { defineConfig, mergeConfig } from "vitest/config";
import type { Plugin } from "vite";
import base from "../../vitest.browser.base.config.ts";
// The "react-native" condition causes Vite to resolve #platform/* subpath imports
// to their RN variants. The RN userAgentPlatform imports { Platform } from "react-native",
// but the actual react-native package uses Flow syntax that Vite/esbuild cannot parse.
// This plugin stubs the "react-native" import with a minimal shim.
function stubReactNative(): Plugin {
return {
name: "stub-react-native",
enforce: "pre",
resolveId(id) {
if (id === "react-native") {
return "\0react-native-stub";
}
},
load(id) {
if (id === "\0react-native-stub") {
return `export const Platform = { OS: "test", Version: "0.0.0", constants: { reactNativeVersion: { major: 0, minor: 0, patch: 0 } } };`;
}
},
};
}
export default mergeConfig(
base,
defineConfig({
plugins: [stubReactNative()],
optimizeDeps: {
exclude: ["react-native"],
},
resolve: {
conditions: ["react-native"],
},
test: {
include: ["test/**/*.spec.ts"],
exclude: ["test/**/node/**", "test/**/browser/**", "test/snippets.spec.ts"],
},
}),
);