@ctrl/dom 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +160 -9
  2. package/dist/aria/_chunks/aria-query.generated-BivoZ-ec.js +4 -0
  3. package/dist/aria/_chunks/aria-query.generated-CeGmXXFh.cjs +4 -0
  4. package/dist/aria/index.cjs +1 -1
  5. package/dist/aria/index.js +1 -1
  6. package/dist/aria/internal.cjs +1 -1
  7. package/dist/aria/internal.js +1 -1
  8. package/dist/aria/types/data/aria-query.generated.d.ts +1 -0
  9. package/dist/aria/types/internal.d.ts +3 -1
  10. package/dist/aria/types/role.d.ts +2 -8
  11. package/dist/bridge/internal.js +1 -1
  12. package/dist/dom/_chunks/{src-CImID776.js → src-DR6mBc_4.js} +13 -12
  13. package/dist/dom/index.js +1 -1
  14. package/dist/dom/internal.js +1 -1
  15. package/dist/dom/types/css/declarations.d.ts +4 -1
  16. package/dist/dom/types/nodes/attributes.d.ts +1 -1
  17. package/dist/dom/types/nodes/cache.d.ts +1 -3
  18. package/dist/dom/types/nodes/constants.d.ts +2 -0
  19. package/dist/dom/types/window/clock.d.ts +3 -1
  20. package/dist/react/_chunks/pure-B2u-ZqTR.js +1 -0
  21. package/dist/react/_chunks/pure-DCcqflDh.cjs +1 -0
  22. package/dist/react/index.cjs +1 -1
  23. package/dist/react/index.js +1 -1
  24. package/dist/react/pure.cjs +1 -1
  25. package/dist/react/pure.js +1 -1
  26. package/dist/testing-library/index.cjs +4 -4
  27. package/dist/testing-library/index.js +8 -8
  28. package/dist/user-event/index.cjs +10 -10
  29. package/dist/user-event/index.js +12 -12
  30. package/dist/user-event/types/event/selection/getTargetTypeAndSelection.d.ts +2 -1
  31. package/dist/user-event/types/utils/keyDef/readNextDescriptor.d.ts +8 -1
  32. package/dist/vitest-environment/plugin.js +1 -1
  33. package/dist/vitest-environment/prewarm.js +1 -0
  34. package/dist/vitest-environment/types/plugin.d.ts +20 -0
  35. package/dist/vitest-environment/types/prewarm.d.ts +1 -0
  36. package/dist/vitest-environment/types/warm-forks.d.ts +60 -0
  37. package/package.json +5 -1
  38. package/dist/aria/_chunks/roles-list-CtWrIYOt.cjs +0 -4
  39. package/dist/aria/_chunks/roles-list-D5_w80vQ.js +0 -4
  40. package/dist/react/_chunks/pure-CYb0HgYU.js +0 -1
  41. package/dist/react/_chunks/pure-DRugbA0j.cjs +0 -1
package/README.md CHANGED
@@ -36,14 +36,107 @@ pnpm add -D @ctrl/dom
36
36
  import { defineConfig } from "vitest/config";
37
37
  import { ctrlDom } from "@ctrl/dom/vitest";
38
38
 
