Debian Codegraph @

node-manten/skills/manten/MIGRATION.md

raw
1 # Migration Guide
2
3 Migrating from manten v1 (callback destructuring) to v2 (standalone imports via AsyncLocalStorage).
4
5 ## Quick Summary
6
7 **Before:** APIs accessed via callback destructuring
8
9 ```ts
10 import { describe, testSuite } from 'manten'
11
12 describe('App', ({
13 test, describe, onFinish, skip
14 }) => {
15 test('works', ({ onTestFail, expectSnapshot }) => {
16 // ...
17 })
18 })
19
20 export default testSuite(({ test }, parameter) => {
21 // ...
22 })
23 ```
24
25 **After:** APIs are standalone imports, callbacks only receive `{ signal }`
26
27 ```ts
28 import {
29 describe, test, onFinish, skip, onTestFail, expectSnapshot
30 } from 'manten'
31
32 describe('App', () => {
33 test('works', () => {
34 // ...
35 })
36 })
37 ```
38
39 ## Step-by-Step Migration
40
41 ### 1. Remove callback destructuring from `describe`
42
43 ```diff
44 -describe('Group', ({ test, describe, onFinish, skip }) => {
45 +describe('Group', () => {
46 ```
47
48 If the callback uses `signal`, keep it:
49
50 ```diff
51 -describe('Group', ({ test, signal }) => {
52 +describe('Group', ({ signal }) => {
53 ```
54
55 ### 2. Remove callback destructuring from `test`
56
57 ```diff
58 -test('name', ({ onTestFail, onTestFinish, skip, expectSnapshot }) => {
59 +test('name', () => {
60 ```
61
62 If the callback uses `signal`, keep it:
63
64 ```diff
65 -test('name', ({ signal, onTestFail }) => {
66 +test('name', ({ signal }) => {
67 ```
68
69 ### 3. Add standalone imports
70
71 Add any APIs that were destructured from callbacks to the import statement:
72
73 ```diff
74 -import { describe } from 'manten';
75 +import { describe, test, onFinish, skip } from 'manten';
76 ```
77
78 ```diff
79 -import { testSuite, expect } from 'manten';
80 +import { describe, test, expect, onTestFail } from 'manten';
81 ```
82
83 Full list of standalone imports:
84
85 | API | Previously from | Now |
86 | --- | --- | --- |
87 | `test` | `describe` callback | `import { test } from 'manten'` |
88 | `describe` | `describe` callback | `import { describe } from 'manten'` |
89 | `onFinish` | `describe` callback | `import { onFinish } from 'manten'` |
90 | `skip` | `describe`/`test` callback | `import { skip } from 'manten'` |
91 | `onTestFail` | `test` callback | `import { onTestFail } from 'manten'` |
92 | `onTestFinish` | `test` callback | `import { onTestFinish } from 'manten'` |
93 | `expectSnapshot` | `test` callback | `import { expectSnapshot } from 'manten'` |
94
95 ### 4. Replace `testSuite` + `runTestSuite` with `describe` + `import()`
96
97 `testSuite` and `runTestSuite` are removed. There are two replacement patterns:
98
99 #### Pattern A: Self-executing files (no parameters)
100
101 Files with a top-level `describe()` that runs on import. These files are also independently runnable as `node file.ts`.
102
103 **Must use dynamic `import()` inside a `describe()` block** — the import executes the top-level `describe()`, which registers with the parent context via AsyncLocalStorage.
104
105 Spec file:
106
107 ```diff
108 -import { testSuite, expect } from 'manten';
109 +import { describe, test, expect } from 'manten';
110
111 -export default testSuite('Auth', ({ test }) => {
112 +describe('Auth', () => {
113 test('login', () => {
114 expect(true).toBe(true);
115 });
116 });
117 ```
118
119 Entry point:
120
121 ```diff
122 -describe('App', ({ runTestSuite }) => {
123 - runTestSuite(import('./specs/auth.js'));
124 - runTestSuite(import('./specs/api.js'));
125 +describe('App', async () => {
126 + await import('./specs/auth.ts');
127 + await import('./specs/api.ts');
128 });
129 ```
130
131 #### Pattern B: Exported functions (with parameters)
132
133 Files that export a function wrapping a `describe()` call. Since the `describe()` doesn't execute until the function is called, these **can use static `import` at the top of the file**.
134
135 Spec file:
136
137 ```diff
138 -import { testSuite, expect } from 'manten';
139 +import { describe, test, expect } from 'manten';
140
141 -export default testSuite(({ test }, nodePath: string) => {
142 - test('works', async () => {
143 - const result = await run(nodePath);
144 - expect(result.exitCode).toBe(0);
145 - });
146 -});
147 +export const builds = (nodePath: string) =>
148 + describe('builds', () => {
149 + test('works', async () => {
150 + const result = await run(nodePath);
151 + expect(result.exitCode).toBe(0);
152 + });
153 + });
154 ```
155
156 The export name should match the file name or `describe()` description. Entry point:
157
158 ```diff
159 +import { builds } from './specs/builds.ts';
160 +import { errors } from './specs/errors.ts';
161
162 -describe('App', ({ runTestSuite }) => {
163 - runTestSuite(import('./specs/builds.js'), nodePath);
164 - runTestSuite(import('./specs/errors.js'), nodePath);
165 +describe('App', () => {
166 + builds(nodePath);
167 + errors(nodePath);
168 });
169 ```
170
171 #### Pattern B with nested suites and options
172
173 ```diff
174 -import { testSuite } from 'manten';
175 -
176 -export default testSuite('builds', ({ runTestSuite }, nodePath: string) => {
177 - runTestSuite(import('./output-commonjs.js'), nodePath);
178 - runTestSuite(import('./output-module.js'), nodePath);
179 -}, {
180 - parallel: 'auto',
181 -});
182 +import { describe } from 'manten';
183 +import { outputCommonjs } from './output-commonjs.ts';
184 +import { outputModule } from './output-module.ts';
185 +
186 +export const builds = (nodePath: string) =>
187 + describe('builds', () => {
188 + outputCommonjs(nodePath);
189 + outputModule(nodePath);
190 + }, {
191 + parallel: 'auto',
192 + });
193 ```
194
195 ### 5. Update file extensions
196
197 If migrating to native Node.js type stripping (Node 22.6+), change `.js` imports to `.ts`:
198
199 ```diff
200 -runTestSuite(import('./specs/auth.js'));
201 +await import('./specs/auth.ts');
202 ```
203
204 ### 6. Remove unused imports
205
206 After migration, remove `testSuite` and `runTestSuite` from all imports. They no longer exist:
207
208 ```diff
209 -import { testSuite, expect } from 'manten';
210 +import { describe, test, expect } from 'manten';
211 ```
212
213 ## Removed APIs
214
215 | Removed | Replacement |
216 | --- | --- |
217 | `testSuite()` | `describe()` + exported function |
218 | `runTestSuite()` | `await import()` inside `describe()` |
219 | `TestSuite` type | Not needed |
220 | `Context` type | Not needed |
221 | `ContextApi` type | Not needed |
222 | `TestApi` type | Not needed |
223
224 ## Unchanged APIs
225
226 These work exactly the same:
227
228 - `expect()` — Jest assertion library
229 - `setProcessTimeout(ms)` — process-level timeout
230 - `configure({ snapshotPath })` — snapshot configuration
231 - `test(title, fn, timeout)` — timeout as number
232 - `test(title, fn, { retry, timeout })` — options object
233 - `describe(title, fn, { parallel, timeout })` — options object
234
235 ## Common Patterns
236
237 ### Entry point with parameterized suites
238
239 This pattern is common when testing across Node versions:
240
241 ```diff
242 -import { describe, setProcessTimeout } from 'manten';
243 -
244 -setProcessTimeout(1000 * 60 * 10);
245 -
246 -describe('App', async ({ describe }) => {
247 - for (const nodeVersion of nodeVersions) {
248 - const node = await getNode(nodeVersion);
249 - await describe(`Node ${node.version}`, ({ runTestSuite }) => {
250 - runTestSuite(import('./specs/builds.js'), node.path);
251 - runTestSuite(import('./specs/errors.js'), node.path);
252 - });
253 - }
254 -});
255 +import { describe, setProcessTimeout } from 'manten';
256 +import { builds } from './specs/builds.ts';
257 +import { errors } from './specs/errors.ts';
258 +
259 +setProcessTimeout(1000 * 60 * 10);
260 +
261 +describe('App', async () => {
262 + for (const nodeVersion of nodeVersions) {
263 + const node = await getNode(nodeVersion);
264 + await describe(`Node ${node.version}`, () => {
265 + builds(node.path);
266 + errors(node.path);
267 + });
268 + }
269 +});
270 ```
271
272 ### Test with hooks and signal
273
274 ```diff
275 -test('fetches data', async ({ signal, onTestFail, onTestFinish }) => {
276 +test('fetches data', async ({ signal }) => {
277 const response = await fetch('/api', { signal });
278 onTestFail(error => console.log('Response:', response));
279 onTestFinish(() => cleanup());
280 expect(response.ok).toBe(true);
281 });
282 ```
283
284 ### Describe with onFinish cleanup
285
286 ```diff
287 -describe('Database', async ({ test, onFinish }) => {
288 - const database = await connect();
289 - onFinish(() => database.close());
290 +describe('Database', async () => {
291 + const database = await connect();
292 + onFinish(() => database.close());
293
294 - test('query', async () => {
295 + test('query', async () => {
296 expect(await database.query('SELECT 1')).toBeTruthy();
297 });
298 });
299 ```
300
301 ### Describe with skip
302
303 ```diff
304 -describe('Linux only', ({ test, skip }) => {
305 - if (process.platform !== 'linux') {
306 - skip('Not on Linux');
307 - }
308 +describe('Linux only', () => {
309 + if (process.platform !== 'linux') {
310 + skip('Not on Linux');
311 + }
312
313 - test('works', () => {
314 + test('works', () => {
315 // ...
316 });
317 });
318 ```
319
320 ## Recommendations
321
322 ### Prefer concurrent execution
323
324 `test()` and `describe()` return plain promises. Node.js won't exit while promises are still settling, so you don't need to `await` them to keep the process alive. Prefer leaving off `await` to maximize concurrency — only add it when you need ordering guarantees.
325
326 ### Use a single entry point
327
328 Instead of running each test file independently, create a `tests/index.ts` that imports all test files. This gives you one command to run everything and enables `node --watch` across all files.
329
330 ### Recommended project structure
331
332 ```
333 tests/
334 specs/ # test files
335 utils/ # shared test helpers
336 fixtures/ # static test data
337 index.ts # entry point — run this
338 ```