@storybook/nextjs-vite 9.2.0-alpha.2 → 10.0.0-beta.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.
Files changed (46) hide show
  1. package/README.md +3 -1
  2. package/dist/_browser-chunks/chunk-2RWWBM6A.js +261 -0
  3. package/dist/_browser-chunks/chunk-L5NVL7MD.js +37 -0
  4. package/dist/_browser-chunks/react-18-G7Q4PNHD.js +71 -0
  5. package/dist/_node-chunks/chunk-IHKOVONR.js +54 -0
  6. package/dist/_node-chunks/dist-YEP22NZU.js +7476 -0
  7. package/dist/_node-chunks/jiti-4UV67MCQ.js +32362 -0
  8. package/dist/export-mocks/cache/index.d.ts +308 -1
  9. package/dist/export-mocks/cache/index.js +22 -1
  10. package/dist/export-mocks/headers/index.d.ts +309 -1
  11. package/dist/export-mocks/headers/index.js +77 -1
  12. package/dist/export-mocks/navigation/index.js +81 -1
  13. package/dist/export-mocks/router/index.d.ts +2 -2
  14. package/dist/export-mocks/router/index.js +97 -1
  15. package/dist/index.d.ts +51 -7
  16. package/dist/index.js +22153 -45
  17. package/dist/node/index.d.ts +25 -5
  18. package/dist/node/index.js +23 -1
  19. package/dist/preset.js +714 -580
  20. package/dist/preview.d.ts +3 -19
  21. package/dist/preview.d.tsx +19 -0
  22. package/dist/preview.js +11 -1
  23. package/dist/vite-plugin/index.d.ts +1 -3
  24. package/dist/vite-plugin/index.js +21 -1
  25. package/package.json +35 -83
  26. package/preset.js +1 -1
  27. package/template/cli/js/Configure.mdx +11 -11
  28. package/template/cli/ts/Configure.mdx +11 -11
  29. package/dist/chunk-GKRSUUNG.mjs +0 -6
  30. package/dist/chunk-J3BB7LEU.mjs +0 -20
  31. package/dist/chunk-XP5HYGXS.mjs +0 -3
  32. package/dist/export-mocks/cache/index.mjs +0 -6
  33. package/dist/export-mocks/headers/index.mjs +0 -12
  34. package/dist/export-mocks/navigation/index.mjs +0 -11
  35. package/dist/export-mocks/router/index.mjs +0 -10
  36. package/dist/images/decorator.d.ts +0 -6
  37. package/dist/images/decorator.js +0 -1
  38. package/dist/images/decorator.mjs +0 -2
  39. package/dist/index.d-ff220430.d.ts +0 -310
  40. package/dist/index.mjs +0 -57
  41. package/dist/node/index.mjs +0 -5
  42. package/dist/preset.d.ts +0 -9
  43. package/dist/preview.mjs +0 -3
  44. package/dist/react-18-IGIL3GJQ.mjs +0 -7
  45. package/dist/types-d044381e.d.ts +0 -52
  46. package/dist/vite-plugin/index.mjs +0 -6