39
- export default defineConfig({
40
- plugins: [ctrlDom()],
41
- // Optional and faster: each test file gets a fresh context in a reused worker instead of a
42
- // new worker, so per-file setup drops from about 23 ms to about 2 ms.
43
- test: { pool: "vmThreads" },
44
- });
39
+ export default defineConfig({ plugins: [ctrlDom()] });
40
+ ```
41
+
42
+ That is the whole setup: every test file still runs in its own fresh process, as with Vitest's
43
+ defaults. With hundreds of test files, Vitest's `pool` setting can more than halve the run:
44
+ [Faster runs](#faster-runs).
45
+
46
+ DOM only (stock Testing Library packages keep working):
47
+
48
+ ```ts
49
+ export default defineConfig({ plugins: [ctrlDom({ testingLibrary: false })] });
50
+ ```
51
+
52
+ Options go to the plugin: `ctrlDom({ url: "http://localhost:3000/", width: 1280 })`. With the
53
+ plugin installed, a file can also pick the environment itself: `// @vitest-environment ctrl-dom`.
54
+ An `environment` you set in the config other than `"ctrl-dom"` stays the project's default.
55
+
56
+ The plugin also looks after Vitest's workers, since the default pool starts one per test file:
57
+ with its Testing Library it turns Node's compile cache on as each worker starts (so Vitest's own
58
+ worker modules load from it too, not only the DOM), and it starts the next files' workers ahead
59
+ of time ([warm workers](#warm-workers)).
60
+
61
+ ### Faster runs
62
+
63
+ `ctrlDom()` alone is the safe setup: a new process for every test file, as Vitest does by default.
64
+ Past that, how fast a suite runs is mostly a Vitest question, and Vitest's own guide is the place
65
+ to start: [Improving Performance](https://vitest.dev/guide/improving-performance). What in it
66
+ matters most for component tests:
67
+
68
+ - **Find out where the time goes first.** Vitest prints the split at the end of a run
69
+ (`Duration … environment, import, tests`) and has a guide to
70
+ [profiling](https://vitest.dev/guide/profiling-test-performance). For short component tests it
71
+ is usually starting a worker for every file, not the tests.
72
+ - **[Pool](https://vitest.dev/guide/improving-performance#pool).** With many test files this is
73
+ the setting that matters. `pool: "vmThreads"` reuses workers and gives each file a fresh
74
+ context: 1,000 short React test files went from 18.7 s to 7.0 s here. It uses more memory
75
+ ([`vmMemoryLimit`](https://vitest.dev/config/vmmemorylimit)), and `instanceof` no longer holds
76
+ for values that come from Node (an error from `fs`, a `Buffer`); DOM objects are unaffected.
77
+ - **[Test isolation](https://vitest.dev/guide/improving-performance#test-isolation).** Rather
78
+ than `isolate: false`, use `ctrlDom({ sharedWorkers: true })`: it sets that and resets the
79
+ window between files, which stock Testing Library's cleanup does not do past a worker's first
80
+ file. [Shared workers](#shared-workers).
81
+ - **[Sharding](https://vitest.dev/guide/improving-performance#sharding)** splits a large suite
82
+ across CI machines.
83
+ - **Watch mode while you work.** A one-file `vitest run` is mostly Vitest starting; `vitest` pays
84
+ that once.
85
+
86
+ DOM only (stock Testing Library packages keep working):
87
+
88
+ ```ts
89
+ export default defineConfig({ plugins: [ctrlDom({ testingLibrary: false })] });
45
90
  ```
46
91
 
92
+ Options go to the plugin: `ctrlDom({ url: "http://localhost:3000/", width: 1280 })`. With the
93
+ plugin installed, a file can also pick the environment itself: `// @vitest-environment ctrl-dom`.
94
+ An `environment` you set in the config other than `"ctrl-dom"` stays the project's default.
95
+
96
+ The plugin also looks after Vitest's workers, since the default pool starts one per test file:
97
+ with its Testing Library it turns Node's compile cache on as each worker starts (so Vitest's own
98
+ worker modules load from it too, not only the DOM), and it starts the next files' workers ahead
99
+ of time ([warm workers](#warm-workers)).
100
+
101
+ ### Faster runs
102
+
103
+ `ctrlDom()` alone is the safe setup: a new process for every test file, as Vitest does by default,
104
+ with each file's process started ahead of time ([warm workers](#warm-workers)). For a small suite
105
+ that is all you need.
106
+
107
+ For a large suite, change the pool. Most of what a short component test costs is starting its
108
+ worker, not the test, and that cost is per file. The same React test files on each option, with
109
+ Vitest's default of 15 workers on a 16-thread machine:
110
+
111
+ | | 100 files | 1,000 files | Memory, 1,000 files | What changes for your tests |
112
+ | --- | ---: | ---: | --- | --- |
113
+ | `ctrlDom()` (Vitest's default pool) | 2.56 s | 18.7 s | ~100 MB per worker (1.5 GB for 15) | Nothing. A new process for every test file. |
114
+ | `test: { pool: "threads" }` | 2.33 s | 16.7 s | | A new thread per file instead of a process: `process.chdir()` throws, and native addons must be thread-safe. |
115
+ | `test: { pool: "vmThreads" }` | 1.61 s | **7.0 s** | 4.0 GB at peak | Workers are reused, and each file gets a fresh global scope and module graph in them: how Jest isolates. See below. |
116
+ | `test: { pool: "vmForks" }` | 1.64 s | 7.3 s | 273 MB per worker, up to 527 MB | The same, in processes instead of threads. |
117
+ | `ctrlDom({ sharedWorkers: true })` | | | | Files of a worker share one realm; the plugin resets the window and re-evaluates your own modules before each file. Base UI's 9,199 tests: 35 s → 7–8 s. [Shared workers](#shared-workers). |
118
+
119
+ Medians of 3–5 runs, i5-12600K, Node 26.13.1. With 4 workers and 1,000 files: 24.3 s by default,
120
+ 11.9 s on `vmThreads`.
121
+
122
+ **`pool: "vmThreads"` is the one to try first on a large suite: 2.7× faster here**, and the gap
123
+ grows with the number of files. Two things to check when you switch:
124
+
125
+ - **Memory.** Reused workers hold on to the modules of every file they have run. Vitest recycles
126
+ a worker when it passes [`vmMemoryLimit`](https://vitest.dev/config/#vmmemorylimit); lower it
127
+ if CI runs out.
128
+ - **`instanceof` on values from Node.** Each file's globals are its own, so an error thrown by
129
+ `fs` or `fetch`, or a `Buffer`, is not an instance of the file's `Error` or `Uint8Array`. DOM
130
+ objects are fine: `@ctrl/dom`'s classes are shared with every file.
131
+
132
+ `sharedWorkers` is the other way to reuse workers. Files then share one set of globals, so
133
+ `instanceof` holds, but they also share module state in dependencies, and memory grows there too.
134
+ Do not set `isolate: false` yourself: stock Testing Library then cleans up after a worker's first
135
+ file only, which `sharedWorkers` handles.
136
+
137
+ While you work, use watch mode: `vitest run` on one file is about 0.9 s for a 0.02 s test, and
138
+ `vitest` pays the start once and keeps a worker ready for the file you just saved.
139
+
47
140
  DOM only (stock Testing Library packages keep working):
48
141
 
49
142
  ```ts
@@ -54,9 +147,38 @@ Options go to the plugin: `ctrlDom({ url: "http://localhost:3000/", width: 1280
54
147
  plugin installed, a file can also pick the environment itself: `// @vitest-environment ctrl-dom`.
55
148
  An `environment` you set in the config other than `"ctrl-dom"` stays the project's default.
56
149
 
57
- The plugin also adds a Node flag to Vitest's workers, since the default pool starts one per test
58
- file: with its Testing Library it turns the compile cache on as each worker starts, so Vitest's
59
- own worker modules load from it too, not only the DOM.
150
+ The plugin also looks after Vitest's workers, since the default pool starts one per test file:
151
+ with its Testing Library it turns Node's compile cache on as each worker starts (so Vitest's own
152
+ worker modules load from it too, not only the DOM), and it starts the next files' workers ahead
153
+ of time ([warm workers](#warm-workers)).
154
+
155
+ ### Faster runs
156
+
157
+ Most of what a suite of short component tests costs is not the tests: a file that renders a form
158
+ and clicks through it runs in about 20 ms, and starting a worker for it takes several times that.
159
+ Vitest prints the split at the end of a run (`Duration … environment 40%, tests 25%, …`). When
160
+ `tests` is the small share, this section pays.
161
+
162
+ 1. **Start with `ctrlDom()` and nothing else.** It already does the safe things: this package's
163
+ Testing Library, the compile cache, warm workers. Prefer it to `environment: "ctrl-dom"` alone,
164
+ which gets you the DOM and none of that.
165
+ 2. **Use watch mode while you work.** `vitest run` on one file is about 0.9 s, of which the test
166
+ is 0.02 s; the rest is Vitest starting. `vitest` pays that once and keeps a worker ready, so
167
+ the file you just saved starts immediately.
168
+ 3. **Leave `maxWorkers` alone** unless the machine is busy with something else.
169
+ 4. **For more, trade isolation for speed deliberately.** The same 100 React test files:
170
+
171
+ | | 4 workers | 15 workers | What changes for your tests |
172
+ | --- | ---: | ---: | --- |
173
+ | `ctrlDom()` (Vitest's default pool) | 2.86 s | 2.56 s | Nothing. A new process for every test file. |
174
+ | `test: { pool: "threads" }` | 3.60 s | 2.33 s | A new thread for every file instead of a process: `process.chdir()` throws, and native addons must be thread-safe. Only ahead when every core is busy. |
175
+ | `test: { pool: "vmThreads" }` | 2.05 s | 1.61 s | Workers are reused; each file gets a fresh global scope and module graph in them, which is how Jest isolates. Objects Node itself creates (an error from `fs` or `fetch`, a `Buffer`) come from outside that scope, so `instanceof Error` on them is false, and memory grows until a worker is recycled (`vmMemoryLimit`). `vmForks` is the same with processes: 2.16 s / 1.64 s. |
176
+ | `ctrlDom({ sharedWorkers: true })` | | | Files of a worker share one realm; the plugin resets the window and re-evaluates your own modules before each file. Module state in dependencies carries over. Base UI's 9,199 tests: 35 s → 7–8 s. [Shared workers](#shared-workers). |
177
+
178
+ Medians of 5 runs, i5-12600K (16 threads), Node 26.13.1. Whether a suite passes on `vmThreads`
179
+ unchanged comes down to those `instanceof` checks on values from Node: run the whole suite on it
180
+ before committing to it. Do not set `isolate: false` yourself: stock Testing Library
181
+ then cleans up after a worker's first file only, which `sharedWorkers` exists to handle.
60
182
 
61
183
  ## Jest
62
184
 
@@ -268,9 +390,38 @@ Options of the Vitest plugin alone (`ctrlDom({ ... })`):
268
390
  | --- | --- | --- |
269
391
  | `testingLibrary` | `true` | route stock `@testing-library/*` imports to @ctrl/dom's implementations; `false` swaps only the DOM |
270
392
  | `jsxRuntime` | `true` | resolve `react/jsx-runtime` and `react/jsx-dev-runtime` to ES module copies of them. Vitest hands test and application code a Proxy over a CommonJS dependency, so every JSX element otherwise goes through the Proxy's `get` trap; a hooks-heavy render is 8% faster without it. Left alone when the project resolves the runtime to anything but its React |
393
+ | `warmWorkers` | `true` | start the next test files' worker processes ahead of time, with the DOM already loaded: see below |
271
394
  | `sharedWorkers` | `false` | run a worker's test files one after another in the same realm instead of starting a worker per file: see below |
272
395
  | `debug` | off | `"aria"` or `"compact"`: [debugging with coding agents](#debugging-with-coding-agents) |
273
396
 
397
+ ### Warm workers
398
+
399
+ Vitest's default pool gives every test file a fresh worker process, started when the file's turn
400
+ comes. The file then waits about 100 ms while Node boots, Vitest's runtime loads and the DOM is
401
+ set up: longer than a short component test takes to run.
402
+
403
+ The plugin starts those processes ahead of time instead. While one file's tests run, the next
404
+ file's worker boots on a core Vitest is not using and loads the DOM; when its turn comes it starts
405
+ at once. Nothing is shared or reused: each file still runs in its own new process and window,
406
+ exactly as without the option (`isolate` stays on), and a worker that is never needed is not
407
+ started.
408
+
409
+ | 100 React test files | without | with |
410
+ | --- | ---: | ---: |
411
+ | 4 workers on 12 cores | 4.22 s | 2.94 s |
412
+ | 8 workers on 16 cores (what `vitest` in watch mode uses) | 3.10 s | 2.51 s |
413
+ | 15 workers on 16 cores (what `vitest run` uses) | 2.55 s | 2.42 s |
414
+
415
+ It needs idle cores to boot on, so it helps most where Vitest runs fewer workers than the machine
416
+ has cores: watch mode, a `maxWorkers` setting, CI machines. In watch mode one worker stays ready
417
+ between runs, so the file you just saved starts immediately. When every core already has a
418
+ worker, runs take what they took.
419
+
420
+ It applies to `pool: "forks"` (Vitest's default) and turns itself off under a debugger or
421
+ profiler (`--inspect`, `--cpu-prof`). `ctrlDom({ warmWorkers: false })` or
422
+ `CTRL_DOM_WARM_WORKERS=0` turns it off; `CTRL_DOM_WARM_WORKERS=debug` prints how many workers
423
+ started ahead of their file.
424
+
274
425
  ### Shared workers
275
426
 
276
427
  ```ts