Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
12.1 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
 * @file vitest.config.ts
 * @input Uses vitest/config, @vitejs/plugin-react
 * @output Vitest configuration with jsdom, coverage, test setup, the three
 *   test projects (`ui`, `perf`, `node`), and the sequencer that splits
 *   `--shard` legs
 * @position Root test config and the actual test entry point. The projects
 *   under `test.projects` decide which files run where via their per-project
 *   include lists. The root-level `test` options (globals, environment,
 *   coverage, setupFiles, pool sizing) are inherited by projects that set
 *   `extends: true`; the `node` project deliberately does NOT extend, so it
 *   runs in a plain node environment without the jsdom/StyleX overhead.
 *
 *   (Migrated from the removed `vitest.workspace.ts` for Vitest 4, which
 *   dropped the standalone workspace file in favor of inline `test.projects`.)
 *
 * SYNC: When modified, update this header and root README.md
 */

import path from 'node:path';
import {configDefaults, defineConfig} from 'vitest/config';
import react from '@vitejs/plugin-react';
import stylexResolution from './scripts/stylex-module-resolution.cjs';
import DurationShardSequencer from './scripts/vitest-shard-sequencer.mjs';

const rootDir = path.resolve(__dirname, '.');
const coreSrc = path.resolve(__dirname, 'packages/core/src');
// Some Node 25 releases leave the negative spelling out of this list, so the
// check reads the positive one.
const webStorageOff = process.allowedNodeEnvironmentFlags.has(
  '--experimental-webstorage',
)
  ? ['--no-experimental-webstorage']
  : [];
// Times the Markdown helper pipeline against the empty one on the CPU clock.
// CPU time still includes cache and memory-bandwidth contention from other
// work, so this file runs alone in the `perf` project.
const helperOverheadBenchmark =
  'packages/core/src/Markdown/Markdown.fr23.perf.test.ts';

