@provino/mock 0.0.0-stage → 0.1.0-rc.1
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/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/index.d.ts +457 -0
- package/dist/index.js +2312 -0
- package/package.json +37 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pixel Perfect Software
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @provino/mock
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The mock engine: mock functions, spies, replaced properties and stubs, a registry per test file, and module mocking (automock and manual mocks). A dialect's mock policy supplies every value and message.
|
|
4
|
+
|
|
5
|
+
This package is a building block of [Provino](https://github.com/pixel-perfect-software/provino), a test runner for Jest projects, on npm as [`provino`](https://www.npmjs.com/package/provino). The `provino` package ships every `@provino/*` package inlined in one bundle, so you install `provino`, not this package.
|
|
6
|
+
|
|
7
|
+
**Versioning:** this package is `@beta` as a whole. There is no semver promise for it before 1.0, and its API can change in any release.
|
|
8
|
+
|
|
9
|
+
[MIT](https://github.com/pixel-perfect-software/provino/blob/main/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { LoadActual, MockCleanupStep, MockDecision, MockPolicy, ModuleMockRegisterOptions, ModuleMockRegistry, ModulePolicy, Resolver } from "@provino/types";
|
|
2
|
+
/**
|
|
3
|
+
* An `Error` from the mock engine; its `name` stays `Error`.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export declare class MockError extends Error {
|
|
7
|
+
readonly code: string;
|
|
8
|
+
constructor(code: string, message: string);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A `TypeError` from the mock engine; its `name` stays `TypeError`.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export declare class MockTypeError extends TypeError {
|
|
15
|
+
readonly code: string;
|
|
16
|
+
constructor(code: string, message: string, options?: ErrorOptions);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What the registry needs of a mock: its two dirty flags, and the two operations a walk runs. The
|
|
20
|
+
* registry holds these only through `WeakRef`s, so a mock no test can reach can be collected.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
interface DirtyMock {
|
|
24
|
+
stateDirty: boolean;
|
|
25
|
+
configDirty: boolean;
|
|
26
|
+
ref: WeakRef<DirtyMock> | null;
|
|
27
|
+
/** The `mockClear` operation. */
|
|
28
|
+
clearState(): void;
|
|
29
|
+
/** The `mockReset` operation. */
|
|
30
|
+
resetMock(): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A restorer the registry runs on `restoreAll`. A replaced property's restorer also says what it
|
|
34
|
+
* replaced, so a second `replaceProperty` on the same key finds it.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
interface Restorer {
|
|
38
|
+
(): void;
|
|
39
|
+
object?: object;
|
|
40
|
+
property?: PropertyKey;
|
|
41
|
+
replaced?: unknown;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The unstub operations a `Stubs` value registers, so the between-test steps can reach it.
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
interface StubSteps {
|
|
48
|
+
unstubAllEnvs(): unknown;
|
|
49
|
+
unstubAllGlobals(): unknown;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Per-test cleanup flags by their neutral names; a missing flag takes the policy's value.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
type MockCleanupConfig = { readonly [K in MockCleanupStep]?: boolean | undefined; };
|
|
56
|
+
/**
|
|
57
|
+
* One test file's mock state. Mocks, spies and stubs built with it share its invocation counter
|
|
58
|
+
* and are what its `*All` operations reach.
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
interface MockRegistry {
|
|
62
|
+
/** The next call's place in `mock.invocationCallOrder`. The first is 1. */
|
|
63
|
+
nextInvocationOrder(): number;
|
|
64
|
+
/** `mockClear` on every mock with state to clear. */
|
|
65
|
+
clearAll(): void;
|
|
66
|
+
/** `mockReset` on every mock with state or configuration to reset. */
|
|
67
|
+
resetAll(): void;
|
|
68
|
+
/** Runs every restorer once and forgets them: spies and replaced properties, not stubs. */
|
|
69
|
+
restoreAll(): void;
|
|
70
|
+
/** The cleanup before a test: the steps of `between.order` that `cfg` or `between` turn on. */
|
|
71
|
+
beforeEach(cfg: MockCleanupConfig, between: MockPolicy["between"]): void;
|
|
72
|
+
/** File teardown: `restoreAll`, both unstubs, and every reference dropped. */
|
|
73
|
+
dispose(): void;
|
|
74
|
+
/**
|
|
75
|
+
* How many `resetAll`s have run. A mock that has never been touched isn't in a dirty set, so it
|
|
76
|
+
* compares this on first use and applies the reset then.
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
readonly resets: number;
|
|
80
|
+
/** @internal */
|
|
81
|
+
markStateDirty(mock: DirtyMock): void;
|
|
82
|
+
/** @internal */
|
|
83
|
+
markConfigDirty(mock: DirtyMock): void;
|
|
84
|
+
/** @internal */
|
|
85
|
+
addRestorer(restorer: Restorer): void;
|
|
86
|
+
/** @internal */
|
|
87
|
+
deleteRestorer(restorer: Restorer): void;
|
|
88
|
+
/** @internal */
|
|
89
|
+
findRestorer(object: object, property: PropertyKey): Restorer | undefined;
|
|
90
|
+
/** @internal */
|
|
91
|
+
addStubs(stubs: StubSteps): void;
|
|
92
|
+
/** How many mocks each dirty set holds, including any that have been collected. @internal */
|
|
93
|
+
dirtyCounts(): {
|
|
94
|
+
readonly state: number;
|
|
95
|
+
readonly config: number;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* A new registry, one per test file. The first invocation order it hands out is 1.
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export declare function createMockRegistry(): MockRegistry;
|
|
103
|
+
/**
|
|
104
|
+
* A function of unknown shape, the default target of a mock.
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
type UnknownFunction = (...args: unknown[]) => unknown;
|
|
108
|
+
/**
|
|
109
|
+
* Any function. Parameters are `never[]` so that every function type is assignable to it.
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
type FunctionLike = (...args: never[]) => unknown;
|
|
113
|
+
/**
|
|
114
|
+
* Any class.
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
type ClassLike = abstract new (...args: never[]) => unknown;
|
|
118
|
+
/**
|
|
119
|
+
* What a mock can stand in for.
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
type MockTarget = FunctionLike | ClassLike;
|
|
123
|
+
/**
|
|
124
|
+
* The arguments of a target's call or construction.
|
|
125
|
+
* @public
|
|
126
|
+
*/
|
|
127
|
+
type MockParameters<T extends MockTarget> = T extends ((...args: infer P) => unknown) ? P : T extends (abstract new (...args: infer P) => unknown) ? P : never;
|
|
128
|
+
/**
|
|
129
|
+
* What a target's call returns, or the instance its construction makes.
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
type MockReturn<T extends MockTarget> = T extends ((...args: never[]) => infer R) ? R : T extends (abstract new (...args: never[]) => infer R) ? R : never;
|
|
133
|
+
/**
|
|
134
|
+
* One entry of `mock.results`. `incomplete` is a call still running.
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
type MockResult<R> = {
|
|
138
|
+
type: "return";
|
|
139
|
+
value: R;
|
|
140
|
+
} | {
|
|
141
|
+
type: "throw";
|
|
142
|
+
value: unknown;
|
|
143
|
+
} | {
|
|
144
|
+
type: "incomplete";
|
|
145
|
+
value: undefined;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* One entry of `mock.settledResults`, on the policies that have it.
|
|
149
|
+
* @public
|
|
150
|
+
*/
|
|
151
|
+
type MockSettledResult<R> = {
|
|
152
|
+
type: "fulfilled";
|
|
153
|
+
value: Awaited<R>;
|
|
154
|
+
} | {
|
|
155
|
+
type: "rejected";
|
|
156
|
+
value: unknown;
|
|
157
|
+
} | {
|
|
158
|
+
type: "incomplete";
|
|
159
|
+
value: undefined;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* What `fn.mock` holds. `lastCall` and `settledResults` exist per policy.
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
interface MockState<T extends MockTarget = UnknownFunction> {
|
|
166
|
+
calls: Array<MockParameters<T>>;
|
|
167
|
+
contexts: unknown[];
|
|
168
|
+
instances: Array<MockReturn<T>>;
|
|
169
|
+
invocationCallOrder: number[];
|
|
170
|
+
results: Array<MockResult<MockReturn<T>>>;
|
|
171
|
+
settledResults?: Array<MockSettledResult<MockReturn<T>>>;
|
|
172
|
+
lastCall?: MockParameters<T> | undefined;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The methods every mock has, under every policy (Track 4 L228): a policy changes defaults and
|
|
176
|
+
* return values, never whether a method exists.
|
|
177
|
+
* @public
|
|
178
|
+
*/
|
|
179
|
+
interface MockInstance<T extends MockTarget = UnknownFunction> {
|
|
180
|
+
readonly mock: MockState<T>;
|
|
181
|
+
readonly _isMockFunction: true;
|
|
182
|
+
getMockName(): string;
|
|
183
|
+
mockName(name: string): this;
|
|
184
|
+
getMockImplementation(): T | undefined;
|
|
185
|
+
mockClear(): this;
|
|
186
|
+
mockReset(): this;
|
|
187
|
+
mockRestore(): void;
|
|
188
|
+
mockImplementation(implementation: T): this;
|
|
189
|
+
mockImplementationOnce(implementation: T): this;
|
|
190
|
+
withImplementation(implementation: T, callback: () => Promise<unknown>): Promise<this | undefined>;
|
|
191
|
+
withImplementation(implementation: T, callback: () => unknown): this | undefined;
|
|
192
|
+
mockReturnThis(): this;
|
|
193
|
+
mockReturnValue(value: MockReturn<T>): this;
|
|
194
|
+
mockReturnValueOnce(value: MockReturn<T>): this;
|
|
195
|
+
mockResolvedValue(value: Awaited<MockReturn<T>>): this;
|
|
196
|
+
mockResolvedValueOnce(value: Awaited<MockReturn<T>>): this;
|
|
197
|
+
mockRejectedValue(value: unknown): this;
|
|
198
|
+
mockRejectedValueOnce(value: unknown): this;
|
|
199
|
+
mockThrow(value: unknown): this;
|
|
200
|
+
mockThrowOnce(value: unknown): this;
|
|
201
|
+
/** A sub-mock that answers the calls whose arguments equal these. */
|
|
202
|
+
whenCalledWith(...args: unknown[]): Mock<T>;
|
|
203
|
+
[Symbol.dispose](): void;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* A mock function: callable and constructable like its target, plus the mock methods.
|
|
207
|
+
* @public
|
|
208
|
+
*/
|
|
209
|
+
interface Mock<T extends MockTarget = UnknownFunction> extends MockInstance<T> {
|
|
210
|
+
(...args: MockParameters<T>): MockReturn<T>;
|
|
211
|
+
new (...args: MockParameters<T>): MockReturn<T>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* What `createMockFn` builds a mock from. Every option is one a later builder needs: `impl` for
|
|
215
|
+
* `fn(impl)`, `spy`, `original` and `restore` for spies, `name`, `length` and `fallback` for
|
|
216
|
+
* automock and factories (B13b).
|
|
217
|
+
* @public
|
|
218
|
+
*/
|
|
219
|
+
interface CreateMockOptions<T extends MockTarget = UnknownFunction> {
|
|
220
|
+
/** The implementation, as in `fn(impl)`. Under `resetTo: 'original-impl'`, `mockReset` returns to it. */
|
|
221
|
+
readonly impl?: T | undefined;
|
|
222
|
+
/** The function's own `name`. Any string; a caller that needs an identifier sanitizes it first. */
|
|
223
|
+
readonly name?: string | undefined;
|
|
224
|
+
/** The function's own `length`. Defaults to the implementation's. */
|
|
225
|
+
readonly length?: number | undefined;
|
|
226
|
+
/**
|
|
227
|
+
* What a call runs when the mock has no implementation, surviving `mockReset`. jest-mock's
|
|
228
|
+
* `_protoImpl`: automocked instance methods fall through to their prototype's mock.
|
|
229
|
+
*/
|
|
230
|
+
readonly fallback?: FunctionLike | undefined;
|
|
231
|
+
/** This mock is a spy: `spyNameFromMethod` and `spyReportsBridgeImpl` apply to it. */
|
|
232
|
+
readonly spy?: boolean | undefined;
|
|
233
|
+
/** The spied function. A call runs it while the spy has no implementation of its own. */
|
|
234
|
+
readonly original?: FunctionLike | undefined;
|
|
235
|
+
/** What `mockRestore` undoes after resetting. `restoreAll` needs its own restorer: `addRestorer`. */
|
|
236
|
+
readonly restore?: (() => void) | undefined;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* A property `replaceProperty` replaced.
|
|
240
|
+
* @public
|
|
241
|
+
*/
|
|
242
|
+
interface Replaced<V = unknown> {
|
|
243
|
+
/** Sets another value; the original is still what `restore` puts back. */
|
|
244
|
+
replaceValue(value: V): Replaced<V>;
|
|
245
|
+
restore(): void;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* The environment and global stubs. Every method returns the same value, so calls chain.
|
|
249
|
+
* @public
|
|
250
|
+
*/
|
|
251
|
+
interface Stubs {
|
|
252
|
+
/** Sets a variable, or deletes it for `undefined`. Anything else becomes a string. */
|
|
253
|
+
stubEnv(name: string, value: string | undefined): Stubs;
|
|
254
|
+
/** Defines a global as a plain writable value. */
|
|
255
|
+
stubGlobal(name: PropertyKey, value: unknown): Stubs;
|
|
256
|
+
/** Puts back every variable stubbed since the last call. */
|
|
257
|
+
unstubAllEnvs(): Stubs;
|
|
258
|
+
/** Puts back every global stubbed since the last call. */
|
|
259
|
+
unstubAllGlobals(): Stubs;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The keys of `T` whose values are functions or classes, which is what `spyOn` accepts without an
|
|
263
|
+
* access type.
|
|
264
|
+
* @public
|
|
265
|
+
*/
|
|
266
|
+
type SpyableKeys<T> = { [K in keyof T]-?: Required<T>[K] extends MockTarget ? K : never; }[keyof T];
|
|
267
|
+
/**
|
|
268
|
+
* What `spyOn(object, key)` returns.
|
|
269
|
+
* @public
|
|
270
|
+
*/
|
|
271
|
+
type SpiedMethod<V> = V extends MockTarget ? Mock<V> : never;
|
|
272
|
+
/**
|
|
273
|
+
* What `spyOn(object, key, 'get')` returns.
|
|
274
|
+
* @public
|
|
275
|
+
*/
|
|
276
|
+
type SpiedGetter<V> = Mock<() => V>;
|
|
277
|
+
/**
|
|
278
|
+
* What `spyOn(object, key, 'set')` returns.
|
|
279
|
+
* @public
|
|
280
|
+
*/
|
|
281
|
+
type SpiedSetter<V> = Mock<(value: V) => void>;
|
|
282
|
+
/**
|
|
283
|
+
* Marks a mock made by this engine. Every mock also has `_isMockFunction: true`, for the ecosystem
|
|
284
|
+
* and for `isMockFunction`.
|
|
285
|
+
* @public
|
|
286
|
+
*/
|
|
287
|
+
export declare const MOCK_BRAND: unique symbol;
|
|
288
|
+
/**
|
|
289
|
+
* Builds a mock function: `fn()`, `fn(impl)`, and through its options every other builder (spies,
|
|
290
|
+
* automock members, factories). Takes one policy slice, and the file's registry.
|
|
291
|
+
* @public
|
|
292
|
+
*/
|
|
293
|
+
export declare function createMockFn<T extends MockTarget = UnknownFunction>(policy: MockPolicy, registry: MockRegistry, options?: CreateMockOptions<T>): Mock<T>;
|
|
294
|
+
/**
|
|
295
|
+
* Whether a value is a mock function. Under `isMockAcceptsObjects` any value with
|
|
296
|
+
* `_isMockFunction: true` is; otherwise only a function is.
|
|
297
|
+
* @public
|
|
298
|
+
*/
|
|
299
|
+
export declare function isMockFunction(policy: MockPolicy, value: unknown): value is Mock<UnknownFunction>;
|
|
300
|
+
/**
|
|
301
|
+
* `automock` mocks everything; `autospy` keeps the real behaviour and records calls (Vitest's
|
|
302
|
+
* `vi.mock(path, { spy: true })`).
|
|
303
|
+
* @public
|
|
304
|
+
*/
|
|
305
|
+
type AutomockMode = "automock" | "autospy";
|
|
306
|
+
/**
|
|
307
|
+
* Makes one mock function, as `createMockFn` does with the file's policy and registry bound.
|
|
308
|
+
* @public
|
|
309
|
+
*/
|
|
310
|
+
type AutomockMaker = (options: CreateMockOptions) => UnknownFunction;
|
|
311
|
+
/**
|
|
312
|
+
* Builds the automock of a module's real exports (T4.6). Functions become mocks that return
|
|
313
|
+
* `undefined`; a class's constructor body never runs, and each instance's methods record into the
|
|
314
|
+
* prototype's mocks too. Statics, nested objects and getters are mocked (a getter reads
|
|
315
|
+
* `undefined`); arrays become `[]`; `Map`s, primitives and `null` are kept. Cycles are followed.
|
|
316
|
+
* `metadata` is jest-mock's shape (a `Date` or `BigInt` is dropped, a `RegExp` emptied, the module
|
|
317
|
+
* gets `__esModule`); `deep` is `@vitest/mocker`'s. `autospy` keeps the real behaviour.
|
|
318
|
+
*
|
|
319
|
+
* Throws `MockError` with code `automock-no-metadata` under `metadata` when the root has no
|
|
320
|
+
* metadata, as `getMetadata` returning `null` does.
|
|
321
|
+
* @public
|
|
322
|
+
*/
|
|
323
|
+
export declare function automock(value: unknown, shape: ModulePolicy["automockShape"], mode: AutomockMode, mk: AutomockMaker): unknown;
|
|
324
|
+
/**
|
|
325
|
+
* A manual mock the search found.
|
|
326
|
+
* @public
|
|
327
|
+
*/
|
|
328
|
+
interface ManualMock {
|
|
329
|
+
/** The absolute path of the mock file. */
|
|
330
|
+
readonly path: string;
|
|
331
|
+
/** `root` beside `node_modules`, or `adjacent` to the module it replaces. */
|
|
332
|
+
readonly kind: "root" | "adjacent";
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Where to look.
|
|
336
|
+
* @public
|
|
337
|
+
*/
|
|
338
|
+
interface FindManualMockOptions {
|
|
339
|
+
/** The directories whose `__mocks__` hold root mocks (Jest `roots`). */
|
|
340
|
+
readonly roots: readonly string[];
|
|
341
|
+
/** The extensions a mock file may have, with or without the dot (`.ts` or `ts`). */
|
|
342
|
+
readonly extensions: readonly string[];
|
|
343
|
+
/** The resolved file of a relative or absolute specifier, for the adjacent search. */
|
|
344
|
+
readonly resolved?: string | null | undefined;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The root `__mocks__` trees, listed once (B13c Gap 8). Under `rootMocksAutoApply` every bare
|
|
348
|
+
* specifier asks for its root mock, so the worker lists the trees once and every file's registry
|
|
349
|
+
* looks names up in the list instead of stat-ing a path per name. Nothing invalidates it: there is
|
|
350
|
+
* no watch mode yet.
|
|
351
|
+
* @public
|
|
352
|
+
*/
|
|
353
|
+
interface RootMockIndex {
|
|
354
|
+
/** The root mock of a package or core name, as `findRootMock` would find it, or `null`. */
|
|
355
|
+
find(specifier: string): ManualMock | null;
|
|
356
|
+
/** Whether no root has a root mock at all: then nothing is looked up. */
|
|
357
|
+
isEmpty(): boolean;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Lists each root's `__mocks__` tree on first use. A name is the file's path under `__mocks__`
|
|
361
|
+
* without its extension (`pkg`, `@scope/pkg`, `pkg/sub`, `fs`); the first root wins, then the
|
|
362
|
+
* first extension, as in `findRootMock`.
|
|
363
|
+
* @public
|
|
364
|
+
*/
|
|
365
|
+
export declare function createRootMockIndex(options: Pick<FindManualMockOptions, "roots" | "extensions">): RootMockIndex;
|
|
366
|
+
/**
|
|
367
|
+
* Looks for the manual mock of a module: the root mock for a package or core name first, then
|
|
368
|
+
* `__mocks__/<file>` beside the resolved file. Pure file-system reads; callers cache.
|
|
369
|
+
* @public
|
|
370
|
+
*/
|
|
371
|
+
export declare function findManualMock(specifier: string, options: FindManualMockOptions): ManualMock | null;
|
|
372
|
+
declare const NEUTRAL_MODULE_MESSAGES: {
|
|
373
|
+
readonly "module-missing": "Cannot find module '{specifier}' from '{from}'. To mock a module that does not exist on disk, pass the `virtual` option";
|
|
374
|
+
readonly "unmock-module-missing": "Cannot find module '{specifier}' from '{from}'";
|
|
375
|
+
readonly "factory-not-object": "The mock factory for \"{raw}\" must return an object";
|
|
376
|
+
readonly "missing-factory-export": "No \"{name}\" export is defined on the \"{raw}\" mock";
|
|
377
|
+
readonly "nested-isolation": "{caller} cannot be nested inside another {caller} or {other}";
|
|
378
|
+
readonly "automock-no-metadata": "Failed to get mock metadata: {id}";
|
|
379
|
+
readonly "circular-mock": "The mock factory for \"{raw}\" imports the module it mocks";
|
|
380
|
+
readonly "registry-disposed": "The module mock registry was disposed";
|
|
381
|
+
readonly "unresolved-relative-mock": "Cannot find module '{id}'";
|
|
382
|
+
};
|
|
383
|
+
/**
|
|
384
|
+
* The codes `ModulePolicy.messages` is keyed by.
|
|
385
|
+
* @public
|
|
386
|
+
*/
|
|
387
|
+
type ModuleMessageCode = keyof typeof NEUTRAL_MODULE_MESSAGES;
|
|
388
|
+
/**
|
|
389
|
+
* Options for one `register` call (`ModuleMockRegisterOptions` in `@provino/types`, where the
|
|
390
|
+
* registry's contract lives so the loader can take one by injection).
|
|
391
|
+
* @public
|
|
392
|
+
*/
|
|
393
|
+
type RegisterOptions = ModuleMockRegisterOptions;
|
|
394
|
+
/**
|
|
395
|
+
* What `createModuleMockRegistry` needs.
|
|
396
|
+
* @public
|
|
397
|
+
*/
|
|
398
|
+
interface ModuleMockRegistryOptions {
|
|
399
|
+
readonly policy: ModulePolicy;
|
|
400
|
+
readonly mockPolicy: MockPolicy;
|
|
401
|
+
/** The file's mock-function registry: every mock the registry makes joins it. */
|
|
402
|
+
readonly mocks: MockRegistry;
|
|
403
|
+
/** The loader's resolver (D34): `@provino/mock` never builds one. */
|
|
404
|
+
readonly resolver: Resolver;
|
|
405
|
+
/** Directories whose `__mocks__` hold root mocks (Jest `roots`); the first also roots error paths. */
|
|
406
|
+
readonly roots: readonly string[];
|
|
407
|
+
/** Extensions a manual mock may have (`.ts` or `ts`). */
|
|
408
|
+
readonly extensions: readonly string[];
|
|
409
|
+
/** Resolution conditions for `register`'s resolve (default none: the resolver's own). */
|
|
410
|
+
readonly conditions?: readonly string[];
|
|
411
|
+
/**
|
|
412
|
+
* Loads a module's real exports (and a manual mock's file), bypassing the registry for that
|
|
413
|
+
* module. Automock asks for a scratch load.
|
|
414
|
+
*/
|
|
415
|
+
readonly loadActual: LoadActual;
|
|
416
|
+
/**
|
|
417
|
+
* The root `__mocks__` trees, listed once per worker (`createRootMockIndex` over the same
|
|
418
|
+
* `roots` and `extensions`). Without it the registry lists them itself, once per file.
|
|
419
|
+
*/
|
|
420
|
+
readonly rootMocks?: RootMockIndex;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Creates a test file's module-mock registry. It never imports a dialect: every difference between
|
|
424
|
+
* Jest and Vitest is a key of `options.policy`.
|
|
425
|
+
* @public
|
|
426
|
+
*/
|
|
427
|
+
export declare function createModuleMockRegistry(options: ModuleMockRegistryOptions): ModuleMockRegistry;
|
|
428
|
+
/**
|
|
429
|
+
* Replaces a function or class property with a spy that calls through. A property already holding
|
|
430
|
+
* a mock returns that mock.
|
|
431
|
+
* @public
|
|
432
|
+
*/
|
|
433
|
+
export declare function spyOn<T extends object, K extends SpyableKeys<T>>(policy: MockPolicy, registry: MockRegistry, object: T, key: K): SpiedMethod<Required<T>[K]>;
|
|
434
|
+
/**
|
|
435
|
+
* Replaces an accessor's getter with a spy that calls through.
|
|
436
|
+
* @public
|
|
437
|
+
*/
|
|
438
|
+
export declare function spyOn<T extends object, K extends keyof T>(policy: MockPolicy, registry: MockRegistry, object: T, key: K, access: "get"): SpiedGetter<T[K]>;
|
|
439
|
+
/**
|
|
440
|
+
* Replaces an accessor's setter with a spy that calls through.
|
|
441
|
+
* @public
|
|
442
|
+
*/
|
|
443
|
+
export declare function spyOn<T extends object, K extends keyof T>(policy: MockPolicy, registry: MockRegistry, object: T, key: K, access: "set"): SpiedSetter<T[K]>;
|
|
444
|
+
/**
|
|
445
|
+
* Replaces a plain value property until `restore` or the registry's `restoreAll`. Follows
|
|
446
|
+
* jest-mock's `replaceProperty` (index.ts L1448-L1558), but, like `spyOn`, registers its restorer
|
|
447
|
+
* only after the first write has worked.
|
|
448
|
+
* @public
|
|
449
|
+
*/
|
|
450
|
+
export declare function replaceProperty<T extends object, K extends keyof T>(policy: MockPolicy, registry: MockRegistry, object: T, key: K, value: T[K]): Replaced<T[K]>;
|
|
451
|
+
/**
|
|
452
|
+
* The stubs for one test file. `env` is the object to stub variables on and `global` the
|
|
453
|
+
* environment's global; the registry's between-test `unstub*` steps and `dispose` reach them.
|
|
454
|
+
* @public
|
|
455
|
+
*/
|
|
456
|
+
export declare function createStubs(registry: MockRegistry, env: Record<string, string | undefined>, global?: object): Stubs;
|
|
457
|
+
export type { AutomockMaker, AutomockMode, ClassLike, CreateMockOptions, FindManualMockOptions, FunctionLike, ManualMock, Mock, MockCleanupConfig, MockDecision, MockInstance, MockParameters, MockRegistry, MockResult, MockReturn, MockSettledResult, MockState, MockTarget, ModuleMessageCode, ModuleMockRegistry, ModuleMockRegistryOptions, RegisterOptions, Replaced, RootMockIndex, SpiedGetter, SpiedMethod, SpiedSetter, SpyableKeys, Stubs, UnknownFunction };
|