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