@hailstails/hone 2.0.0-pre.4 → 2.0.0-pre.5
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/ADOPTING.md +4 -15
- package/CHANGELOG.md +34 -0
- package/MIGRATING.md +6 -6
- package/README.md +3 -4
- package/chunks/{chunk-FPIP7JEC.js → chunk-4AJQHZ7I.js} +1 -1
- package/chunks/{chunk-CXM5KBZX.js → chunk-6LTJRJOE.js} +1 -1
- package/chunks/{chunk-PVSC4SE3.js → chunk-BISP2JOL.js} +4 -6
- package/chunks/{chunk-YGMO5Z6U.js → chunk-EFUWHLZK.js} +187 -193
- package/chunks/{chunk-BRANVR7K.js → chunk-FRE5IA3C.js} +1 -1
- package/chunks/{chunk-E4D5WDRX.js → chunk-GMLT3MHO.js} +252 -254
- package/chunks/chunk-M4VSJ27S.js +139 -0
- package/chunks/chunk-ND3EOGDK.js +2350 -0
- package/chunks/chunk-ONDBACCR.js +14332 -0
- package/chunks/{chunk-LD766VWU.js → chunk-U6W65ZOF.js} +31 -2
- package/chunks/{chunk-BKQ3K766.js → chunk-WPEOCLTP.js} +39 -33
- package/chunks/{chunk-A3RL4O5V.js → chunk-XNIXPQQ5.js} +35 -2
- package/docs/extending.md +2 -2
- package/methods/lenses/function-first.md +2 -2
- package/package.json +1 -1
- package/rules/README.md +5 -3
- package/rules/SPIRIT.md +1 -1
- package/rules/numbers.md +4 -4
- package/rules/principles/no-leak-across-a-boundary.md +1 -1
- package/rules/principles.md +1 -1
- package/rules/record-kinds/authorised-location.md +1 -1
- package/rules/records.md +11 -19
- package/rules/rules.md +5 -5
- package/rules/sources.md +6 -7
- package/rules/typescript/checks.md +55 -387
- package/rules/typescript/recipes/adapter-per-outside-party.md +10 -5
- package/rules/typescript/recipes/assert-to-narrow.md +114 -4
- package/rules/typescript/recipes/closure-factory.md +5 -5
- package/rules/typescript/recipes/companion-test-file.md +5 -3
- package/rules/typescript/recipes/composed-orchestrator.md +6 -4
- package/rules/typescript/recipes/composition-root.md +5 -3
- package/rules/typescript/recipes/{bounded-context-generic.md → constrained-context-generic.md} +6 -4
- package/rules/typescript/recipes/copy-never-write-in.md +6 -4
- package/rules/typescript/recipes/declared-not-bundled.md +5 -3
- package/rules/typescript/recipes/dependency-record.md +5 -3
- package/rules/typescript/recipes/domain-vocabulary-list.md +5 -3
- package/rules/typescript/recipes/entity-module.md +8 -5
- package/rules/typescript/recipes/exact-pins-and-a-lock-file.md +5 -3
- package/rules/typescript/recipes/extension-outside-the-vendored-code.md +5 -3
- package/rules/typescript/recipes/failure-asserted-as-a-value.md +44 -4
- package/rules/typescript/recipes/failure-mapping-at-the-adapter.md +8 -5
- package/rules/typescript/recipes/fix-the-declaration-not-the-value.md +5 -3
- package/rules/typescript/recipes/functional-iteration.md +8 -3
- package/rules/typescript/recipes/guard-clauses.md +5 -3
- package/rules/typescript/recipes/integration-test-owns-its-scratch.md +5 -3
- package/rules/typescript/recipes/integration-test-suffix.md +5 -3
- package/rules/typescript/recipes/it-each-over-typed-rows.md +117 -0
- package/rules/typescript/recipes/map-out-at-the-adapter.md +36 -4
- package/rules/typescript/recipes/meaning-in-names-not-comments.md +5 -5
- package/rules/typescript/recipes/named-exported-type.md +5 -3
- package/rules/typescript/recipes/narrow-ports.md +5 -3
- package/rules/typescript/recipes/one-argument-object.md +8 -3
- package/rules/typescript/recipes/{port-in-the-domain.md → port-in-a-module-of-its-own.md} +7 -5
- package/rules/typescript/recipes/ports-in-the-argument.md +5 -3
- package/rules/typescript/recipes/repo-scripts-only.md +5 -3
- package/rules/typescript/recipes/result-union.md +5 -5
- package/rules/typescript/recipes/screaming-folders.md +5 -5
- package/rules/typescript/recipes/shared-strict-base-config.md +5 -5
- package/rules/typescript/recipes/stub-with-vi-fn.md +130 -0
- package/rules/typescript/recipes/tagged-union-and-exhaustive-switch.md +5 -3
- package/rules/typescript/recipes/whole-words.md +5 -3
- package/rules/typescript/recipes/whole-written-out-expectation.md +284 -4
- package/rules/typescript/recipes/world-values-through-a-port.md +5 -3
- package/rules/typescript/recipes/zod-at-the-producers-adapter.md +8 -3
- package/rules/typescript/recipes.md +567 -21
- package/rules/typescript/symptoms/ambient-randomness.md +1 -1
- package/rules/typescript/symptoms/answer-set-in-a-hook.md +1 -1
- package/rules/typescript/symptoms/{assertion-outside-a-test → assertion-outside-a-test-body}/assertion-site.md +1 -1
- package/rules/typescript/symptoms/{assertion-outside-a-test.md → assertion-outside-a-test-body.md} +1 -1
- package/rules/typescript/symptoms/branch-in-a-test/test-body.md +10 -184
- package/rules/typescript/symptoms/branch-in-a-test.md +11 -1
- package/rules/typescript/symptoms/claimed-type/non-null-assertions.md +1 -1
- package/rules/typescript/symptoms/{code-or-prose-read-as-text.md → code-or-prose-read-as-text-in-a-test.md} +2 -2
- package/rules/typescript/symptoms/computed-value-in-a-test/test-body.md +26 -117
- package/rules/typescript/symptoms/computed-value-in-a-test.md +1 -1
- package/rules/typescript/symptoms/date-used-directly.md +1 -1
- package/rules/typescript/symptoms/default-for-a-required-value.md +1 -1
- package/rules/typescript/symptoms/double-with-a-body/logic-in-a-passed-function.md +1 -18
- package/rules/typescript/symptoms/double-with-a-body.md +1 -1
- package/rules/typescript/symptoms/{export-from-a-test-file.md → export-from-a-test.md} +1 -1
- package/rules/typescript/symptoms/failure-shaped-as-success.md +1 -1
- package/rules/typescript/symptoms/inherited-environment-in-a-subprocess/git-spawn-environment.md +1 -1
- package/rules/typescript/symptoms/inner-layer-imports-outer.md +1 -1
- package/rules/typescript/symptoms/library-class-built-in-place.md +1 -1
- package/rules/typescript/symptoms/loop-keyword/loop-statement.md +1 -1
- package/rules/typescript/symptoms/outside-import-past-a-boundary/import-or-require.md +1 -0
- package/rules/typescript/symptoms/outside-import-past-a-boundary.md +5 -4
- package/rules/typescript/symptoms/outside-type-in-a-signature.md +2 -2
- package/rules/typescript/symptoms/{port-outside-the-domain.md → port-beside-its-adapter.md} +2 -2
- package/rules/typescript/symptoms/port-named-for-a-mechanism.md +1 -1
- package/rules/typescript/symptoms/process-global-used-in-place.md +1 -1
- package/rules/typescript/symptoms/rebinding/test-body.md +1 -18
- package/rules/typescript/symptoms/rebinding.md +11 -1
- package/rules/typescript/symptoms/schema-past-its-adapter.md +2 -2
- package/rules/typescript/symptoms/text-past-its-reader/test-body.md +2 -17
- package/rules/typescript/symptoms/text-past-its-reader.md +1 -1
- package/rules/typescript/symptoms/throw-or-catch-in-a-test/try-block.md +1 -25
- package/rules/typescript/symptoms/type-declared-in-a-test/test-type-declaration.md +1 -1
- package/rules/typescript/symptoms/unasserted-call.md +1 -1
- package/rules/typescript/symptoms.md +35 -35
- package/src/domain/language-layer/authored-item-types.d.ts +21 -5
- package/src/domain/runner/runner-result-types.d.ts +1 -2
- package/src/eslint-plugin.js +40 -38
- package/src/hone.js +15 -13638
- package/src/infrastructure/disciplines-file/version-one-names.json +4 -5
- package/src/infrastructure/engines/eslint/runner/runner.js +55 -45
- package/src/infrastructure/engines/walker/{assertion-outside-a-test → assertion-outside-a-test-body}/assertion-site/assertion-site.js +1 -1
- package/src/infrastructure/engines/walker/branch-in-a-test/test-body/test-body-logic.js +39 -65
- package/src/infrastructure/engines/walker/{code-or-prose-read-as-text → code-or-prose-read-as-text-in-a-test}/raw-read/raw-read.js +1 -1
- package/src/infrastructure/engines/walker/{export-from-a-test-file → export-from-a-test}/test-file-export/test-file-export.js +1 -1
- package/src/infrastructure/engines/walker/runner/runner.js +10 -10
- package/src/infrastructure/engines/walker/strictness-weakened/strict/compiler-overrides.js +3 -3
- package/src/infrastructure/engines/walker/type-declared-in-a-test/test-type-declaration/test-type-declaration.examples.json +15 -3
- package/src/infrastructure/engines/walker/type-declared-in-a-test/test-type-declaration/test-type-declaration.js +118 -4
- package/src/measure-engines.process.js +80 -0
- package/src/testing.js +81 -9
- package/chunks/chunk-7FVSZ2VL.js +0 -1304
- package/chunks/chunk-BIDD2YWP.js +0 -398
- package/rules/record-kinds/boundary-record.md +0 -9
- package/rules/typescript/recipes/iteach-over-typed-rows.md +0 -18
- package/rules/typescript/recipes/stub-with-vifn.md +0 -20
- /package/rules/typescript/symptoms/{code-or-prose-read-as-text → code-or-prose-read-as-text-in-a-test}/raw-read.md +0 -0
- /package/rules/typescript/symptoms/{export-from-a-test-file → export-from-a-test}/test-file-export.md +0 -0
- /package/src/infrastructure/engines/walker/{assertion-outside-a-test → assertion-outside-a-test-body}/assertion-site/assertion-site.examples.json +0 -0
- /package/src/infrastructure/engines/walker/{assertion-outside-a-test → assertion-outside-a-test-body}/assertion-site/piece.json +0 -0
- /package/src/infrastructure/engines/walker/{code-or-prose-read-as-text → code-or-prose-read-as-text-in-a-test}/raw-read/piece.json +0 -0
- /package/src/infrastructure/engines/walker/{code-or-prose-read-as-text → code-or-prose-read-as-text-in-a-test}/raw-read/raw-read.examples.json +0 -0
- /package/src/infrastructure/engines/walker/{export-from-a-test-file → export-from-a-test}/test-file-export/piece.json +0 -0
- /package/src/infrastructure/engines/walker/{export-from-a-test-file → export-from-a-test}/test-file-export/test-file-export.examples.json +0 -0
|
@@ -2,17 +2,49 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"principle": "no-leak-across-a-boundary"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"As we should be runtime objects not json as soon as we leave a boundary\".",
|
|
11
13
|
"standing": "accepted"
|
|
12
|
-
}
|
|
14
|
+
},
|
|
15
|
+
"examples": [
|
|
16
|
+
{
|
|
17
|
+
"anchor": "serialising-or-parsing-json",
|
|
18
|
+
"answers": [
|
|
19
|
+
{
|
|
20
|
+
"check": "text-past-its-reader#test-body",
|
|
21
|
+
"form": "serialising-or-parsing-json"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
13
26
|
}
|
|
14
27
|
```
|
|
15
28
|
|
|
16
29
|
- **Method:** Business logic hands the adapter our own value. Only the adapter turns it into the receiver's form (`JSON.stringify`, a status, a header).
|
|
17
30
|
- **You can tell because:** No outside form is built in business logic.
|
|
18
31
|
- **Industry name:** anticorruption layer (Evans).
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
### Serialising or parsing JSON
|
|
36
|
+
```ts
|
|
37
|
+
declare const publish: (request: { readonly id: string; readonly send: (text: string) => void }) => void;
|
|
38
|
+
const send = vi.fn<(text: string) => void>();
|
|
39
|
+
publish({ id: "a", send });
|
|
40
|
+
expect(send).toHaveBeenCalledWith(JSON.stringify({ id: "a" }));
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Hand the adapter our own value, and leave the text to it:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
declare const publish: (request: { readonly id: string; readonly send: (task: { readonly id: string }) => void }) => void;
|
|
47
|
+
const send = vi.fn<(task: { readonly id: string }) => void>();
|
|
48
|
+
publish({ id: "a", send });
|
|
49
|
+
expect(send).toHaveBeenCalledWith({ id: "a" });
|
|
50
|
+
```
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "one-word-everywhere"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"If the code isn't self explanatory that's the problem. You shouldn't need usage notes at all.\"",
|
|
@@ -17,6 +19,4 @@
|
|
|
17
19
|
- **You can tell because:** No comment or JSDoc in our code.
|
|
18
20
|
- **Industry name:** none; ours. It is stricter than Martin's *Clean Code*, which keeps a section on good comments.
|
|
19
21
|
|
|
20
|
-
[One argument object](one-argument-object.md) also serves this rule.
|
|
21
|
-
|
|
22
22
|
Casing is a check that runs the adopter's own lint. It is not a recipe. Basic logic in the language's own constructs is a case under the principle; its recipe is the native array and object methods, chosen because an outside dependency's language for them is "language obscuring standard operations".
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "declared-not-asserted"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "A derived type couples the consumer to the producer's implementation, not its contract.",
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "handed-what-it-uses"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"Rather than writing 6 different concrete things 1 for each.\"; \"narrowed ports that still help us find the seams where the dependencies link up\".",
|
|
@@ -2,9 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "handed-what-it-uses"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"rule": "one-word-everywhere"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
8
13
|
"standing": "accepted",
|
|
9
14
|
"reason": {
|
|
10
15
|
"text": "\"Shows up in tests where to avoid writing a large object for 2 fields that impact th test they double cast. It shows the minimum interface principle.\"; \"Functions with no outside influence should take 1. 2 is debatable. But for agents the goal is always 1.\"; \"nope, you will not use tupple like arrays for data\".",
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
# Port in
|
|
1
|
+
# Port in a module of its own
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "ports-declared-inside"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"In clean architecture the port is domain and to be kept separate to the concrete implementation or use.\"; \"then host facts source should be declared separately to its concrete implementation to protiect dependency inversion\".",
|
|
@@ -13,7 +15,7 @@
|
|
|
13
15
|
}
|
|
14
16
|
```
|
|
15
17
|
|
|
16
|
-
- **Method:** The port's interface is declared
|
|
18
|
+
- **Method:** The port's interface is declared on the inside, in a module of its own, named for the capability. It sits apart from the adapter that fulfils it and from the unit that uses it; both import it from there. Storage is such a port, named for what the domain asks.
|
|
17
19
|
- **You can tell because:** No port is declared in an adapter's module or in its consumer's. No business-logic file imports an adapter. No query or driver type in business logic.
|
|
18
20
|
- **Industry name:** dependency inversion (Martin); Clean Architecture.
|
|
19
21
|
- **Review:** storage sits behind a repository port and domain code never imports a storage driver
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "written-out-answers"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"If it calls something and uses its output we hardcode that output using DI.\"; \"there should be absolutely no reason to be setting process env in a test, if you are doing this you have severely messed up your dependency management\".",
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "tools-live-in-the-repo"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"if this is common script it should be in package json, not run through tsx\"; \"I don't want a custom script, can you get something more standardised and provided by open source or something?\"",
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "failures-are-values"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"it makes the output clear to type system instead of hidden in throws\".",
|
|
@@ -17,6 +19,4 @@
|
|
|
17
19
|
- **You can tell because:** No `throw` between units of ours. No failure is returned as `null`, `false`, `[]` or a default.
|
|
18
20
|
- **Industry name:** Result type (Wlaschin).
|
|
19
21
|
|
|
20
|
-
[Failure mapping at the adapter](failure-mapping-at-the-adapter.md) also serves this rule.
|
|
21
|
-
|
|
22
22
|
- **Review:** operational errors are tagged data built by a factory and dispatched on their tag
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "one-reason-per-module"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"all types have a purpose. that purpose is its reason to exist and its reason to change\"; screaming architecture \"making those boundaries easier to see when broken\".",
|
|
@@ -16,5 +18,3 @@
|
|
|
16
18
|
- **Method:** Folders and files are named for the domain capability they hold. None is called utils, helpers, types or common.
|
|
17
19
|
- **You can tell because:** Each path names a domain concept.
|
|
18
20
|
- **Industry name:** Screaming Architecture (Martin); package by feature.
|
|
19
|
-
|
|
20
|
-
[Entity module](entity-module.md) also serves this rule.
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "declared-not-asserted"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "One place sets strictness, so no project weakens it unseen.",
|
|
@@ -16,5 +18,3 @@
|
|
|
16
18
|
- **Method:** All strict flags sit in one base config that every `tsconfig.json` extends. No file turns one off.
|
|
17
19
|
- **You can tell because:** The resolved config of every project has every flag on.
|
|
18
20
|
- **Industry name:** none; ours.
|
|
19
|
-
|
|
20
|
-
[Zod at the producer's adapter](zod-at-the-producers-adapter.md) and [Assert to narrow](assert-to-narrow.md) also serve this rule. The framework's assertion is not a claim of ours.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Stub with vi.fn
|
|
2
|
+
|
|
3
|
+
```json
|
|
4
|
+
{
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "written-out-answers"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
10
|
+
"standing": "accepted",
|
|
11
|
+
"reason": {
|
|
12
|
+
"text": "\"these should be vi.fn, with mockResolvedValue, not custom async functions\"; \"Agents take fake to mean whatever they want it to mean right now.\" The framework's double has no body of ours to hold logic.",
|
|
13
|
+
"standing": "accepted"
|
|
14
|
+
},
|
|
15
|
+
"examples": [
|
|
16
|
+
{
|
|
17
|
+
"anchor": "an-optional-chain-on-a-mock-call",
|
|
18
|
+
"answers": [
|
|
19
|
+
{
|
|
20
|
+
"check": "branch-in-a-test#test-body",
|
|
21
|
+
"form": "an-optional-chain-on-a-mock-call"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"anchor": "an-optional-chain-on-a-mock-result",
|
|
27
|
+
"answers": [
|
|
28
|
+
{
|
|
29
|
+
"check": "branch-in-a-test#test-body",
|
|
30
|
+
"form": "an-optional-chain-on-a-mock-result"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"anchor": "logic-inside-a-function-the-test-passes-on",
|
|
36
|
+
"answers": [
|
|
37
|
+
{
|
|
38
|
+
"check": "double-with-a-body#logic-in-a-passed-function",
|
|
39
|
+
"form": "logic-inside-a-function-the-test-passes-on"
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"anchor": "a-let",
|
|
45
|
+
"answers": [
|
|
46
|
+
{
|
|
47
|
+
"check": "rebinding#test-body",
|
|
48
|
+
"form": "a-let"
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- **Method:** Each collaborator is a `vi.fn()` declared as a const inside the test that uses it. Shared boilerplate or a mock builder is allowed where it hides none of three things: a wide interface, an assumption a test rests on, or coupling between tests. A shared double is therefore a symptom, suspect by default, not a breach. Its answer is `mockReturnValue` or `mockResolvedValue` with a written-out value. Each call is checked with `toHaveBeenCalledWith` and a written-out argument.
|
|
57
|
+
- **You can tell because:** No double has a body, a branch or a table. Every call the unit makes is asserted.
|
|
58
|
+
- **Industry name:** stub and spy; Test Stub (Meszaros, *xUnit Test Patterns*).
|
|
59
|
+
|
|
60
|
+
A fake is not a recipe. It is a [deviation record](../../record-kinds/deviation.md), and the bar it must meet is: "Moving a fake is only allowed if the test cannot be done with deterministic stubs or hardcoded things."
|
|
61
|
+
|
|
62
|
+
## Examples
|
|
63
|
+
|
|
64
|
+
### An optional chain on a mock call
|
|
65
|
+
```ts
|
|
66
|
+
declare const start: (run: (task: { readonly id: string }) => void) => void;
|
|
67
|
+
const run = vi.fn<(task: { readonly id: string }) => void>();
|
|
68
|
+
start(run);
|
|
69
|
+
expect(run.mock.calls[0]?.[0].id).toBe("a");
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Check the call, its argument written out:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
declare const start: (run: (task: { readonly id: string }) => void) => void;
|
|
76
|
+
const run = vi.fn<(task: { readonly id: string }) => void>();
|
|
77
|
+
start(run);
|
|
78
|
+
expect(run).toHaveBeenCalledWith({ id: "a" });
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### An optional chain on a mock result
|
|
82
|
+
```ts
|
|
83
|
+
declare const pricing: { readonly priceOf: (amount: number) => number };
|
|
84
|
+
declare const settle: (order: { readonly amount: number; readonly priceOf: (amount: number) => number }) => number;
|
|
85
|
+
const priceOf = vi.spyOn(pricing, "priceOf");
|
|
86
|
+
settle({ amount: 2, priceOf: pricing.priceOf });
|
|
87
|
+
expect(priceOf.mock.results[0]?.value).toBe(4);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Write out what the collaborator gives back, then check what the unit returns and the call it makes:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
declare const settle: (order: { readonly amount: number; readonly priceOf: (amount: number) => number }) => number;
|
|
94
|
+
const priceOf = vi.fn<(amount: number) => number>().mockReturnValue(4);
|
|
95
|
+
expect(settle({ amount: 2, priceOf })).toBe(4);
|
|
96
|
+
expect(priceOf).toHaveBeenCalledWith(2);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Logic inside a function the test passes on
|
|
100
|
+
```ts
|
|
101
|
+
declare const isInstalled: (fileKind: { readonly kindOf: (path: string) => "file" | "missing" }) => "installed" | "missing";
|
|
102
|
+
const installedFiles = ["bin/hone.js"];
|
|
103
|
+
expect(isInstalled({ kindOf: (path) => (installedFiles.includes(path) ? "file" : "missing") })).toBe("installed");
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Make it a `vi.fn` with a written-out return, and check the call:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
declare const isInstalled: (fileKind: { readonly kindOf: (path: string) => "file" | "missing" }) => "installed" | "missing";
|
|
110
|
+
const kindOf = vi.fn<(path: string) => "file" | "missing">().mockReturnValue("file");
|
|
111
|
+
expect(isInstalled({ kindOf })).toBe("installed");
|
|
112
|
+
expect(kindOf).toHaveBeenCalledExactlyOnceWith("bin/hone.js");
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### A let
|
|
116
|
+
```ts
|
|
117
|
+
declare const record: (store: { readonly save: (text: string) => void }) => void;
|
|
118
|
+
let saved = "";
|
|
119
|
+
record({ save: (text) => { saved = text; } });
|
|
120
|
+
expect(saved).toBe("a");
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Pass in a `vi.fn`, and check the call:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
declare const record: (store: { readonly save: (text: string) => void }) => void;
|
|
127
|
+
const save = vi.fn<(text: string) => void>();
|
|
128
|
+
record({ save });
|
|
129
|
+
expect(save).toHaveBeenCalledWith("a");
|
|
130
|
+
```
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "kinds-are-named"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"boolean inputs are a smell, disallowed\"; \"no boolean inputs, breaks clean code\".",
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
```json
|
|
4
4
|
{
|
|
5
|
-
"serves":
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
"serves": [
|
|
6
|
+
{
|
|
7
|
+
"rule": "one-word-everywhere"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
8
10
|
"standing": "accepted",
|
|
9
11
|
"reason": {
|
|
10
12
|
"text": "\"dont care if its a lambda function or not, meaningful names always\"; \"no, the problem is the lack of a whole word\".",
|