@boopkit/cap-testing 0.5.1 → 0.5.2
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/package.json
CHANGED
|
@@ -7,7 +7,7 @@ metadata:
|
|
|
7
7
|
# Testing: Express App (Web & APIs)
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
Parent skill: [testing
|
|
10
|
+
Parent skill: [`boop-testing-testing`](../boop-testing-testing/SKILL.md) (philosophy, generic approach)
|
|
11
11
|
|
|
12
12
|
---
|
|
13
13
|
|
|
@@ -20,63 +20,68 @@ npx jest tests/services/foo --no-coverage # Pattern match
|
|
|
20
20
|
npx jest tests/services/foo.test.ts -t "handles replay" # By test name
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
**Prerequisites**:
|
|
23
|
+
**Prerequisites**: integration tests run against the project's real backing services rather than mocks, so whatever the app talks to at runtime — the database first of all — has to be reachable from the machine running the tests. Which services those are depends on the capabilities the project has installed; its README and environment template are the authority.
|
|
24
24
|
|
|
25
25
|
## Test File Layout
|
|
26
26
|
|
|
27
27
|
```
|
|
28
28
|
tests/
|
|
29
|
-
helpers
|
|
29
|
+
helpers/ # shared setup, factories, harnesses (see below)
|
|
30
30
|
services/
|
|
31
31
|
foo.test.ts # Unit tests (mocked deps)
|
|
32
32
|
foo.integration.test.ts # Integration tests (real services)
|
|
33
33
|
controllers/
|
|
34
|
-
|
|
34
|
+
itemsAPI.test.ts # HTTP route tests
|
|
35
35
|
middleware/
|
|
36
36
|
utils/
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
- `.test.ts` for Jest auto-discovery
|
|
39
|
+
- `.test.ts` for Jest auto-discovery. Keep the mocked tests and the real-service tests for the same subject in **separate files**: `jest.mock` is hoisted above the imports and applies to every test in the module, so one file cannot both stub a dependency and exercise the real one. `.integration.test.ts` is the conventional name for the half that uses the real services.
|
|
40
40
|
- Group by domain: `services/`, `controllers/`, `middleware/`
|
|
41
41
|
|
|
42
|
-
## Test Helpers
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
## Test Helpers
|
|
43
|
+
|
|
44
|
+
Shared test setup belongs in one place under `tests/`, not re-derived in each spec file: booting the app, creating the entities a test needs and tearing them down again, and harnesses that make an awkward dependency — a broker, a queue, a rendered template — drivable from a test.
|
|
45
|
+
|
|
46
|
+
**Read what the project already has before you write one.** These helpers are not scaffolded and cannot be, because they follow whichever capabilities and domain the project has. Every project grows its own set, and the shape varies from one shared module to a directory of purpose-built ones. Reuse what fits and extend it, rather than starting a private copy inside your test file. A helper used by exactly one test is not a helper — leave it in the test.
|
|
47
|
+
|
|
48
|
+
When you do add one, keep two properties. A factory returns a real, persisted entity rather than a hand-built object literal, so the test exercises the same shape production does. And whatever creates data owns its teardown, so one failed run does not poison the next.
|
|
46
49
|
|
|
47
50
|
## Integration Test Data Isolation
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
|
|
52
|
+
Integration tests share their backing services with every other integration test, and with whatever the last run left behind. Three habits keep them from interfering with each other:
|
|
53
|
+
|
|
54
|
+
- **Namespace whatever you create.** A per-run prefix (`_test/${Date.now()}`, a UUID) keeps parallel runs and repeat runs from colliding on unique constraints.
|
|
55
|
+
- **Track created records and remove them in `afterAll`**, each cleanup wrapped in its own `try/catch` so one failure doesn't strand the rest.
|
|
56
|
+
- **Scope instead of deleting where deletes are expensive.** Some stores — append-only logs, analytics columnstores, anything with asynchronous merge semantics — make row deletion slow or eventually-consistent. Isolate those by writing under a test-specific tenant or key and leave the rows in place.
|
|
51
57
|
|
|
52
58
|
## HTTP Route Tests
|
|
53
|
-
Test Express routes via **real HTTP requests** (`fetch()`) against a running local server. This exercises the full middleware chain (auth →
|
|
59
|
+
Test Express routes via **real HTTP requests** (`fetch()`) against a running local server. This exercises the full middleware chain (auth → controller → service → DB → response).
|
|
54
60
|
|
|
55
61
|
Why real HTTP over supertest: supertest skips TCP/listen and can mask middleware ordering or config bugs. Auth middleware is a critical path worth testing through, not around.
|
|
56
62
|
|
|
57
63
|
**Setup:**
|
|
58
64
|
```typescript
|
|
59
65
|
const BASE_URL = process.env.TEST_BASE_URL || 'http://localhost:3000';
|
|
60
|
-
const TEST_API_KEY = process.env.TEST_API_KEY; //
|
|
66
|
+
const TEST_API_KEY = process.env.TEST_API_KEY; // however this project issues API keys
|
|
61
67
|
const authHeaders = { 'Authorization': `Bearer ${TEST_API_KEY}`, 'Content-Type': 'application/json' };
|
|
62
68
|
```
|
|
63
69
|
|
|
64
|
-
**What to cover:** auth rejection (401/403),
|
|
70
|
+
**What to cover:** auth rejection (401/403), request validation (400), the happy-path response shape, and the authorization boundary — that one account cannot reach another's data. If the project gates routes on anything further (feature flags, roles, plan tiers), cover the closed gate too, and assert the status code it actually returns rather than the one that seems right.
|
|
71
|
+
|
|
72
|
+
**Prereqs:** a local server running against a reachable database, a durable test account, and whatever credential the auth middleware expects (`TEST_API_KEY` by convention).
|
|
65
73
|
|
|
66
|
-
|
|
74
|
+
## Ad-Hoc Testing Tools
|
|
67
75
|
|
|
68
|
-
|
|
76
|
+
Jest is what runs in CI and before a commit. Alongside it, projects grow small CLIs for the things a test suite is the wrong shape for: generating fixture data, poking a live local server, checking what actually landed in the database.
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
|---|---|
|
|
72
|
-
| `npm run tester -- <cmd>` | Exploratory smoke tests, fixture data generation (`crawl`, `access-logs-generate`, `s3-raw-smoke`, `user-agent`, etc.) |
|
|
73
|
-
| `npm run admin -- <cmd>` | Entity/data operations (accounts, agents, credentials, CH export/replicate) |
|
|
78
|
+
Boop scaffolds one — `npm run db:query` (database capability) — for reading the local database without opening Prisma Studio. Anything beyond that is project-grown; `package.json` scripts and `src/scripts/` are where to look for what yours has, and `--help` on each is faster than reading its source.
|
|
74
79
|
|
|
75
|
-
|
|
80
|
+
These are exploratory aids. They are not part of the automated suite, and none of them substitutes for a Jest test that can go red on its own.
|
|
76
81
|
|
|
77
82
|
## References
|
|
78
83
|
|
|
79
|
-
- [
|
|
80
|
-
- [
|
|
81
|
-
-
|
|
82
|
-
-
|
|
84
|
+
- [`boop-testing-testing`](../boop-testing-testing/SKILL.md) — testing philosophy and the generic approach
|
|
85
|
+
- [`boop-testing-tdd-light`](../boop-testing-tdd-light/SKILL.md) — the red/green loop for a single change
|
|
86
|
+
- The project's own shared test setup under `tests/` — read it before adding another helper
|
|
87
|
+
- `jest.config.js` at the project root — test discovery, transform, and match config
|