@@ -1,4 +1,311 @@
1
- import { M as Mock } from '../../index.d-ff220430.js';
1
+ interface MockResultReturn<T> {
2
+ type: "return";
3
+ /**
4
+ * The value that was returned from the function. If function returned a Promise, then this will be a resolved value.
5
+ */
6
+ value: T;
7
+ }
8
+ interface MockResultIncomplete {
9
+ type: "incomplete";
10
+ value: undefined;
11
+ }
12
+ interface MockResultThrow {
13
+ type: "throw";
14
+ /**
15
+ * An error that was thrown during function execution.
16
+ */
17
+ value: any;
18
+ }
19
+ interface MockSettledResultFulfilled<T> {
20
+ type: "fulfilled";
21
+ value: T;
22
+ }
23
+ interface MockSettledResultRejected {
24
+ type: "rejected";
25
+ value: any;
26
+ }
27
+ type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
28
+ type MockSettledResult<T> = MockSettledResultFulfilled<T> | MockSettledResultRejected;
29
+ interface MockContext<T extends Procedure> {
30
+ /**
31
+ * This is an array containing all arguments for each call. One item of the array is the arguments of that call.
32
+ *
33
+ * @see https://vitest.dev/api/mock#mock-calls
34
+ * @example
35
+ * const fn = vi.fn()
36
+ *
37
+ * fn('arg1', 'arg2')
38
+ * fn('arg3')
39
+ *
40
+ * fn.mock.calls === [
41
+ * ['arg1', 'arg2'], // first call
42
+ * ['arg3'], // second call
43
+ * ]
44
+ */
45
+ calls: Parameters<T>[];
46
+ /**
47
+ * This is an array containing all instances that were instantiated when mock was called with a `new` keyword. Note that this is an actual context (`this`) of the function, not a return value.
48
+ * @see https://vitest.dev/api/mock#mock-instances
49
+ */
50
+ instances: ReturnType<T>[];
51
+ /**
52
+ * An array of `this` values that were used during each call to the mock function.
53
+ * @see https://vitest.dev/api/mock#mock-contexts
54
+ */
55
+ contexts: ThisParameterType<T>[];
56
+ /**
57
+ * The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.
58
+ *
59
+ * @see https://vitest.dev/api/mock#mock-invocationcallorder
60
+ * @example
61
+ * const fn1 = vi.fn()
62
+ * const fn2 = vi.fn()
63
+ *
64
+ * fn1()
65
+ * fn2()
66
+ * fn1()
67
+ *
68
+ * fn1.mock.invocationCallOrder === [1, 3]
69
+ * fn2.mock.invocationCallOrder === [2]
70
+ */
71
+ invocationCallOrder: number[];
72
+ /**
73
+ * This is an array containing all values that were `returned` from the function.
74
+ *
75
+ * The `value` property contains the returned value or thrown error. If the function returned a `Promise`, then `result` will always be `'return'` even if the promise was rejected.
76
+ *
77
+ * @see https://vitest.dev/api/mock#mock-results
78
+ * @example
79
+ * const fn = vi.fn()
80
+ * .mockReturnValueOnce('result')
81
+ * .mockImplementationOnce(() => { throw new Error('thrown error') })
82
+ *
83
+ * const result = fn()
84
+ *
85
+ * try {
86
+ * fn()
87
+ * }
88
+ * catch {}
89
+ *
90
+ * fn.mock.results === [
91
+ * {
92
+ * type: 'return',
93
+ * value: 'result',
94
+ * },
95
+ * {
96
+ * type: 'throw',
97
+ * value: Error,
98
+ * },
99
+ * ]
100
+ */
101
+ results: MockResult<ReturnType<T>>[];
102
+ /**
103
+ * An array containing all values that were `resolved` or `rejected` from the function.
104
+ *
105
+ * This array will be empty if the function was never resolved or rejected.
106
+ *
107
+ * @see https://vitest.dev/api/mock#mock-settledresults
108
+ * @example
109
+ * const fn = vi.fn().mockResolvedValueOnce('result')
110
+ *
111
+ * const result = fn()
112
+ *
113
+ * fn.mock.settledResults === []
114
+ * fn.mock.results === [
115
+ * {
116
+ * type: 'return',
117
+ * value: Promise<'result'>,
118
+ * },
119
+ * ]
120
+ *
121
+ * await result
122
+ *
123
+ * fn.mock.settledResults === [
124
+ * {
125
+ * type: 'fulfilled',
126
+ * value: 'result',
127
+ * },
128
+ * ]
129
+ */
130
+ settledResults: MockSettledResult<Awaited<ReturnType<T>>>[];
131
+ /**
132
+ * This contains the arguments of the last call. If spy wasn't called, will return `undefined`.
133
+ * @see https://vitest.dev/api/mock#mock-lastcall
134
+ */
135
+ lastCall: Parameters<T> | undefined;
136
+ }
137
+ type Procedure = (...args: any[]) => any;
138
+ // pick a single function type from function overloads, unions, etc...
139
+ type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
140
+ /*
141
+ cf. https://typescript-eslint.io/rules/method-signature-style/
142
+
143
+ Typescript assignability is different between
144
+ { foo: (f: T) => U } (this is "method-signature-style")
145
+ and
146
+ { foo(f: T): U }
147
+
148
+ Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows assignment such as:
149
+ const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true)
150
+ */
151
+ /* eslint-disable ts/method-signature-style */
152
+ interface MockInstance<T extends Procedure = Procedure> extends Disposable {
153
+ /**
154
+ * Use it to return the name assigned to the mock with the `.mockName(name)` method. By default, it will return `vi.fn()`.
155
+ * @see https://vitest.dev/api/mock#getmockname
156
+ */
157
+ getMockName(): string;
158
+ /**
159
+ * Sets the internal mock name. This is useful for identifying the mock when an assertion fails.
160
+ * @see https://vitest.dev/api/mock#mockname
161
+ */
162
+ mockName(name: string): this;
163
+ /**
164
+ * Current context of the mock. It stores information about all invocation calls, instances, and results.
165
+ */
166
+ mock: MockContext<T>;
167
+ /**
168
+ * Clears all information about every call. After calling it, all properties on `.mock` will return to their initial state. This method does not reset implementations. It is useful for cleaning up mocks between different assertions.
169
+ *
170
+ * To automatically call this method before each test, enable the [`clearMocks`](https://vitest.dev/config/#clearmocks) setting in the configuration.
171
+ * @see https://vitest.dev/api/mock#mockclear
172
+ */
173
+ mockClear(): this;
174
+ /**
175
+ * Does what `mockClear` does and resets inner implementation to the original function. This also resets all "once" implementations.
176
+ *
177
+ * Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
178
+ * Resetting a mock from `vi.fn(impl)` will set implementation to `impl`. It is useful for completely resetting a mock to its default state.
179
+ *
180
+ * To automatically call this method before each test, enable the [`mockReset`](https://vitest.dev/config/#mockreset) setting in the configuration.
181
+ * @see https://vitest.dev/api/mock#mockreset
182
+ */
183
+ mockReset(): this;
184
+ /**
185
+ * Does what `mockReset` does and restores original descriptors of spied-on objects.
186
+ *
187
+ * Note that restoring mock from `vi.fn()` will set implementation to an empty function that returns `undefined`. Restoring a `vi.fn(impl)` will restore implementation to `impl`.
188
+ * @see https://vitest.dev/api/mock#mockrestore
189
+ */
190
+ mockRestore(): void;
191
+ /**
192
+ * Returns current permanent mock implementation if there is one.
193
+ *
194
+ * If mock was created with `vi.fn`, it will consider passed down method as a mock implementation.
195
+ *
196
+ * If mock was created with `vi.spyOn`, it will return `undefined` unless a custom implementation was provided.
197
+ */
198
+ getMockImplementation(): NormalizedProcedure<T> | undefined;
199
+ /**
200
+ * Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function.
201
+ * @see https://vitest.dev/api/mock#mockimplementation
202
+ * @example
203
+ * const increment = vi.fn().mockImplementation(count => count + 1);
204
+ * expect(increment(3)).toBe(4);
205
+ */
206
+ mockImplementation(fn: NormalizedProcedure<T>): this;
207
+ /**
208
+ * Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function. This method can be chained to produce different results for multiple function calls.
209
+ *
210
+ * When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
211
+ * @see https://vitest.dev/api/mock#mockimplementationonce
212
+ * @example
213
+ * const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
214
+ * expect(fn(3)).toBe(4);
215
+ * expect(fn(3)).toBe(3);
216
+ */
217
+ mockImplementationOnce(fn: NormalizedProcedure<T>): this;
218
+ /**
219
+ * Overrides the original mock implementation temporarily while the callback is being executed.
220
+ *
221
+ * Note that this method takes precedence over the [`mockImplementationOnce`](https://vitest.dev/api/mock#mockimplementationonce).
222
+ * @see https://vitest.dev/api/mock#withimplementation
223
+ * @example
224
+ * const myMockFn = vi.fn(() => 'original')
225
+ *
226
+ * myMockFn.withImplementation(() => 'temp', () => {
227
+ * myMockFn() // 'temp'
228
+ * })
229
+ *
230
+ * myMockFn() // 'original'
231
+ */
232
+ withImplementation<T2>(fn: NormalizedProcedure<T>, cb: () => T2): T2 extends Promise<unknown> ? Promise<this> : this;
233
+ /**
234
+ * Use this if you need to return the `this` context from the method without invoking the actual implementation.
235
+ * @see https://vitest.dev/api/mock#mockreturnthis
236
+ */
237
+ mockReturnThis(): this;
238
+ /**
239
+ * Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
240
+ * @see https://vitest.dev/api/mock#mockreturnvalue
241
+ * @example
242
+ * const mock = vi.fn()
243
+ * mock.mockReturnValue(42)
244
+ * mock() // 42
245
+ * mock.mockReturnValue(43)
246
+ * mock() // 43
247
+ */
248
+ mockReturnValue(value: ReturnType<T>): this;
249
+ /**
250
+ * Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
251
+ *
252
+ * When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
253
+ * @example
254
+ * const myMockFn = vi
255
+ * .fn()
256
+ * .mockReturnValue('default')
257
+ * .mockReturnValueOnce('first call')
258
+ * .mockReturnValueOnce('second call')
259
+ *
260
+ * // 'first call', 'second call', 'default'
261
+ * console.log(myMockFn(), myMockFn(), myMockFn())
262
+ */
263
+ mockReturnValueOnce(value: ReturnType<T>): this;
264
+ /**
265
+ * Accepts a value that will be resolved when the async function is called. TypeScript will only accept values that match the return type of the original function.
266
+ * @example
267
+ * const asyncMock = vi.fn().mockResolvedValue(42)
268
+ * asyncMock() // Promise<42>
269
+ */
270
+ mockResolvedValue(value: Awaited<ReturnType<T>>): this;
271
+ /**
272
+ * Accepts a value that will be resolved during the next function call. TypeScript will only accept values that match the return type of the original function. If chained, each consecutive call will resolve the specified value.
273
+ * @example
274
+ * const myMockFn = vi
275
+ * .fn()
276
+ * .mockResolvedValue('default')
277
+ * .mockResolvedValueOnce('first call')
278
+ * .mockResolvedValueOnce('second call')
279
+ *
280
+ * // Promise<'first call'>, Promise<'second call'>, Promise<'default'>
281
+ * console.log(myMockFn(), myMockFn(), myMockFn())
282
+ */
283
+ mockResolvedValueOnce(value: Awaited<ReturnType<T>>): this;
284
+ /**
285
+ * Accepts an error that will be rejected when async function is called.
286
+ * @example
287
+ * const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
288
+ * await asyncMock() // throws Error<'Async error'>
289
+ */
290
+ mockRejectedValue(error: unknown): this;
291
+ /**
292
+ * Accepts a value that will be rejected during the next function call. If chained, each consecutive call will reject the specified value.
293
+ * @example
294
+ * const asyncMock = vi
295
+ * .fn()
296
+ * .mockResolvedValueOnce('first call')
297
+ * .mockRejectedValueOnce(new Error('Async error'))
298
+ *
299
+ * await asyncMock() // first call
300
+ * await asyncMock() // throws Error<'Async error'>
301
+ */
302
+ mockRejectedValueOnce(error: unknown): this;
303
+ }
304
+ /* eslint-enable ts/method-signature-style */
305
+ interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
306
+ new (...args: Parameters<T>): ReturnType<T>;
307
+ (...args: Parameters<T>): ReturnType<T>;
308
+ }
2
309
 
