@codefast/di-testing 0.3.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 +19 -0
- package/README.md +12 -10
- package/dist/discovery/mock-binder.js +1 -1
- package/dist/test-bed/bed-builder.d.ts +1 -1
- package/dist/test-bed/bed-builder.js +2 -2
- 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,24 @@
|
|
|
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
|
+
|
|
3
22
|
## 0.3.0
|
|
4
23
|
|
|
5
24
|
### 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.
|
|
@@ -147,7 +147,7 @@ export function bindMocks(container, dependencies, overrides, mockFactory, expos
|
|
|
147
147
|
consumed.add(override);
|
|
148
148
|
if (override.override.kind === "absent") {
|
|
149
149
|
if (!slot.optional && !slot.multi) {
|
|
150
|
-
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.");
|
|
151
151
|
}
|
|
152
152
|
if (!overrideValues.has(override)) {
|
|
153
153
|
overrideValues.set(override, undefined);
|
|
@@ -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;
|
|
@@ -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
|
}
|
|
@@ -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
|
}
|