Skip to content

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

TypeScript

perf(test): split vitest into ui (jsdom) and node projects (#3814)

sangwook82b47b66941e
diff --git a/packages/cli/src/commands/build-theme.variants.test.mjs b/packages/cli/src/commands/build-theme.variants.test.mjs
index 54403db92f..479d52d67b 100644
--- a/packages/cli/src/commands/build-theme.variants.test.mjs
+++ b/packages/cli/src/commands/build-theme.variants.test.mjs
@@ -28,20 +28,10 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import {fileURLToPath} from 'node:url';
+import {ensureCoreBuilt} from './ensure-core-built.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const CLI_BIN = path.resolve(__dirname, '../../bin/astryx.mjs');
-const REPO_ROOT = path.resolve(__dirname, '../../../..');
-const CORE_THEME_ENTRY = path.join(
- REPO_ROOT,
- 'packages/core/dist/theme/index.js',
-);
-// The fix reads core's shipped component declarations to decide whether an
-// interface is augmentable; those .d.ts files come from the same core build.
-const CORE_BUTTON_DTS = path.join(
- REPO_ROOT,
- 'packages/core/dist/Button/index.d.ts',
-);
function runCli(args, cwd) {
try {
@@ -68,14 +58,13 @@ function writeTheme(dir, contents) {
return file;
}
+// Build core through the shared lock helper — this suite previously ran its
+// own unguarded `if (!exists) pnpm -F core build`, and when Vitest scheduled
+// it alongside the other build-theme suites on a fresh checkout, the
+// concurrent builds collided on packages/core/dist (core's build starts by
+// wiping dist), nondeterministically breaking whichever suite was mid-read.
beforeAll(() => {
- if (!fs.existsSync(CORE_THEME_ENTRY) || !fs.existsSync(CORE_BUTTON_DTS)) {
- execFileSync('pnpm', ['-F', '@astryxdesign/core', 'build'], {
- cwd: REPO_ROOT,
- stdio: 'pipe',
- timeout: 180_000,
- });
- }
+ ensureCoreBuilt();
}, 200_000);
let tmpDir;
diff --git a/vitest.config.ts b/vitest.config.ts
index ab4ddffbd5..31ea0abdf6 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -4,7 +4,12 @@
* @file vitest.config.ts
* @input Uses vitest/config, @vitejs/plugin-react
* @output Vitest configuration with jsdom, coverage, and test setup
- * @position Root test config; applies to all packages in monorepo
+ * @position Root test config; extended by the `ui` project in
+ * vitest.workspace.ts, which is the actual test entry point. Which files
+ * run where is decided by the per-project include lists in the workspace
+ * file — deliberately NOT here: workspace `extends` MERGES arrays, so an
+ * include list in this file would leak into every extending project and
+ * double-run files.
*
* SYNC: When modified, update this header and root README.md
*/
@@ -59,11 +64,6 @@ export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
- include: [
- 'packages/**/src/**/*.test.{ts,tsx,mjs}',
- 'internal/**/*.test.{ts,tsx,mjs}',
- 'scripts/**/*.test.{ts,tsx,mjs}',
- ],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
diff --git a/vitest.workspace.ts b/vitest.workspace.ts
new file mode 100644
index 0000000000..fe3168e700
--- /dev/null
+++ b/vitest.workspace.ts
@@ -0,0 +1,65 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file vitest.workspace.ts
+ * @input Uses vitest/config defineWorkspace, extends ./vitest.config.ts
+ * @output Two test projects: `ui` (jsdom + StyleX babel) and `node` (plain node)
+ * @position Test entry point; when this file exists, `vitest run` executes the
+ * projects below instead of the root config's include list.
+ *
+ * Why two projects: ~105 of the ~316 test files (packages/cli, scripts,
+ * internal tooling) never touch the DOM, but the single root config gave every
+ * file a fresh jsdom environment plus the React/StyleX babel transform —
+ * pure per-file overhead for node code. Splitting lets the node project skip
+ * both.
+ *
+ * Partitioning rule (nothing can fall through):
+ * - `ui` = packages/core + packages/lab (explicit list)
+ * - `node`= everything else the root include matches (same globs, with
+ * core/lab excluded)
+ * A new package lands in `node` by default; if its tests need the DOM they
+ * fail loudly there — move the package into the `ui` include list.
+ *
+ * SYNC: When modified, update this header. Keep the `ui` include list and the
+ * `node` exclude list in lockstep.
+ */
+
+import {configDefaults, defineWorkspace} from 'vitest/config';
+
+export default defineWorkspace([
+ // UI packages — need jsdom, the StyleX babel transform, and the jest-dom
+ // setup file. Inherits all of that from the root config.
+ {
+ extends: './vitest.config.ts',
+ test: {
+ name: 'ui',
+ include: [
+ 'packages/core/src/**/*.test.{ts,tsx,mjs}',
+ 'packages/lab/src/**/*.test.{ts,tsx,mjs}',
+ ],
+ },
+ },
+ // Node-only code (CLI, build tooling, scripts, internal utils) — no DOM, no
+ // StyleX/babel transform, no jest-dom matchers. Deliberately does NOT extend
+ // the root config: a plain node environment skips the per-file jsdom
+ // instantiation that dominated these files' runtime.
+ // forks pool (vitest default) is required here: several CLI tests call
+ // process.chdir(), which worker threads do not support.
+ {
+ test: {
+ name: 'node',
+ globals: true,
+ environment: 'node',
+ include: [
+ 'packages/**/src/**/*.test.{ts,tsx,mjs}',
+ 'internal/**/*.test.{ts,tsx,mjs}',
+ 'scripts/**/*.test.{ts,tsx,mjs}',
+ ],
+ exclude: [
+ ...configDefaults.exclude,
+ 'packages/core/**',
+ 'packages/lab/**',
+ ],
+ },
+ },
+]);