3
310
  declare const revalidatePath: Mock<(...args: any[]) => any>;
4
311
  declare const revalidateTag: Mock<(...args: any[]) => any>;
@@ -1 +1,22 @@
1
- "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__copyProps=(to,from,except,desc)=>{if(from&&typeof from=="object"||typeof from=="function")for(let key of __getOwnPropNames(from))!__hasOwnProp.call(to,key)&&key!==except&&__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:!0}),mod);var cache_exports={};__export(cache_exports,{default:()=>cache_default,revalidatePath:()=>revalidatePath,revalidateTag:()=>revalidateTag,unstable_cache:()=>unstable_cache,unstable_noStore:()=>unstable_noStore});module.exports=__toCommonJS(cache_exports);var import_test=require("storybook/test"),revalidatePath=(0,import_test.fn)().mockName("next/cache::revalidatePath"),revalidateTag=(0,import_test.fn)().mockName("next/cache::revalidateTag"),unstable_cache=(0,import_test.fn)().mockName("next/cache::unstable_cache").mockImplementation(cb=>cb),unstable_noStore=(0,import_test.fn)().mockName("next/cache::unstable_noStore"),cacheExports={unstable_cache,revalidateTag,revalidatePath,unstable_noStore},cache_default=cacheExports;0&&(module.exports={revalidatePath,revalidateTag,unstable_cache,unstable_noStore});
1
+ import "../../_browser-chunks/chunk-L5NVL7MD.js";
2
+
3
+ // src/export-mocks/cache/index.ts
4
+ import { fn } from "storybook/test";
5
+ var revalidatePath = fn().mockName("next/cache::revalidatePath");
6
+ var revalidateTag = fn().mockName("next/cache::revalidateTag");
7
+ var unstable_cache = fn().mockName("next/cache::unstable_cache").mockImplementation((cb) => cb);
8
+ var unstable_noStore = fn().mockName("next/cache::unstable_noStore");
9
+ var cacheExports = {
10
+ unstable_cache,
11
+ revalidateTag,
12
+ revalidatePath,
13
+ unstable_noStore
14
+ };
15
+ var cache_default = cacheExports;
16
+ export {
17
+ cache_default as default,
18
+ revalidatePath,
19
+ revalidateTag,
20
+ unstable_cache,
21
+ unstable_noStore
22
+ };
@@ -1,10 +1,318 @@
1
- import { M as Mock } from '../../index.d-ff220430.js';
2
1
  import { draftMode as draftMode$1 } from 'next/dist/server/request/draft-mode';
