joist-codegen 2.4.0-next.30 → 2.4.0-next.32
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-codegen",
|
|
3
|
-
"version": "2.4.0-next.
|
|
3
|
+
"version": "2.4.0-next.32",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"skills"
|
|
49
49
|
],
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"joist-utils": "2.4.0-next.
|
|
51
|
+
"joist-utils": "2.4.0-next.32",
|
|
52
52
|
"knex": "^3.1.0",
|
|
53
53
|
"pg": "^8.23.0"
|
|
54
54
|
},
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: joist-test-factories
|
|
3
|
-
description: Use when writing or refactoring Joist ORM tests with newTestInstance factories, DeepNew,
|
|
3
|
+
description: Use when writing or refactoring Joist ORM entity or API-layer tests with newTestInstance factories, DeepNew, toMatchEntity, and nested Given graphs, or deciding whether EntityManager isolation with run, makeRun, or RunPlugin is needed.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Joist Test Factories
|
|
7
7
|
|
|
8
|
-
Write
|
|
8
|
+
Write focused, succinct tests that use Joist's factories for setup, retain
|
|
9
9
|
typed entity references through the action, and assert those same entities
|
|
10
10
|
with `toMatchEntity`.
|
|
11
11
|
|
|
12
|
+
Entity tests normally use one `EntityManager` for Given, When, and Then.
|
|
13
|
+
Use `run` for API-layer tests and other production boundaries that need a
|
|
14
|
+
separate `EntityManager`, not as a default wrapper around every test action.
|
|
15
|
+
|
|
12
16
|
Authoritative references:
|
|
13
17
|
|
|
14
18
|
- <https://joist-orm.io/testing/test-factories/>
|
|
@@ -27,7 +31,9 @@ Authoritative references:
|
|
|
27
31
|
4. Keep using the factory-created `DeepNew` graph after same-`EntityManager`
|
|
28
32
|
actions or actions run through Joist's test `run` helper. Do not reload the
|
|
29
33
|
same rows merely to assert against them.
|
|
30
|
-
5.
|
|
34
|
+
5. Keep entity tests in the original test `EntityManager`; they normally do not
|
|
35
|
+
need EM isolation or `RunPlugin`. Use `run` or the project's `makeRun` wrapper
|
|
36
|
+
for API/request-layer tests or another production boundary that needs an
|
|
31
37
|
isolated `EntityManager`; it flushes Given state and mirrors the callback's
|
|
32
38
|
flushed Joist writes into the original test graph.
|
|
33
39
|
Do not `await em.flush()` right before `run` or a `makeRun` helper: it
|
|
@@ -45,6 +51,23 @@ happens, Then the state of the world is Z": the factory-created graph is the
|
|
|
45
51
|
Given, the code under test is the When, and `toMatchEntity` proves the Then.
|
|
46
52
|
Mark the three phases with `// Given`, `// When`, `// Then` comments:
|
|
47
53
|
|
|
54
|
+
For an entity test, act directly on the factory-created entity:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
it.withCtx("updates a book's title", async (ctx) => {
|
|
58
|
+
// Given a book with its original title
|
|
59
|
+
const book = newBook(ctx.em, { title: "Before" });
|
|
60
|
+
|
|
61
|
+
// When we change the book's title
|
|
62
|
+
book.title = "After";
|
|
63
|
+
|
|
64
|
+
// Then the book has the new title
|
|
65
|
+
expect(book).toMatchEntity({ title: "After" });
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
For an API-layer test, use `run` to model its separate production unit of work:
|
|
70
|
+
|
|
48
71
|
```ts
|
|
49
72
|
it.withCtx("updates a book", async (ctx) => {
|
|
50
73
|
// Given an author with one book
|
|
@@ -245,6 +268,23 @@ test-only loads.
|
|
|
245
268
|
|
|
246
269
|
## Use run for Production Isolation
|
|
247
270
|
|
|
271
|
+
Choose isolation based on the behavior under test:
|
|
272
|
+
|
|
273
|
+
- **Entity tests:** Test entity methods, properties, relationships, and domain
|
|
274
|
+
logic directly with the test's `ctx.em` and factory-created entities. These
|
|
275
|
+
normally do not need `run`, a second EM, or a manually installed `RunPlugin`.
|
|
276
|
+
Flush only when the behavior requires it, such as validation, flush hooks,
|
|
277
|
+
persisted reactive fields, or database constraints. Needing a flush does not
|
|
278
|
+
by itself mean the test needs EM isolation.
|
|
279
|
+
- **API/request-layer tests:** Use `run` or an existing project wrapper such as
|
|
280
|
+
`runMutation` when testing a resolver, endpoint, or service entry point whose
|
|
281
|
+
production unit of work uses a fresh EM. This ensures the action loads its
|
|
282
|
+
own entities rather than inheriting the test's loaded factory graph.
|
|
283
|
+
- **Other boundaries:** Use `run` when a fresh production unit of work is part
|
|
284
|
+
of the behavior being tested. A service/helper that operates on existing
|
|
285
|
+
entities or the caller's EM does not need isolation merely because it is
|
|
286
|
+
production code.
|
|
287
|
+
|
|
248
288
|
Joist's `run(ctx, fn)` executes `fn` with a fresh production-style context and
|
|
249
289
|
`EntityManager`. Before the callback, it flushes the test factory graph. As the
|
|
250
290
|
callback flushes Joist writes, `RunPlugin` mirrors those writes into the
|
|
@@ -252,6 +292,11 @@ original test `EntityManager`. It does not call `EntityManager.refresh`. The
|
|
|
252
292
|
callback result is also mapped back to entities from the original test
|
|
253
293
|
`EntityManager`.
|
|
254
294
|
|
|
295
|
+
`run` installs `RunPlugin` automatically on the isolated EM. The plugin bridges
|
|
296
|
+
that production unit of work back to the retained test entities; it is not
|
|
297
|
+
needed when Given, When, and Then already share one EM. Prefer `run` or the
|
|
298
|
+
project's wrapper over manually wiring the plugin.
|
|
299
|
+
|
|
255
300
|
```ts
|
|
256
301
|
const author = newAuthor(ctx.em, { books: [{ title: "Before" }] });
|
|
257
302
|
const [book] = author.books.get;
|
|
@@ -428,6 +473,8 @@ reuse decisions instead of replacing factories with manual setup.
|
|
|
428
473
|
- Given state uses minimal factory opts and does not invoke the behavior under
|
|
429
474
|
test.
|
|
430
475
|
- Important entities have direct, role-based `const` names.
|
|
476
|
+
- Entity tests use the original test EM unless the behavior specifically needs
|
|
477
|
+
a separate production unit of work; a flush alone does not require `run`.
|
|
431
478
|
- Separate production units of work use `run`/`makeRun` and flush normally.
|
|
432
479
|
- Assertions reuse loaded factory entities with `toMatchEntity`.
|
|
433
480
|
- Side-effect assertions follow domain relations or action results instead of
|