@codefast/di-testing 0.2.0 → 0.4.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 +58 -0
- package/README.md +13 -11
- package/dist/discovery/mock-binder.js +5 -4
- package/dist/test-bed/bed-builder.d.ts +1 -1
- package/dist/test-bed/bed-builder.js +3 -3
- package/dist/test-bed/sociable-builder.js +2 -1
- package/dist/test-bed/unit-test-bed.d.ts +1 -1
- package/dist/test-bed/unit-test-bed.js +5 -5
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,63 @@
|
|
|
1
1
|
# @codefast/di-testing
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#1101](https://github.com/codefastlabs/codefast/pull/1101) **Breaking:** a test bed's `dispose()` is renamed `disposeAsync()`, matching `@codefast/di`'s `Container#disposeAsync()`
|
|
8
|
+
— it returns a `Promise`, so it carries the `Async` suffix. `await using bed = …compile()` is unchanged; replace
|
|
9
|
+
`await bed.dispose()` with `await bed.disposeAsync()`.
|
|
10
|
+
|
|
11
|
+
- [#1068](https://github.com/codefastlabs/codefast/pull/1068) Lower the Node.js floor from 24 to the lowest release the package, its dependencies and its full test suite run on:
|
|
12
|
+
22.12 for `@codefast/cli` and `@codefast/tailwind-variants`, 22.18 for `@codefast/di` and `@codefast/di-testing`, and
|
|
13
|
+
22.22.2 for `@codefast/theme`, `@codefast/tracking` and `@codefast/ui`. Each floor is type-checked against what Node 22
|
|
14
|
+
ships and proven by running the package's tests on that release in CI.
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- [#1120](https://github.com/codefastlabs/codefast/pull/1120) The README and the test bed's internals follow `@codefast/di`'s rename of `optional()` to `injectOptional()`.
|
|
19
|
+
- Updated dependencies:
|
|
20
|
+
- @codefast/di@0.14.0
|
|
21
|
+
|
|
22
|
+
## 0.3.0
|
|
23
|
+
|
|
24
|
+
### Minor Changes
|
|
25
|
+
|
|
26
|
+
- [#999](https://github.com/codefastlabs/codefast/pull/999) Requires `@codefast/di` 0.12, whose chains declare a binding's slot before its strategy; the test beds bind their mocks
|
|
27
|
+
and real collaborators in that order.
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- [#1005](https://github.com/codefastlabs/codefast/pull/1005) **Breaking for deep subpath imports only** — the root entry `@codefast/di` is unchanged. `src/` now follows three rules,
|
|
32
|
+
and the published subpaths follow `src/` with no exception: a directory names a family of two or more modules and a lone
|
|
33
|
+
module sits flat at its layer; a directory carries the topic and a file its role, so no file repeats its directory's
|
|
34
|
+
name; and the exports map is the source tree, so the `strip` that kept the introspection modules at flat specifiers is
|
|
35
|
+
gone.
|
|
36
|
+
|
|
37
|
+
Moves: the `MetadataReader` verification leaves `resolution/cache/class-introspector` for `metadata/verify` — it was the
|
|
38
|
+
one value import pointing up from `metadata/` into `resolution/`; the `RESOLUTION_DIAGNOSTICS` channel leaves `errors/`
|
|
39
|
+
for `introspection/diagnostics`; the fluent chain's contract leaves `core/binding` for `core/binding-builders`, and the
|
|
40
|
+
class implementing it is `container/binding-chain`; `effectiveBindingScope` folds into `core/binding`.
|
|
41
|
+
|
|
42
|
+
Renamed specifiers: `./errors/errors` → `./errors`; `./errors/diagnostics` → `./introspection/diagnostics`;
|
|
43
|
+
`./ambient/active-container` → `./ambient-container`; `./container/binding-builders` → `./container/binding-chain`;
|
|
44
|
+
`./core/binding-scope` → gone (`effectiveBindingScope` is in `./core/binding`); `./injection/resolve-options` →
|
|
45
|
+
`./injection/dependency-slot`;
|
|
46
|
+
`./metadata/{metadata-keys,metadata-types,metadata-reader-token,symbol-metadata-reader,verifying-metadata-reader}` →
|
|
47
|
+
`./metadata/{keys,types,reader-token,symbol-reader,verifying-reader}`; `./lifecycle/{lifecycle-manager,scope-manager}` →
|
|
48
|
+
`./lifecycle/{hooks,scopes}`; `./decorators/{lifecycle-decorators,decorator-metadata}` →
|
|
49
|
+
`./decorators/{lifecycle,metadata-record}`; `./resolution/path/resolution-path` → `./resolution/path`;
|
|
50
|
+
`./resolution/plan/{instantiation-plan,plan-codegen}` → `./resolution/plan/{compiler,codegen}`;
|
|
51
|
+
`./resolution/select/binding-select` → `./resolution/select/candidates`; `./resolution/cache/binding-lookup-cache` →
|
|
52
|
+
`./resolution/cache/lookup`; `./{inspector,explanation,dependency-graph,graph-adapters/*}` →
|
|
53
|
+
`./introspection/{inspector,explanation,dependency-graph,graph-adapters/*}`. New: `./core/binding-builders`,
|
|
54
|
+
`./metadata/verify`.
|
|
55
|
+
|
|
56
|
+
`@codefast/di-testing` follows the one specifier it imports, `metadata/verifying-reader`.
|
|
57
|
+
|
|
58
|
+
- Updated dependencies:
|
|
59
|
+
- @codefast/di@0.12.0
|
|
60
|
+
|
|
3
61
|
## 0.2.0
|
|
4
62
|
|
|
5
63
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -33,10 +33,11 @@ production — and you assert against the mocks the bed created.
|
|
|
33
33
|
pnpm add -D @codefast/di-testing
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
`@codefast/di-testing` requires Node.js
|
|
37
|
-
the same TypeScript setup
|
|
38
|
-
types, from `@types/node`
|
|
39
|
-
versioned on its own track: breaking changes ship as minor versions, so pin the
|
|
36
|
+
`@codefast/di-testing` requires Node.js 22.18 or later, TypeScript 7 or later, and a peer install of `@codefast/di`,
|
|
37
|
+
with the same TypeScript setup and [toolchain](../di/README.md#toolchain): standard decorators, `experimentalDecorators`
|
|
38
|
+
off, and the explicit resource management types, from `@types/node` 22 or later or from `ESNext.Disposable` in `lib`.
|
|
39
|
+
The package is published on 0.x and versioned on its own track: breaking changes ship as minor versions, so pin the
|
|
40
|
+
minor version when you need stability.
|
|
40
41
|
|
|
41
42
|
## Quick start
|
|
42
43
|
|
|
@@ -95,9 +96,10 @@ The zero-dependency default is the `TestBed` export itself. Assert against the b
|
|
|
95
96
|
with `.mockReturnValue()`:
|
|
96
97
|
|
|
97
98
|
```ts
|
|
98
|
-
import { TestBed } from "@codefast/di-testing";
|
|
99
99
|
import assert from "node:assert/strict";
|
|
100
100
|
|
|
101
|
+
import { TestBed } from "@codefast/di-testing";
|
|
102
|
+
|
|
101
103
|
const { unit, mocks } = TestBed.solitary(OrderProcessor).compile();
|
|
102
104
|
unit.placeOrder("u1", 42);
|
|
103
105
|
assert.deepEqual(mocks.get(PaymentGatewayToken).charge.mock.calls[0], ["u1", 42]);
|
|
@@ -133,8 +135,8 @@ The builder records overrides, then compiles:
|
|
|
133
135
|
`fn().returns(...)` (Sinon) type-check against the factory you chose.
|
|
134
136
|
- `.mock(token).using(value)` — bind a fixed value. The value is **sealed**: it has no mock surface, so `mocks.get`
|
|
135
137
|
refuses it rather than hand it back mistyped — the test already holds the reference it passed in.
|
|
136
|
-
- `.mock(token).absent()` — leave the dependency unbound: an `
|
|
137
|
-
slot `[]`. On a required dependency this is an `OverrideMismatchError`.
|
|
138
|
+
- `.mock(token).absent()` — leave the dependency unbound: an `injectOptional()` slot resolves `undefined`, an
|
|
139
|
+
`injectAll()` slot `[]`. On a required dependency this is an `OverrideMismatchError`.
|
|
138
140
|
- `.mock(token).usingAll([a, b])` — supply the elements of an unconstrained `injectAll()` slot, in order. Sealed like
|
|
139
141
|
`.using`.
|
|
140
142
|
- `.mock(token, { name })` / `.mock(token, { tag })` — target one slot of a token that is injected several ways; the
|
|
@@ -209,8 +211,8 @@ it("prices through the real PricingService over a mocked tax boundary", () => {
|
|
|
209
211
|
|
|
210
212
|
- A class whose constructor takes no parameters is testable without `@injectable`; a parameterful constructor with no
|
|
211
213
|
metadata is a `NotInjectableError`.
|
|
212
|
-
- An `
|
|
213
|
-
optional would give in production. Use `.mock(token).absent()` to exercise the absent branch.
|
|
214
|
+
- An `injectOptional()` dependency is auto-mocked like any other — it resolves to the mock, not to the `undefined` an
|
|
215
|
+
unbound optional would give in production. Use `.mock(token).absent()` to exercise the absent branch.
|
|
214
216
|
- An `injectAll()` dependency receives a one-element array holding the token's mock; use `.mock(token).usingAll([...])`
|
|
215
217
|
to supply several elements.
|
|
216
218
|
- Named or tagged parameters of one token share the token's mock unless a slot-targeted `.mock(token, { name })` gives
|
|
@@ -227,7 +229,7 @@ it("prices through the real PricingService over a mocked tax boundary", () => {
|
|
|
227
229
|
- `mocks.get(token, options?)` — the `Mocked<T>` bound for a dependency, or for one slot of it. Only auto-mocks and
|
|
228
230
|
`.stub` stubs come back; sealed values and exposed classes throw `SealedDependencyError`.
|
|
229
231
|
- `resetMocks()` — clear the call history and configured behaviour of every auto-mock and stub the bed created.
|
|
230
|
-
- `
|
|
232
|
+
- `disposeAsync()` — run the unit's `@preDestroy` hooks and dispose the container.
|
|
231
233
|
|
|
232
234
|
The bed implements `AsyncDisposable`, so `await using bed = TestBed.solitary(X).compile()` disposes it at the end of the
|
|
233
235
|
block.
|
|
@@ -251,7 +253,7 @@ extending it, so setup failures can be caught separately from resolution failure
|
|
|
251
253
|
## Documentation
|
|
252
254
|
|
|
253
255
|
- [Rendered docs on codefastlabs.com](https://codefastlabs.com/docs/di-testing)
|
|
254
|
-
- [`@codefast/di`](../di/README.md) — the container this package builds on, including its `
|
|
256
|
+
- [`@codefast/di`](../di/README.md) — the container this package builds on, including its specification under `spec/`.
|
|
255
257
|
- [`CHANGELOG.md`](./CHANGELOG.md) — release history.
|
|
256
258
|
|
|
257
259
|
## Contributing
|
|
@@ -127,7 +127,7 @@ export function bindMocks(container, dependencies, overrides, mockFactory, expos
|
|
|
127
127
|
}
|
|
128
128
|
};
|
|
129
129
|
const bindConstant = (slot, value) => {
|
|
130
|
-
const builder = container.bind(slot.token)
|
|
130
|
+
const builder = container.bind(slot.token);
|
|
131
131
|
if (slot.name !== undefined) {
|
|
132
132
|
builder.whenNamed(slot.name);
|
|
133
133
|
}
|
|
@@ -136,6 +136,7 @@ export function bindMocks(container, dependencies, overrides, mockFactory, expos
|
|
|
136
136
|
builder.whenTagged(tag);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
+
builder.toConstantValue(value);
|
|
139
140
|
};
|
|
140
141
|
for (const slot of dependencies) {
|
|
141
142
|
const list = overrides.get(slot.token) ?? [];
|
|
@@ -146,7 +147,7 @@ export function bindMocks(container, dependencies, overrides, mockFactory, expos
|
|
|
146
147
|
consumed.add(override);
|
|
147
148
|
if (override.override.kind === "absent") {
|
|
148
149
|
if (!slot.optional && !slot.multi) {
|
|
149
|
-
throw new OverrideMismatchError(tokenName(slot.token), ".absent() targets a required dependency — only
|
|
150
|
+
throw new OverrideMismatchError(tokenName(slot.token), ".absent() targets a required dependency — only injectOptional() or injectAll() slots can be absent.");
|
|
150
151
|
}
|
|
151
152
|
if (!overrideValues.has(override)) {
|
|
152
153
|
overrideValues.set(override, undefined);
|
|
@@ -167,8 +168,8 @@ export function bindMocks(container, dependencies, overrides, mockFactory, expos
|
|
|
167
168
|
for (const [index, value] of values.entries()) {
|
|
168
169
|
container
|
|
169
170
|
.bind(slot.token)
|
|
170
|
-
.
|
|
171
|
-
.
|
|
171
|
+
.whenNamed(`${ALL_ELEMENT_SLOT_PREFIX}${String(index)}`)
|
|
172
|
+
.toConstantValue(value);
|
|
172
173
|
}
|
|
173
174
|
record(slot.token, { criteria: override.criteria, value: values, kind: "all" });
|
|
174
175
|
}
|
|
@@ -42,7 +42,7 @@ export interface MockOverrideBuilder<Dependency, Owner, Backend extends MockFunc
|
|
|
42
42
|
* @remarks The callback runs once per compile, so beds built from one builder never share spies.
|
|
43
43
|
*/
|
|
44
44
|
stub(setup: (fn: MockFactory<Backend>) => DeepPartial<Dependency>): Owner;
|
|
45
|
-
/** Leaves the dependency unbound: an `
|
|
45
|
+
/** Leaves the dependency unbound: an `injectOptional()` slot resolves `undefined`, an `injectAll()` slot `[]`. */
|
|
46
46
|
absent(): Owner;
|
|
47
47
|
/** Supplies every element of an unconstrained `injectAll()` slot, in order. Sealed like `.using`. */
|
|
48
48
|
usingAll(values: ReadonlyArray<Dependency>): Owner;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** The override-recording core both test-bed builders extend. */
|
|
2
2
|
import { defaultMetadataReader } from "@codefast/di";
|
|
3
|
-
import { verifyingMetadataReader } from "@codefast/di/metadata/verifying-
|
|
3
|
+
import { verifyingMetadataReader } from "@codefast/di/metadata/verifying-reader";
|
|
4
4
|
import { criteriaEquals, normalizeCriteria } from "#discovery/mock-binder";
|
|
5
5
|
/**
|
|
6
6
|
* The shared builder core: records overrides, resolves the reader and mock factory, and owns the
|
|
@@ -56,7 +56,7 @@ export class BedBuilder {
|
|
|
56
56
|
}
|
|
57
57
|
catch (error) {
|
|
58
58
|
// A failed compile still owns the container — dispose it so no lifecycle state leaks.
|
|
59
|
-
void prepared?.container.
|
|
59
|
+
void prepared?.container.disposeAsync().catch(() => undefined);
|
|
60
60
|
throw error;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -69,7 +69,7 @@ export class BedBuilder {
|
|
|
69
69
|
return build(unit, prepared);
|
|
70
70
|
}
|
|
71
71
|
catch (error) {
|
|
72
|
-
await prepared?.container.
|
|
72
|
+
await prepared?.container.disposeAsync().catch(() => undefined);
|
|
73
73
|
throw error;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -61,13 +61,14 @@ export class SociableBuilder extends BedBuilder {
|
|
|
61
61
|
container.bind(realToken).to(realClass).singleton();
|
|
62
62
|
container.bind(realClass).toDynamic((context) => context.resolve(realToken));
|
|
63
63
|
for (const slot of slots) {
|
|
64
|
-
const builder = container.bind(realClass)
|
|
64
|
+
const builder = container.bind(realClass);
|
|
65
65
|
if (slot.name !== undefined) {
|
|
66
66
|
builder.whenNamed(slot.name);
|
|
67
67
|
}
|
|
68
68
|
for (const tag of slot.tags ?? []) {
|
|
69
69
|
builder.whenTagged(tag);
|
|
70
70
|
}
|
|
71
|
+
builder.toDynamic((context) => context.resolve(realToken));
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
// Sealed entries so mocks.get points at bed.exposed() instead of handing back a fake Mocked.
|
|
@@ -42,7 +42,7 @@ export interface UnitTestBed<Class, Backend extends MockFunction = Spy> extends
|
|
|
42
42
|
/** Clears the call history and configured behaviour of every auto-mock and stub the bed created. */
|
|
43
43
|
resetMocks(this: void): void;
|
|
44
44
|
/** Runs the unit's `@preDestroy` hooks and disposes the backing container. */
|
|
45
|
-
|
|
45
|
+
disposeAsync(this: void): Promise<void>;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* A sociable bed: the solitary surface plus access to the real collaborators it exposed.
|
|
@@ -15,7 +15,7 @@ export function createSociableUnitTestBed(bed, container, exposedClasses) {
|
|
|
15
15
|
unit: bed.unit,
|
|
16
16
|
mocks: bed.mocks,
|
|
17
17
|
resetMocks: bed.resetMocks,
|
|
18
|
-
|
|
18
|
+
disposeAsync: bed.disposeAsync,
|
|
19
19
|
[Symbol.asyncDispose]: bed[Symbol.asyncDispose],
|
|
20
20
|
exposed(target) {
|
|
21
21
|
if (!exposedClasses.has(target)) {
|
|
@@ -60,14 +60,14 @@ export function createUnitTestBed(unit, entries, container) {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
|
-
const
|
|
64
|
-
await container.
|
|
63
|
+
const disposeAsync = async () => {
|
|
64
|
+
await container.disposeAsync();
|
|
65
65
|
};
|
|
66
66
|
return {
|
|
67
67
|
unit,
|
|
68
68
|
mocks,
|
|
69
69
|
resetMocks,
|
|
70
|
-
|
|
71
|
-
[Symbol.asyncDispose]:
|
|
70
|
+
disposeAsync,
|
|
71
|
+
[Symbol.asyncDispose]: disposeAsync,
|
|
72
72
|
};
|
|
73
73
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/di-testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Solitary and sociable auto-mocking test beds for @codefast/di",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"auto-mock",
|
|
@@ -100,9 +100,9 @@
|
|
|
100
100
|
"access": "public"
|
|
101
101
|
},
|
|
102
102
|
"peerDependencies": {
|
|
103
|
-
"@codefast/di": ">=0.
|
|
103
|
+
"@codefast/di": ">=0.14.0"
|
|
104
104
|
},
|
|
105
105
|
"engines": {
|
|
106
|
-
"node": ">=
|
|
106
|
+
"node": ">=22.18.0"
|
|
107
107
|
}
|
|
108
108
|
}
|