@warlock.js/core 4.13.0 → 4.15.0
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.
- package/CHANGELOG.md +98 -0
- package/esm/generations/add-command.action.mjs +18 -6
- package/esm/generations/add-command.action.mjs.map +1 -1
- package/esm/tests/index.d.mts +3 -2
- package/esm/tests/index.mjs +2 -2
- package/esm/tests/test-connectors-selection.d.mts +14 -0
- package/esm/tests/test-connectors-selection.d.mts.map +1 -0
- package/esm/tests/test-connectors-selection.mjs +72 -0
- package/esm/tests/test-connectors-selection.mjs.map +1 -0
- package/esm/tests/test-lifecycle-state.mjs +27 -0
- package/esm/tests/test-lifecycle-state.mjs.map +1 -0
- package/esm/tests/test-setup-timeout.mjs +53 -0
- package/esm/tests/test-setup-timeout.mjs.map +1 -0
- package/esm/tests/vitest-setup.d.mts +64 -8
- package/esm/tests/vitest-setup.d.mts.map +1 -1
- package/esm/tests/vitest-setup.mjs +317 -11
- package/esm/tests/vitest-setup.mjs.map +1 -1
- package/package.json +74 -30
- package/skills/test-http/SKILL.md +8 -2
- package/skills/test-service/SKILL.md +159 -20
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: test-service
|
|
3
|
-
description: 'Pure unit tests against services, repositories, models, and use-cases — `setupTest({ connectors })` bootstraps
|
|
3
|
+
description: 'Pure unit tests against services, repositories, models, and use-cases — `setupTest({ connectors })` bootstraps the framework with its own DB/cache connections so you can call your code directly, and `teardownTest()` closes it. Triggers: `setupTest`, `teardownTest`, `src/test-setup.ts`, `tests.connectors`, `tests.setupTimeout`, `Application.setEnvironment`; "unit-test a service", "test a repository query", "vitest setupFiles", "skip connectors for pure-logic tests"; typical import `import { setupTest, teardownTest } from "@warlock.js/core/tests"`. Skip: HTTP integration — `@warlock.js/core/test-http/SKILL.md`; warlock add test scaffold — `@warlock.js/core/write-cli-command/SKILL.md`; competing tooling: jest direct, `supertest`, `nock`.'
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Warlock — test a service
|
|
7
7
|
|
|
8
|
-
For unit tests, you import the thing under test and call it directly. No HTTP, no fetch, no controllers. Framework testing in Warlock is about getting your **service layer** under test efficiently — and that means
|
|
8
|
+
For unit tests, you import the thing under test and call it directly. No HTTP, no fetch, no controllers. Framework testing in Warlock is about getting your **service layer** under test efficiently — and that means your tests need a bootstrapped framework with a DB connection.
|
|
9
9
|
|
|
10
|
-
`setupTest()` is the one-call bootstrap that
|
|
10
|
+
`setupTest()` is the one-call bootstrap that provides that environment; `teardownTest()` closes it.
|
|
11
|
+
|
|
12
|
+
⚠ **Corrected in 4.14.0 — `setupTest` is CALLED once per TEST FILE, not once per worker.** Every version of this skill through 4.13.0 said "per worker", the generated `src/test-setup.ts` carries a `Per-Worker Test Setup` comment saying the same thing, and **both were wrong.** Vitest runs `setupFiles` before **each test file**, and the setup module's registry is rebuilt every time — measured across all four `pool` × `isolate` combinations.
|
|
13
|
+
|
|
14
|
+
⛔ **If your project was generated before 4.14.0, fix BOTH the comment and the call.** The comment is false, **and** the generated `setupTest({ connectors: true })` is now an *explicit* value that overrides your `src/config/tests.ts`. **Bare `setupTest()` is the correct call.**
|
|
15
|
+
|
|
16
|
+
**The lifetime is FILE-SCOPED, on purpose.** Your setup file bootstraps the framework and its `afterAll(teardownTest)` closes it, once per test file. **One owner, one pairing — correct under every pool, every isolation setting, and watch mode.**
|
|
17
|
+
|
|
18
|
+
⚠ **A worker-scoped lifetime is possible and is deliberately not shipped yet.** Lifecycle state now lives in the worker runtime, so leaving the framework running would let every file in a worker share one bootstrap. Two things block claiming it: under `pool: "threads"` we cannot observe whether Node reclaims a torn-down thread's sockets and pools, and **in watch mode Vitest reuses workers between reruns, so there is no recycle and no cleanup owner.** It gets taken when the real cost is measured and the integration is chosen, not inherited. See *Lifecycle and repeated calls* below.
|
|
11
19
|
|
|
12
20
|
⚠ **Changed in 4.13.0 — the import is a subpath now.** `setupTest` used to be re-exported from the package root; it is not any more, because that put the test helpers into every application's production module graph. `import { setupTest } from "@warlock.js/core"` now fails with *"has no exported member"* — **add `/tests` to the specifier and nothing else changes.**
|
|
13
21
|
|
|
@@ -34,9 +42,9 @@ describe("registerUserService", () => {
|
|
|
34
42
|
});
|
|
35
43
|
```
|
|
36
44
|
|
|
37
|
-
No `beforeAll(setupTest)` in this file — the project's `src/test-setup.ts` (registered as `setupFiles` in `vite.config.ts`) already ran it
|
|
45
|
+
No `beforeAll(setupTest)` in this file — the project's `src/test-setup.ts` (registered as `setupFiles` in `vite.config.ts`) already ran it **before this file's tests executed**, as it does before every test file.
|
|
38
46
|
|
|
39
|
-
## `setupTest({ connectors })` — the
|
|
47
|
+
## `setupTest({ connectors })` — the bootstrap
|
|
40
48
|
|
|
41
49
|
```ts
|
|
42
50
|
import { setupTest } from "@warlock.js/core/tests";
|
|
@@ -51,10 +59,10 @@ What it does (in order):
|
|
|
51
59
|
3. Runs `bootstrap()` — env, app, prestart hooks.
|
|
52
60
|
4. Initializes the `filesOrchestrator` (module/route/config discovery, no file watching).
|
|
53
61
|
5. Loads all `src/config/*.ts` files.
|
|
54
|
-
6.
|
|
62
|
+
6. Resolves the connector selection — **an explicit parameter wins, then `tests.connectors` from config, then the `true` default.** See *Selecting connectors* below.
|
|
55
63
|
7. Starts the chosen connectors — but **never `http`** when you pass a boolean. HTTP is the global-setup's job.
|
|
56
64
|
|
|
57
|
-
The result:
|
|
65
|
+
The result: DB/cache/logger/storage connections your code can use. Models save, repositories query, services run. Same code as production, just isolated to the test process.
|
|
58
66
|
|
|
59
67
|
### The `connectors` parameter
|
|
60
68
|
|
|
@@ -64,9 +72,9 @@ The result: each worker has its own DB/cache/logger/storage connections. Models
|
|
|
64
72
|
| `false` | None | Pure logic tests with no DB / cache touches (parsers, validators, util functions). |
|
|
65
73
|
| `["database", "cache"]` | Just those, in that order | A test that only needs DB but not, say, the storage driver. |
|
|
66
74
|
|
|
67
|
-
The default `true` is the sane choice. Reach for `false` when the unit you're testing genuinely doesn't talk to any framework subsystem — pulling up a DB connection
|
|
75
|
+
The default `true` is the sane choice. Reach for `false` when the unit you're testing genuinely doesn't talk to any framework subsystem — pulling up a DB connection **for every file** just to test a string parser is wasted setup time.
|
|
68
76
|
|
|
69
|
-
###
|
|
77
|
+
### Selecting connectors — `src/config/tests.ts`
|
|
70
78
|
|
|
71
79
|
```ts title="src/config/tests.ts"
|
|
72
80
|
const testsConfigurations = {
|
|
@@ -76,7 +84,33 @@ const testsConfigurations = {
|
|
|
76
84
|
export default testsConfigurations;
|
|
77
85
|
```
|
|
78
86
|
|
|
79
|
-
|
|
87
|
+
⚠ **BREAKING in 4.14.0 — the precedence flipped.**
|
|
88
|
+
|
|
89
|
+
| | Order |
|
|
90
|
+
|---|---|
|
|
91
|
+
| **4.13.0 and earlier** | `tests.connectors` config **>** `setupTest({ connectors })` parameter **>** `true` |
|
|
92
|
+
| **4.14.0 onward** | **explicit `setupTest({ connectors })` parameter** **>** `tests.connectors` config **>** `true` |
|
|
93
|
+
|
|
94
|
+
**An explicit call-site value now beats project config.** If your project sets `tests.connectors` *and* some test file passes `connectors` explicitly, **that file will start a different connector set after upgrading.** Search for `setupTest({` across your tests before you upgrade — a call passing `connectors` was previously ignored and is now honoured.
|
|
95
|
+
|
|
96
|
+
**"Explicit" means you supplied a non-`undefined` value.** Both of these fall through to config:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
await setupTest(); // → tests.connectors, else true
|
|
100
|
+
await setupTest({}); // → tests.connectors, else true
|
|
101
|
+
await setupTest({ connectors: undefined }); // → tests.connectors, else true — NOT "start none"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The `undefined` rule is deliberate: an optional variable that happens to be `undefined` must not silently erase your project config.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
await setupTest({ connectors: false }); // → none, even if config says otherwise
|
|
108
|
+
await setupTest({ connectors: ["database"] }); // → exactly that, even if config differs
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
⚠ **The generated `src/test-setup.ts` calls `setupTest()` with no argument, on purpose.** If you "helpfully" change it to `setupTest({ connectors: true })`, you have made it explicit and **erased the `tests.connectors` layer for the whole project.**
|
|
112
|
+
|
|
113
|
+
Use `tests.connectors` when every test file agrees on the same minimal list — it saves repeating the array, and individual files can still override it.
|
|
80
114
|
|
|
81
115
|
## Project wiring — `src/test-setup.ts` + `vite.config.ts`
|
|
82
116
|
|
|
@@ -84,14 +118,22 @@ The `warlock add test` feature creates both files. The standard wiring:
|
|
|
84
118
|
|
|
85
119
|
```ts title="src/test-setup.ts"
|
|
86
120
|
/**
|
|
87
|
-
*
|
|
88
|
-
* Runs
|
|
121
|
+
* Test Setup
|
|
122
|
+
* Runs before EACH test file — not once per worker.
|
|
89
123
|
*/
|
|
90
|
-
import {
|
|
124
|
+
import { afterAll } from "vitest";
|
|
125
|
+
import { setupTest, teardownTest } from "@warlock.js/core/tests";
|
|
91
126
|
|
|
92
|
-
await setupTest(
|
|
127
|
+
await setupTest();
|
|
128
|
+
afterAll(teardownTest);
|
|
93
129
|
```
|
|
94
130
|
|
|
131
|
+
⛔ **Three things changed here in 4.14.0. If you generated this file earlier, replace all three — it is not a comment fix.**
|
|
132
|
+
|
|
133
|
+
1. **`afterAll(teardownTest)` is new and mandatory.** Nothing else closes the framework your tests started. This is what makes the lifetime file-scoped and owned rather than left running.
|
|
134
|
+
2. **The call is now bare `setupTest()`, not `setupTest({ connectors: true })`.** Under the flipped precedence, passing `true` is an *explicit* value and would override `tests.connectors` for **every file in the project.** Bare means "whatever this project configured, else the default".
|
|
135
|
+
3. **The comment used to say `Per-Worker Test Setup` / "Runs in EACH Vitest worker thread".** False — see the top of this skill.
|
|
136
|
+
|
|
95
137
|
```ts title="vite.config.ts"
|
|
96
138
|
import { lowerStage3Decorators } from "@warlock.js/core/vite";
|
|
97
139
|
import mongezVite from "@mongez/vite";
|
|
@@ -101,7 +143,7 @@ export default defineConfig({
|
|
|
101
143
|
plugins: [lowerStage3Decorators(), mongezVite()],
|
|
102
144
|
test: {
|
|
103
145
|
globalSetup: "./src/test-global-setup.ts", // ← HTTP server (see test-http skill)
|
|
104
|
-
setupFiles: ["./src/test-setup.ts"], // ← runs setupTest
|
|
146
|
+
setupFiles: ["./src/test-setup.ts"], // ← runs setupTest before EACH test file
|
|
105
147
|
environment: "node",
|
|
106
148
|
globals: false,
|
|
107
149
|
include: ["src/app/**/*.test.ts"],
|
|
@@ -206,7 +248,9 @@ afterEach(async () => {
|
|
|
206
248
|
});
|
|
207
249
|
```
|
|
208
250
|
|
|
209
|
-
Vitest runs tests
|
|
251
|
+
Vitest runs the tests within one file sequentially, so an `afterEach` truncate gives each test a clean slate.
|
|
252
|
+
|
|
253
|
+
⚠ **Cross-*file* isolation is not solved by this.** Separate workers get separate **connections**, not separate **rows** — two files pointed at the same database see each other's committed data regardless of pool or worker count. **Truncate what your file wrote; don't assume the worker boundary did it for you.** Real data isolation (DB-per-worker, transaction-per-test) is a separate piece of work and is not in this release.
|
|
210
254
|
|
|
211
255
|
### Skipping connectors for pure logic tests
|
|
212
256
|
|
|
@@ -226,14 +270,109 @@ describe("slugify", () => {
|
|
|
226
270
|
});
|
|
227
271
|
```
|
|
228
272
|
|
|
229
|
-
|
|
273
|
+
⛔ **BREAKING in 4.14.0 — the example above now REJECTS if your project has a `src/test-setup.ts`.**
|
|
274
|
+
|
|
275
|
+
Through 4.13.0 a second `setupTest` call with different options was a **silent no-op** — you asked for no connectors, got all of them, and nothing told you. In 4.14.0 a conflicting call **rejects with an error naming both the active and the requested selection**, because silently ignoring what you asked for is worse than failing.
|
|
276
|
+
|
|
277
|
+
**If `src/test-setup.ts` already ran `setupTest()` in this file, use one of these instead:**
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
// 1. Tear down first, then set up differently — and PUT IT BACK when the file ends.
|
|
281
|
+
import { afterAll, beforeAll } from "vitest";
|
|
282
|
+
import { setupTest, teardownTest } from "@warlock.js/core/tests";
|
|
283
|
+
|
|
284
|
+
beforeAll(async () => {
|
|
285
|
+
await teardownTest();
|
|
286
|
+
await setupTest({ connectors: false });
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
afterAll(async () => {
|
|
290
|
+
await teardownTest(); // ← REQUIRED, see below
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
⛔ **The `afterAll` is not optional** — without it you leave a `connectors: false` runtime ready when the file ends.
|
|
295
|
+
|
|
296
|
+
⚠ **This pattern interacts with the `afterAll(teardownTest)` in your setup file, and the relative ordering of the two has not been verified.** `teardownTest` is idempotent, so whichever runs second finds an idle lifecycle and no-ops — but **do not build anything on a particular order until someone has measured it.** This is the strongest argument for option 2 below.
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
// 2. Or don't call setupTest at all — a pure-logic test needs nothing from it.
|
|
300
|
+
// The connectors your setup file started are already running; you simply don't use them.
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
**3. Or set `tests.connectors: false` in `src/config/tests.ts`** if no test file in the project needs connectors.
|
|
304
|
+
|
|
305
|
+
**Option 2 is usually right, and option 1 is easy to get wrong.** Tearing down and re-bootstrapping costs a full framework startup — twice, once for your file and once for the next — to avoid a DB connection you were never going to use. **Reach for option 1 only when a connector's mere presence breaks the thing you're testing**, not to save setup time.
|
|
306
|
+
|
|
307
|
+
## Lifecycle and repeated calls
|
|
308
|
+
|
|
309
|
+
`setupTest` / `teardownTest` are a pair. **The harness that calls one owns calling the other in the same context.**
|
|
310
|
+
|
|
311
|
+
| Call | Behaviour |
|
|
312
|
+
|---|---|
|
|
313
|
+
| `setupTest(x)` while idle | bootstraps |
|
|
314
|
+
| `setupTest(x)` while already ready with the **same** effective options | no-op |
|
|
315
|
+
| `setupTest(y)` while ready or starting with **different** effective options | ⛔ **rejects**, naming active vs requested |
|
|
316
|
+
| two concurrent `setupTest(x)` calls | share one startup |
|
|
317
|
+
| `setupTest` after a failed setup | allowed — a failed setup unwinds and returns to idle |
|
|
318
|
+
| `teardownTest()` while idle | no-op |
|
|
319
|
+
| two concurrent `teardownTest()` calls | share one shutdown |
|
|
320
|
+
| `setupTest(y)` after a **successful** teardown | allowed, different options fine |
|
|
321
|
+
|
|
322
|
+
**"Same options" is compared by meaning, not by literal value** — connector arrays are deduplicated and compared as sets, so `["cache", "database"]` and `["database", "cache", "cache"]` are the same selection.
|
|
323
|
+
|
|
324
|
+
⚠ **A failed shutdown poisons the lifecycle.** If `teardownTest()` rejects because the shutdown layer reported a failure, later `setupTest` calls **refuse until the Vitest worker is recycled** or a retried teardown fully succeeds. This is deliberate: a cleared flag does not prove that ports, sockets, pools or timers actually closed, and pretending otherwise hands you a "clean" run built on a leaked runtime.
|
|
325
|
+
|
|
326
|
+
⚠ **What it cannot detect:** `connectorsManager.shutdown()` catches and logs individual connector failures internally. Those never reach this lifecycle, so they never poison it. It surfaces what that layer reports — no more.
|
|
327
|
+
|
|
328
|
+
### `tests.setupTimeout` — the setup attempt is bounded
|
|
329
|
+
|
|
330
|
+
**New in 4.14.0.** A setup attempt that never settles used to leave the lifecycle stuck in `starting` and take the worker down with an out-of-memory crash. It is now bounded.
|
|
331
|
+
|
|
332
|
+
```ts title="src/config/tests.ts"
|
|
333
|
+
const testsConfigurations = {
|
|
334
|
+
connectors: ["database", "logger"],
|
|
335
|
+
setupTimeout: 120000, // milliseconds — this is the default
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export default testsConfigurations;
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Default: `120000` (two minutes)** — far above a healthy cold start, below the point where you'd stop watching the terminal. When it expires:
|
|
342
|
+
|
|
343
|
+
```
|
|
344
|
+
setupTest() did not finish within 120000ms and is stuck in the "starting" state. The
|
|
345
|
+
lifecycle is now poisoned: whatever that attempt had already started is not known to be
|
|
346
|
+
closed, so later setupTest() calls refuse until the Vitest worker is recycled. If your
|
|
347
|
+
cold start is legitimately slower than this, raise the bound with `tests.setupTimeout`
|
|
348
|
+
in `src/config/tests.ts` — milliseconds, default 120000.
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
1. **It bounds the setup ATTEMPT, not teardown separately.** `teardownTest()` awaits the same attempt, so it inherits the bound — **one timer, not two.** A second teardown-side deadline was tried and rejected: it re-introduced the unbounded re-entry this whole guard exists to remove.
|
|
352
|
+
2. **Expiry poisons the lifecycle**, it does not return to `idle`. The attempt may have started connectors nobody can now account for, so pretending the runtime is clean would be worse than refusing.
|
|
353
|
+
3. **⛔ An invalid `setupTimeout` throws, naming the bad value.** Zero, negative and non-numeric all fail loudly rather than falling back to the default — a silent fallback would hide a typo behind a working suite.
|
|
354
|
+
4. **The bound is measured from when the attempt started**, not from when config was read. `tests.setupTimeout` is only readable after `loadConfigFiles()`, which happens *inside* the window being bounded; re-arming naively would give you the default plus your configured value.
|
|
355
|
+
|
|
356
|
+
⚠ **What is proven and what is not.** The nine guards above were each seen to fail under their own mutation. **But every spec injects its scheduler**, so the default *value* is tested while the production timer — and whether its `unref` actually releases the worker — is not. **And no spec observes a real hang**: the stuck attempt is a mock gate, not a socket that never returns. These prove what the lifecycle *decides*, not what a genuinely wedged connector does.
|
|
357
|
+
|
|
358
|
+
### State is per worker runtime, not per module
|
|
359
|
+
|
|
360
|
+
The lifecycle state lives in the worker runtime, not in a module variable. That matters because **Vitest rebuilds the module registry between test files while the worker itself keeps running** — so a module-level flag resets exactly where live DB connections and pools survive. Scope:
|
|
361
|
+
|
|
362
|
+
- **`pool: "forks"`** — state is per worker **process**.
|
|
363
|
+
- **`pool: "threads"`** — state is per worker **thread**. It does **not** cross threads; `globalThis` is per realm, not per process.
|
|
364
|
+
|
|
365
|
+
In both cases the guard's scope matches the resource's scope, which is the point.
|
|
230
366
|
|
|
231
|
-
⚠ **
|
|
367
|
+
⚠ **Not guaranteed:** under `threads` with `isolate: true`, Vitest tears the thread down while the process lives on. **Whether Node reclaims that thread's sockets and pools is unmeasured**, and nothing in this lifecycle can observe it.
|
|
232
368
|
|
|
233
369
|
## Gotchas
|
|
234
370
|
|
|
235
|
-
- **`setupTest`
|
|
236
|
-
- **
|
|
371
|
+
- **`setupTest` runs per test file, not per worker** — every version of this skill through 4.13.0 said otherwise. **You pay one framework bootstrap per test file**, which is what 4.13.0 already cost; the difference is that it is now a chosen lifetime rather than a side effect of a module flag resetting.
|
|
372
|
+
- ⛔ **Never delete the `afterAll(teardownTest)` from your setup file.** Without it nothing closes what `setupTest` opened, and the connectors outlive the file that started them.
|
|
373
|
+
- ⛔ **You can't swap the connector set by calling `setupTest` again — it rejects now.** Through 4.13.0 the second call was silently ignored. Tear down first, or don't call it.
|
|
374
|
+
- **These connections are separate from the HTTP test server's.** A row inserted by a service-level test is on this connection; the HTTP test server has its own. They only see each other if both point at the same physical DB **and** the inserting test has committed.
|
|
375
|
+
- **`setupTest` takes over the process's `connectorsManager` for its lifetime.** Teardown is manager-wide, so **mixing `setupTest` with manually started connectors is unsupported** — teardown may close yours too.
|
|
237
376
|
- **`NODE_ENV` is set to `"test"`** by `setupTest`. Code that branches on `Application.isProduction` / `Application.isDevelopment` sees `false` for both. If your tests need production-like config (cookies, CORS), set those values in `src/config/*.ts` explicitly under the test branch — don't rely on the env flag.
|
|
238
377
|
- **No HTTP from this layer.** `setupTest({ connectors: true })` never starts the HTTP connector by design. Don't try to `request.app.http` your way to a fetch test — use the `test-http` skill instead.
|
|
239
378
|
- **Don't import `vitest-setup` from `@warlock.js/core/src/...`.** The public surface is `import { setupTest } from "@warlock.js/core/tests"`. Reaching into source paths breaks when the package layout shifts.
|