export default defineConfig({
  plugins: [
    react({
      babel: {
        plugins: [
          [
            '@stylexjs/babel-plugin',
            {
              dev: true,
              enableDebugDataProp: false,
              // StyleX's runtime injection, plus each rule its dev runtime drops
              // under a prelude with more than one condition (StyleX 0.19.0).
              runtimeInjection: path.join(
                rootDir,
                'internal/test-utils/src/stylexInject.ts',
              ),
              genConditionalClasses: true,
              treeshakeCompensation: true,
              aliases: {
                '@astryxdesign/core/*': [
                  path.join(rootDir, 'packages/core/src/*'),
                ],
                '@astryxdesign/core': [path.join(rootDir, 'packages/core/src')],
              },
              unstable_moduleResolution:
                stylexResolution.stylexModuleResolution,
            },
          ],
        ],
      },
    }),
  ],
  resolve: {
    alias: [
      // Map @astryxdesign/core subpath imports to source for lab package tests.
      // Must use regex to match subpaths like @astryxdesign/core/Dialog, @astryxdesign/core/theme/tokens.stylex
      // while not breaking core's own relative imports.
      {
        find: /^@astryxdesign\/core\/(.*)$/,
        replacement: path.join(coreSrc, '$1'),
      },
      // Map the bare specifier to source too (charts package tests import
      // runtime components like Text/VisuallyHidden from the package root).
      {
        find: /^@astryxdesign\/core$/,
        replacement: path.join(coreSrc, 'index.ts'),
      },
    ],
  },
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      include: ['packages/**/src/**/*.{ts,tsx}'],
      exclude: ['**/*.test.{ts,tsx}', '**/*.stories.{ts,tsx}', '**/index.ts'],
    },
    setupFiles: ['./internal/test-utils/src/setup.ts'],
    globalSetup: ['./internal/test-utils/src/globalSetup.ts'],
    // Increase worker heap to prevent OOM crashes on memory-heavy test files
    // (e.g. Chat composer tests with contentEditable + popover portals in
    // jsdom). Vitest 4 removed `poolOptions`; per-worker argv is now top-level.
    // Node 25 and later define their own `localStorage` global, which hides
    // jsdom's; a Node that knows the flag turns its own off in the workers.
    execArgv: ['--max-old-space-size=4096', ...webStorageOff],
    // A `--shard` leg of the `node` project runs files chosen by their
    // recorded time, not by count, so the slowest files never share a leg
    // (scripts/vitest-shard-sequencer.mjs, spec:AST-030 FR22).
    sequence: {sequencer: DurationShardSequencer},
    // Test projects (migrated from vitest.workspace.ts). Partitioning rule
    // (nothing can fall through):
    //   - `ui`   = packages/core + packages/lab + packages/charts + packages/richtext
    //              + packages/vega and the Theme Showcase DOM suite — need
    //              jsdom, the StyleX babel
    //              transform, and the jest-dom setup; inherit all of that from
    //              the root config via `extends: true`.
    //   - `perf` = the helper-overhead benchmark, which `ui` excludes. It
    //              inherits the root config too, and runs in a later group.
    //   - `node` = everything else (CLI, build tooling, scripts, internal
    //              utils, and app-level suites that need no DOM) — no DOM, no
    //              StyleX/babel transform, no jest-dom
    //              matchers. Deliberately does NOT extend the root config so it
    //              runs in a plain node environment and skips the per-file jsdom
    //              instantiation that dominated these files' runtime. A new
    //              package lands in `node` by default; if its tests need the DOM
    //              they fail loudly there — move it into the `ui` include list.
    //
    // An app or package that carries its OWN vitest config is invisible to both
    // projects, so its tests belong to no CI job: `pnpm test` runs this file and
    // nothing else. Add the suite to an include list here instead of giving it a
    // second config to be run by.
    projects: [
      {
        extends: true,
        test: {
          name: 'ui',
          // A jsdom component suite can cost ~1.8s p95 per test where a
          // comparable file costs ~80ms, so the 5s default leaves almost no
          // headroom: on a busy machine slow-but-correct runs flake
          // non-deterministically (every failure was "timed out in 5000ms",
          // never a wrong value). Same budget as `node`, for the same reason.
          testTimeout: 30_000,
          hookTimeout: 30_000,
          include: [
            'packages/core/src/**/*.test.{ts,tsx,mjs}',
            'packages/lab/src/**/*.test.{ts,tsx,mjs}',
            'packages/charts/src/**/*.test.{ts,tsx,mjs}',
            'packages/richtext/src/**/*.test.{ts,tsx,mjs}',
            'packages/vega/src/**/*.test.{ts,tsx,mjs}',
            'apps/docsite/src/__tests__/theme-showcase/**/*.test.{ts,tsx,mjs}',
          ],
          exclude: [...configDefaults.exclude, helperOverheadBenchmark],
        },
      },
      {
        extends: true,
        test: {
          name: 'perf',
          // Groups run in order, so this one starts only after the other
          // projects' files finish.
          sequence: {groupOrder: 1},
          testTimeout: 30_000,
          hookTimeout: 30_000,
          include: [helperOverheadBenchmark],
        },
      },
      {
        // forks pool (vitest default) is required here: several CLI tests call
        // process.chdir(), which worker threads do not support.
        // This project does NOT extend the root config, so it carries its own
        // `resolve` — the root's aliases do not reach it.
        resolve: {
          alias: [
            // Catalog templates import docs-only demo content from the
            // docsite. The docsite tsconfig maps the same specifier.
            {
              find: /^@astryx-docs\/demo-content$/,
              replacement: path.join(
                rootDir,
                'apps/docsite/src/templateDemoContent/demoContent.tsx',
              ),
            },
            // Theme packages resolve to `dist/`, which no test run builds. The
            // sandbox palette suite reads their palettes as a reference corpus,
            // so point the BARE specifier at the authored source. Subpath
            // imports (`/built`, `/theme.css`) are untouched and still need a
            // real build.
            {
              find: /^@astryxdesign\/theme-([a-z0-9-]+)$/,
              replacement: path.join(
                rootDir,
                'packages/themes/$1/src/source.ts',
              ),
            },
          ],
        },
        test: {
          name: 'node',
          globals: true,
          environment: 'node',
          // Prebuilt and plain-data modules load as Node loads them for the
          // CLI, not through Vite's transform, which would otherwise inline
          // every file: Core's built bundle (the workspace link resolves it
          // outside node_modules; the theme build imports its root entry, all
          // of Core and React: 0.9 s natively, 4.5 s inlined) and the
          // `.doc.mjs` documentation modules the docs catalog imports by the
          // hundred (doctor's first case: 0.7 s natively, 4.7 s inlined).
          // Both grow several times over on a loaded box. Mocks keep working
          // from inlined callers. The CLI's doc importers import doc files
          // through `import-url.mjs`, which loads natively too, so its imports
          // make no request to Vitest's main process. Each request waits in
          // line behind every worker: `template --list` imports 771 doc files,
          // 0.6 s alone and up to 26 s on a loaded box.
          server: {
            deps: {
              external: [
                /\/packages\/core\/dist\//,
                /\.doc\.mjs$/,
                /\/packages\/cli\/foundation\/fs\/import-url\.mjs$/,
              ],
            },
          },
          // Several CLI suites spawn a fresh `node bin/astryx.mjs` per assertion
          // (real process boundary). Under the forks pool these cold-starts can
          // run long on a busy box; the 5s default testTimeout then flakes
          // non-deterministically (every failure was "timed out in 5000ms").
          // Give spawn-backed tests + their fixture hooks a real budget so a
          // slow-but-correct run never trips the timeout. (The other half of the
          // fix was removing a full `pnpm build` that scripts/build-css.test ran
          // in-suite, which hogged every core and starved these — see that file.)
          testTimeout: 30_000,
          hookTimeout: 30_000,
          // Build @astryxdesign/core once before workers fork. The build-theme
          // suites need a compiled core; doing it here (not per-suite in
          // parallel workers) avoids concurrent clean-and-build collisions that
          // flake under Vitest 4's reworked pool.
          globalSetup: ['./vitest.global-setup.node.mjs'],
          include: [
            'packages/**/src/**/*.test.{ts,tsx,mjs}',
            // The CLI dissolved its src/ wrapper (pillars live at the package
            // root), so collect its colocated tests wherever they now live.
            'packages/cli/**/*.test.{ts,tsx,mjs}',
            'internal/**/*.test.{ts,tsx,mjs}',
            'scripts/**/*.test.{ts,tsx,mjs}',
            '.github/scripts/**/*.test.{ts,tsx,mjs}',
            // Storybook config invariants (no DOM needed) — e.g. the
            // workspace source-alias guard in .storybook/main.test.ts.
            'apps/storybook/.storybook/**/*.test.{ts,tsx,mjs}',
            // Sandbox page logic (palette generation, colour maths) — pure
            // modules with no DOM, so they run here rather than in `ui`.
            'apps/sandbox/src/**/*.test.{ts,tsx,mjs}',
          ],
          exclude: [
            ...configDefaults.exclude,
            'packages/core/**',
            'packages/lab/**',
            'packages/charts/**',
            'packages/richtext/**',
            'packages/vega/**',
          ],
        },
      },
    ],
  },
});