3
2
  export * from 'next/dist/server/request/headers';
4
3
  import { HeadersAdapter } from 'next/dist/server/web/spec-extension/adapters/headers';
5
4
  import * as next_dist_compiled__edge_runtime_cookies from 'next/dist/compiled/@edge-runtime/cookies';
6
5
  import { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies';
7
6
 
7
+ interface MockResultReturn<T> {
8
+ type: "return";
9
+ /**
10
+ * The value that was returned from the function. If function returned a Promise, then this will be a resolved value.
11
+ */
12
+ value: T;
13
+ }
14
+ interface MockResultIncomplete {
15
+ type: "incomplete";
16
+ value: undefined;
17
+ }
18
+ interface MockResultThrow {
19
+ type: "throw";
20
+ /**
21
+ * An error that was thrown during function execution.
22
+ */
23
+ value: any;
24
+ }
25
+ interface MockSettledResultFulfilled<T> {
26
+ type: "fulfilled";
27
+ value: T;
28
+ }
29
+ interface MockSettledResultRejected {
30
+ type: "rejected";
31
+ value: any;
32
+ }
33
+ type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
34
+ type MockSettledResult<T> = MockSettledResultFulfilled<T> | MockSettledResultRejected;
35
+ interface MockContext<T extends Procedure> {
36
+ /**
37
+ * This is an array containing all arguments for each call. One item of the array is the arguments of that call.
38
+ *
39
+ * @see https://vitest.dev/api/mock#mock-calls
40
+ * @example
41
+ * const fn = vi.fn()
42
+ *
43
+ * fn('arg1', 'arg2')
44
+ * fn('arg3')
45
+ *
46
+ * fn.mock.calls === [
47
+ * ['arg1', 'arg2'], // first call
48
+ * ['arg3'], // second call
49
+ * ]
50
+ */
51
+ calls: Parameters<T>[];
52
+ /**
53
+ * This is an array containing all instances that were instantiated when mock was called with a `new` keyword. Note that this is an actual context (`this`) of the function, not a return value.
54
+ * @see https://vitest.dev/api/mock#mock-instances
55
+ */
56
+ instances: ReturnType<T>[];
57
+ /**
58
+ * An array of `this` values that were used during each call to the mock function.
59
+ * @see https://vitest.dev/api/mock#mock-contexts
60
+ */
61
+ contexts: ThisParameterType<T>[];
62
+ /**
63
+ * The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.
64
+ *
65
+ * @see https://vitest.dev/api/mock#mock-invocationcallorder
66
+ * @example
67
+ * const fn1 = vi.fn()
68
+ * const fn2 = vi.fn()
69
+ *
70
+ * fn1()
71
+ * fn2()
72
+ * fn1()
73
+ *
74
+ * fn1.mock.invocationCallOrder === [1, 3]
75
+ * fn2.mock.invocationCallOrder === [2]
76
+ */
77
+ invocationCallOrder: number[];
78
+ /**
79
+ * This is an array containing all values that were `returned` from the function.
80
+ *
81
+ * The `value` property contains the returned value or thrown error. If the function returned a `Promise`, then `result` will always be `'return'` even if the promise was rejected.
82
+ *
83
+ * @see https://vitest.dev/api/mock#mock-results
84
+ * @example
85
+ * const fn = vi.fn()
86
+ * .mockReturnValueOnce('result')
87
+ * .mockImplementationOnce(() => { throw new Error('thrown error') })
88
+ *
89
+ * const result = fn()
90
+ *
91
+ * try {
92
+ * fn()
93
+ * }
94
+ * catch {}
95
+ *
96
+ * fn.mock.results === [
97
+ * {
98
+ * type: 'return',
99
+ * value: 'result',
100
+ * },
101
+ * {
102
+ * type: 'throw',
103
+ * value: Error,
104
+ * },
105
+ * ]
106
+ */
107
+ results: MockResult<ReturnType<T>>[];
108
+ /**
109
+ * An array containing all values that were `resolved` or `rejected` from the function.
110
+ *
111
+ * This array will be empty if the function was never resolved or rejected.
112
+ *
113
+ * @see https://vitest.dev/api/mock#mock-settledresults
114
+ * @example
115
+ * const fn = vi.fn().mockResolvedValueOnce('result')
116
+ *
117
+ * const result = fn()
118
+ *
119
+ * fn.mock.settledResults === []
120
+ * fn.mock.results === [
121
+ * {
122
+ * type: 'return',
123
+ * value: Promise<'result'>,
124
+ * },
125
+ * ]
126
+ *
127
+ * await result
128
+ *
129
+ * fn.mock.settledResults === [
130
+ * {
131
+ * type: 'fulfilled',
132
+ * value: 'result',
133
+ * },
134
+ * ]
135
+ */
136
+ settledResults: MockSettledResult<Awaited<ReturnType<T>>>[];
137
+ /**
138
+ * This contains the arguments of the last call. If spy wasn't called, will return `undefined`.
139
+ * @see https://vitest.dev/api/mock#mock-lastcall
140
+ */
141
+ lastCall: Parameters<T> | undefined;
142
+ }
143
+ type Procedure = (...args: any[]) => any;
144
+ // pick a single function type from function overloads, unions, etc...
145
+ type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>) => ReturnType<T>;
146
+ /*
147
+ cf. https://typescript-eslint.io/rules/method-signature-style/
148
+
149
+ Typescript assignability is different between
150
+ { foo: (f: T) => U } (this is "method-signature-style")
151
+ and
152
+ { foo(f: T): U }
153
+
154
+ Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows assignment such as:
155
+ const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true)
156
+ */
157
+ /* eslint-disable ts/method-signature-style */
158
+ interface MockInstance<T extends Procedure = Procedure> extends Disposable {
159
+ /**
160
+ * Use it to return the name assigned to the mock with the `.mockName(name)` method. By default, it will return `vi.fn()`.
161
+ * @see https://vitest.dev/api/mock#getmockname
162
+ */
163
+ getMockName(): string;
164
+ /**
165
+ * Sets the internal mock name. This is useful for identifying the mock when an assertion fails.
166
+ * @see https://vitest.dev/api/mock#mockname
167
+ */
168
+ mockName(name: string): this;
169
+ /**
170
+ * Current context of the mock. It stores information about all invocation calls, instances, and results.
171
+ */
172
+ mock: MockContext<T>;
173
+ /**
174
+ * Clears all information about every call. After calling it, all properties on `.mock` will return to their initial state. This method does not reset implementations. It is useful for cleaning up mocks between different assertions.
175
+ *
176
+ * To automatically call this method before each test, enable the [`clearMocks`](https://vitest.dev/config/#clearmocks) setting in the configuration.
177
+ * @see https://vitest.dev/api/mock#mockclear
178
+ */
179
+ mockClear(): this;
180
+ /**
181
+ * Does what `mockClear` does and resets inner implementation to the original function. This also resets all "once" implementations.
182
+ *
183
+ * Note that resetting a mock from `vi.fn()` will set implementation to an empty function that returns `undefined`.
184
+ * Resetting a mock from `vi.fn(impl)` will set implementation to `impl`. It is useful for completely resetting a mock to its default state.
185
+ *
186
+ * To automatically call this method before each test, enable the [`mockReset`](https://vitest.dev/config/#mockreset) setting in the configuration.
187
+ * @see https://vitest.dev/api/mock#mockreset
188
+ */
189
+ mockReset(): this;
190
+ /**
191
+ * Does what `mockReset` does and restores original descriptors of spied-on objects.
192
+ *
193
+ * Note that restoring mock from `vi.fn()` will set implementation to an empty function that returns `undefined`. Restoring a `vi.fn(impl)` will restore implementation to `impl`.
194
+ * @see https://vitest.dev/api/mock#mockrestore
195
+ */
196
+ mockRestore(): void;
197
+ /**
198
+ * Returns current permanent mock implementation if there is one.
199
+ *
200
+ * If mock was created with `vi.fn`, it will consider passed down method as a mock implementation.
201
+ *
202
+ * If mock was created with `vi.spyOn`, it will return `undefined` unless a custom implementation was provided.
203
+ */
204
+ getMockImplementation(): NormalizedProcedure<T> | undefined;
205
+ /**
206
+ * Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function.
207
+ * @see https://vitest.dev/api/mock#mockimplementation
208
+ * @example
209
+ * const increment = vi.fn().mockImplementation(count => count + 1);
210
+ * expect(increment(3)).toBe(4);
211
+ */
212
+ mockImplementation(fn: NormalizedProcedure<T>): this;
213
+ /**
214
+ * Accepts a function to be used as the mock implementation. TypeScript expects the arguments and return type to match those of the original function. This method can be chained to produce different results for multiple function calls.
215
+ *
216
+ * When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
217
+ * @see https://vitest.dev/api/mock#mockimplementationonce
218
+ * @example
219
+ * const fn = vi.fn(count => count).mockImplementationOnce(count => count + 1);
220
+ * expect(fn(3)).toBe(4);
221
+ * expect(fn(3)).toBe(3);
222
+ */
223
+ mockImplementationOnce(fn: NormalizedProcedure<T>): this;
224
+ /**
225
+ * Overrides the original mock implementation temporarily while the callback is being executed.
226
+ *
227
+ * Note that this method takes precedence over the [`mockImplementationOnce`](https://vitest.dev/api/mock#mockimplementationonce).
228
+ * @see https://vitest.dev/api/mock#withimplementation
229
+ * @example
230
+ * const myMockFn = vi.fn(() => 'original')
231
+ *
232
+ * myMockFn.withImplementation(() => 'temp', () => {
233
+ * myMockFn() // 'temp'
234
+ * })
235
+ *
236
+ * myMockFn() // 'original'
237
+ */
238
+ withImplementation<T2>(fn: NormalizedProcedure<T>, cb: () => T2): T2 extends Promise<unknown> ? Promise<this> : this;
239
+ /**
240
+ * Use this if you need to return the `this` context from the method without invoking the actual implementation.
241
+ * @see https://vitest.dev/api/mock#mockreturnthis
242
+ */
243
+ mockReturnThis(): this;
244
+ /**
245
+ * Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
246
+ * @see https://vitest.dev/api/mock#mockreturnvalue
247
+ * @example
248
+ * const mock = vi.fn()
249
+ * mock.mockReturnValue(42)
250
+ * mock() // 42
251
+ * mock.mockReturnValue(43)
252
+ * mock() // 43
253
+ */
254
+ mockReturnValue(value: ReturnType<T>): this;
255
+ /**
256
+ * Accepts a value that will be returned whenever the mock function is called. TypeScript will only accept values that match the return type of the original function.
257
+ *
258
+ * When the mocked function runs out of implementations, it will invoke the default implementation set with `vi.fn(() => defaultValue)` or `.mockImplementation(() => defaultValue)` if they were called.
259
+ * @example
260
+ * const myMockFn = vi
261
+ * .fn()
262
+ * .mockReturnValue('default')
263
+ * .mockReturnValueOnce('first call')
264
+ * .mockReturnValueOnce('second call')
265
+ *
266
+ * // 'first call', 'second call', 'default'
267
+ * console.log(myMockFn(), myMockFn(), myMockFn())
268
+ */
269
+ mockReturnValueOnce(value: ReturnType<T>): this;
270
+ /**
271
+ * Accepts a value that will be resolved when the async function is called. TypeScript will only accept values that match the return type of the original function.
272
+ * @example
273
+ * const asyncMock = vi.fn().mockResolvedValue(42)
274
+ * asyncMock() // Promise<42>
275
+ */
276
+ mockResolvedValue(value: Awaited<ReturnType<T>>): this;
277
+ /**
278
+ * Accepts a value that will be resolved during the next function call. TypeScript will only accept values that match the return type of the original function. If chained, each consecutive call will resolve the specified value.
279
+ * @example
280
+ * const myMockFn = vi
281
+ * .fn()
282
+ * .mockResolvedValue('default')
283
+ * .mockResolvedValueOnce('first call')
284
+ * .mockResolvedValueOnce('second call')
285
+ *
286
+ * // Promise<'first call'>, Promise<'second call'>, Promise<'default'>
287
+ * console.log(myMockFn(), myMockFn(), myMockFn())
288
+ */
289
+ mockResolvedValueOnce(value: Awaited<ReturnType<T>>): this;
290
+ /**
291
+ * Accepts an error that will be rejected when async function is called.
292
+ * @example
293
+ * const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
294
+ * await asyncMock() // throws Error<'Async error'>
295
+ */
296
+ mockRejectedValue(error: unknown): this;
297
+ /**
298
+ * Accepts a value that will be rejected during the next function call. If chained, each consecutive call will reject the specified value.
299
+ * @example
300
+ * const asyncMock = vi
301
+ * .fn()
302
+ * .mockResolvedValueOnce('first call')
303
+ * .mockRejectedValueOnce(new Error('Async error'))
304
+ *
305
+ * await asyncMock() // first call
306
+ * await asyncMock() // throws Error<'Async error'>
307
+ */
308
+ mockRejectedValueOnce(error: unknown): this;
309
+ }
310
+ /* eslint-enable ts/method-signature-style */
311
+ interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
312
+ new (...args: Parameters<T>): ReturnType<T>;
313
+ (...args: Parameters<T>): ReturnType<T>;
314
+ }
315
+
8
316
  declare class HeadersAdapterMock extends HeadersAdapter {
9
317
  constructor();
10
318
  append: Mock<(name: string, value: string) => void>;