vitest 1.6.0 → 2.0.0-beta.2

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 (72) hide show
  1. package/LICENSE.md +172 -0
  2. package/dist/browser.d.ts +1 -1
  3. package/dist/browser.js +4 -4
  4. package/dist/chunks/{environments-node.vcoXCoKs.js → environments-node.39w4gmlF.js} +2 -2
  5. package/dist/chunks/{install-pkg.LE8oaA1t.js → install-pkg.DNUmWFkO.js} +2 -2
  6. package/dist/chunks/integrations-globals.B5Jl0grA.js +31 -0
  7. package/dist/chunks/{runtime-console.EO5ha7qv.js → runtime-console.CUES-L8X.js} +9 -9
  8. package/dist/chunks/{runtime-runBaseTests.oAvMKtQC.js → runtime-runBaseTests._dXkRAZc.js} +18 -18
  9. package/dist/cli.js +3 -3
  10. package/dist/config.cjs +2 -3
  11. package/dist/config.d.ts +1 -1
  12. package/dist/config.js +2 -3
  13. package/dist/coverage.d.ts +1 -1
  14. package/dist/coverage.js +2 -2
  15. package/dist/environments.d.ts +1 -1
  16. package/dist/environments.js +1 -1
  17. package/dist/execute.d.ts +2 -1
  18. package/dist/execute.js +2 -2
  19. package/dist/index.d.ts +769 -21
  20. package/dist/index.js +12 -12
  21. package/dist/node.d.ts +2 -2
  22. package/dist/node.js +20 -21
  23. package/dist/{reporters-yx5ZTtEV.d.ts → reporters-DFgqsvtL.d.ts} +29 -32
  24. package/dist/reporters.d.ts +1 -1
  25. package/dist/reporters.js +10 -11
  26. package/dist/runners.d.ts +1 -1
  27. package/dist/runners.js +17 -18
  28. package/dist/snapshot.js +3 -3
  29. package/dist/{suite-IbNSsUWN.d.ts → suite-C_sqQjdz.d.ts} +1 -1
  30. package/dist/suite.d.ts +2 -2
  31. package/dist/suite.js +4 -4
  32. package/dist/vendor/{base.Ybri3C14.js → base.VFkIJ66g.js} +3 -3
  33. package/dist/vendor/{base.5NT-gWu5.js → base._gnK9Slw.js} +1 -2
  34. package/dist/vendor/{benchmark.yGkUTKnC.js → benchmark.BNLebNi5.js} +1 -1
  35. package/dist/vendor/{cac.EdDItJD-.js → cac.CtP0aXu-.js} +5 -6
  36. package/dist/vendor/{cli-api.E07AF1Yq.js → cli-api.B2QW76dQ.js} +3061 -362
  37. package/dist/vendor/{execute.fL3szUAI.js → execute.CLLNVNnK.js} +1 -1
  38. package/dist/vendor/{index.DpVgvm2P.js → index.BfoZyXD1.js} +5 -5
  39. package/dist/vendor/{index.8bPxjt7g.js → index.BpSiYbpB.js} +5 -1
  40. package/dist/vendor/{index.Q04MCqDO.js → index.CRxYS9H3.js} +414 -113
  41. package/dist/vendor/{index.dI9lHwVn.js → index.CmILuxzC.js} +8 -6
  42. package/dist/vendor/{index.xL8XjTLv.js → index.DP-km6lF.js} +1 -1
  43. package/dist/vendor/{index.SMVOaj7F.js → index._7XLd8Kd.js} +2 -2
  44. package/dist/vendor/{rpc.joBhAkyK.js → rpc.DRDE9Pu1.js} +2 -2
  45. package/dist/vendor/{run-once.Olz_Zkd8.js → run-once.DLomgGUH.js} +1 -1
  46. package/dist/vendor/{setup-common.8nJLd4ay.js → setup-common.XeoZAW8t.js} +2 -2
  47. package/dist/vendor/{tasks.IknbGB2n.js → tasks.WC7M-K-v.js} +4 -1
  48. package/dist/vendor/{utils.dEtNIEgr.js → utils.D5gGkwyH.js} +1 -1
  49. package/dist/vendor/{vi.YFlodzP_.js → vi.ClD3hi7L.js} +23 -10
  50. package/dist/vendor/{vm.QEE48c0T.js → vm.Bi3bljci.js} +47 -59
  51. package/dist/worker.js +6 -6
  52. package/dist/workers/forks.js +5 -5
  53. package/dist/workers/runVmTests.js +17 -17
  54. package/dist/workers/threads.js +5 -5
  55. package/dist/workers/vmForks.js +8 -8
  56. package/dist/workers/vmThreads.js +8 -8
  57. package/dist/workers.d.ts +1 -1
  58. package/dist/workers.js +13 -13
  59. package/package.json +29 -29
  60. package/vitest.mjs +1 -1
  61. package/dist/chunks/integrations-globals.kw4co3rx.js +0 -31
  62. package/dist/cli-wrapper.js +0 -119
  63. /package/dist/chunks/{node-git.Hw101KjS.js → node-git.CCI8evVZ.js} +0 -0
  64. /package/dist/vendor/{_commonjsHelpers.jjO7Zipk.js → _commonjsHelpers.BFTU3MAI.js} +0 -0
  65. /package/dist/vendor/{constants.5J7I254_.js → constants.5SOfHUj0.js} +0 -0
  66. /package/dist/vendor/{coverage.E7sG1b3r.js → coverage.ChSqD-qS.js} +0 -0
  67. /package/dist/vendor/{date.Ns1pGd_X.js → date.BKM1wewY.js} +0 -0
  68. /package/dist/vendor/{env.AtSIuHFg.js → env.bmJgw1qP.js} +0 -0
  69. /package/dist/vendor/{global.CkGT_TMy.js → global.7bFbnyXl.js} +0 -0
  70. /package/dist/vendor/{index.GVFv9dZ0.js → index.DeR1hhfY.js} +0 -0
  71. /package/dist/vendor/{inspector.IgLX3ur5.js → inspector.hPQncR7V.js} +0 -0
  72. /package/dist/vendor/{utils.0uYuCbzo.js → utils.CUjzkRH7.js} +0 -0
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { TaskPopulated, File, TaskResultPack, CancelReason } from '@vitest/runner';
2
2
  export { Custom, DoneCallback, ExtendedContext, File, HookCleanupCallback, HookListener, OnTestFailedHandler, RunMode, RuntimeContext, SequenceHooks, SequenceSetupFiles, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, TaskBase, TaskContext, TaskCustomOptions, TaskMeta, TaskResult, TaskResultPack, TaskState, Test, TestAPI, TestContext, TestFunction, TestOptions, afterAll, afterEach, beforeAll, beforeEach, describe, it, onTestFailed, onTestFinished, suite, test } from '@vitest/runner';
3
- export { b as bench } from './suite-IbNSsUWN.js';
3
+ export { b as bench } from './suite-C_sqQjdz.js';
4
4
  import { ExpectStatic } from '@vitest/expect';
5
5
  export { Assertion, AsymmetricMatchersContaining, ExpectStatic, JestAssertion } from '@vitest/expect';
6
- import { F as FakeTimerInstallOpts, M as MockFactoryWithHelper, s as RuntimeConfig, P as ProvidedContext, A as AfterSuiteRunMeta, t as UserConsoleLog, R as ResolvedConfig, u as ModuleGraphData, v as Reporter } from './reporters-yx5ZTtEV.js';
7
- export { Q as ApiConfig, aa as ArgumentsType, a9 as Arrayable, a7 as Awaitable, B as BaseCoverageOptions, ap as BenchFunction, an as Benchmark, aq as BenchmarkAPI, ao as BenchmarkResult, am as BenchmarkUserOptions, a0 as BrowserConfigOptions, r as BrowserScript, L as BuiltinEnvironment, O as CSSModuleScopeStrategy, y as CollectLineNumbers, z as CollectLines, ac as Constructable, G as Context, f as ContextRPC, a3 as ContextTestEnvironment, aj as CoverageIstanbulOptions, C as CoverageOptions, b as CoverageProvider, c as CoverageProviderModule, ai as CoverageReporter, ak as CoverageV8Options, al as CustomProviderOptions, X as DepsOptimizationOptions, E as Environment, S as EnvironmentOptions, ae as EnvironmentReturn, K as HappyDOMOptions, Z as InlineConfig, J as JSDOMOptions, ad as ModuleCache, ab as MutableArray, a8 as Nullable, ag as OnServerRestartHandler, H as Pool, I as PoolOptions, $ as ProjectConfig, w as RawErrsMap, ah as ReportContext, a5 as ResolveIdFunction, a as ResolvedCoverageOptions, a4 as ResolvedTestEnvironment, D as RootAndTarget, a2 as RunnerRPC, e as RuntimeRPC, Y as TransformModePatterns, x as TscErrorInfo, _ as TypecheckConfig, U as UserConfig, a1 as UserWorkspaceConfig, i as Vitest, N as VitestEnvironment, V as VitestRunMode, af as VmEnvironmentReturn, g as WorkerContext, W as WorkerGlobalState, a6 as WorkerRPC } from './reporters-yx5ZTtEV.js';
6
+ import { F as FakeTimerInstallOpts, M as MockFactoryWithHelper, s as RuntimeConfig, P as ProvidedContext, A as AfterSuiteRunMeta, t as UserConsoleLog, R as ResolvedConfig, u as ModuleGraphData, v as Reporter } from './reporters-DFgqsvtL.js';
7
+ export { Q as ApiConfig, aa as ArgumentsType, a9 as Arrayable, a7 as Awaitable, B as BaseCoverageOptions, ap as BenchFunction, an as Benchmark, aq as BenchmarkAPI, ao as BenchmarkResult, am as BenchmarkUserOptions, a0 as BrowserConfigOptions, r as BrowserScript, L as BuiltinEnvironment, O as CSSModuleScopeStrategy, y as CollectLineNumbers, z as CollectLines, ac as Constructable, G as Context, f as ContextRPC, a3 as ContextTestEnvironment, aj as CoverageIstanbulOptions, C as CoverageOptions, b as CoverageProvider, c as CoverageProviderModule, ai as CoverageReporter, ak as CoverageV8Options, al as CustomProviderOptions, X as DepsOptimizationOptions, E as Environment, S as EnvironmentOptions, ae as EnvironmentReturn, K as HappyDOMOptions, Z as InlineConfig, J as JSDOMOptions, ad as ModuleCache, ab as MutableArray, a8 as Nullable, ag as OnServerRestartHandler, H as Pool, I as PoolOptions, $ as ProjectConfig, w as RawErrsMap, ah as ReportContext, a5 as ResolveIdFunction, a as ResolvedCoverageOptions, a4 as ResolvedTestEnvironment, D as RootAndTarget, a2 as RunnerRPC, e as RuntimeRPC, Y as TransformModePatterns, x as TscErrorInfo, _ as TypecheckConfig, U as UserConfig, a1 as UserWorkspaceConfig, i as Vitest, N as VitestEnvironment, V as VitestRunMode, af as VmEnvironmentReturn, g as WorkerContext, W as WorkerGlobalState, a6 as WorkerRPC } from './reporters-DFgqsvtL.js';
8
8
  import { spyOn, fn, MaybeMockedDeep, MaybeMocked, MaybePartiallyMocked, MaybePartiallyMockedDeep, MockInstance } from '@vitest/spy';
9
9
  export { Mock, MockContext, MockInstance, Mocked, MockedClass, MockedFunction, MockedObject, SpyInstance } from '@vitest/spy';
10
10
  export { SnapshotEnvironment } from '@vitest/snapshot/environment';
@@ -26,20 +26,50 @@ import 'vite-node/server';
26
26
  import 'node:worker_threads';
27
27
  import 'node:fs';
28
28
 
29
+ /**
30
+ * Negates a boolean type.
31
+ */
29
32
  declare type Not<T extends boolean> = T extends true ? false : true;
33
+ /**
34
+ * Checks if all the boolean types in the {@linkcode Types} array are `true`.
35
+ */
30
36
  declare type And<Types extends boolean[]> = Types[number] extends true ? true : false;
37
+ /**
38
+ * Represents an equality type that returns {@linkcode Right} if
39
+ * {@linkcode Left} is `true`,
40
+ * otherwise returns the negation of {@linkcode Right}.
41
+ */
31
42
  declare type Eq<Left extends boolean, Right extends boolean> = Left extends true ? Right : Not<Right>;
32
43
  declare const secret: unique symbol;
33
44
  declare type Secret = typeof secret;
45
+ /**
46
+ * Checks if the given type is `never`.
47
+ */
34
48
  declare type IsNever<T> = [T] extends [never] ? true : false;
49
+ /**
50
+ * Checks if the given type is `any`.
51
+ */
35
52
  declare type IsAny<T> = [T] extends [Secret] ? Not<IsNever<T>> : false;
53
+ /**
54
+ * Determines if the given type is `unknown`.
55
+ */
36
56
  declare type IsUnknown<T> = [unknown] extends [T] ? Not<IsAny<T>> : false;
57
+ /**
58
+ * Determines the printable type representation for a given type.
59
+ */
37
60
  declare type PrintType<T> = IsUnknown<T> extends true ? 'unknown' : IsNever<T> extends true ? 'never' : IsAny<T> extends true ? never : boolean extends T ? 'boolean' : T extends boolean ? `literal boolean: ${T}` : string extends T ? 'string' : T extends string ? `literal string: ${T}` : number extends T ? 'number' : T extends number ? `literal number: ${T}` : T extends null ? 'null' : T extends undefined ? 'undefined' : T extends (...args: any[]) => any ? 'function' : '...';
38
- /** Subjective "useful" keys from a type. For objects it's just `keyof` but for tuples/arrays it's the number keys
61
+ /**
62
+ * Subjective "useful" keys from a type. For objects it's just `keyof` but for
63
+ * tuples/arrays it's the number keys.
64
+ *
39
65
  * @example
66
+ * ```ts
40
67
  * UsefulKeys<{a: 1; b: 2}> // 'a' | 'b'
68
+ *
41
69
  * UsefulKeys<['a', 'b']> // '0' | '1'
70
+ *
42
71
  * UsefulKeys<string[]> // number
72
+ * ```
43
73
  */
44
74
  declare type UsefulKeys<T> = T extends any[] ? {
45
75
  [K in keyof T]: K;
@@ -48,15 +78,20 @@ declare type MismatchInfo<Actual, Expected> = And<[Extends<PrintType<Actual>, '.
48
78
  [K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<K extends keyof Actual ? Actual[K] : never, K extends keyof Expected ? Expected[K] : never>;
49
79
  } : StrictEqualUsingBranding<Actual, Expected> extends true ? Actual : `Expected: ${PrintType<Expected>}, Actual: ${PrintType<Exclude<Actual, Expected>>}`;
50
80
  /**
51
- * Recursively walk a type and replace it with a branded type related to the original. This is useful for
52
- * equality-checking stricter than `A extends B ? B extends A ? true : false : false`, because it detects
53
- * the difference between a few edge-case types that vanilla typescript doesn't by default:
81
+ * Represents a deeply branded type.
82
+ *
83
+ * Recursively walk a type and replace it with a branded type related to the
84
+ * original. This is useful for equality-checking stricter than
85
+ * `A extends B ? B extends A ? true : false : false`, because it detects the
86
+ * difference between a few edge-case types that vanilla typescript
87
+ * doesn't by default:
54
88
  * - `any` vs `unknown`
55
89
  * - `{ readonly a: string }` vs `{ a: string }`
56
90
  * - `{ a?: string }` vs `{ a: string | undefined }`
57
91
  *
58
- * Note: not very performant for complex types - this should only be used when you know you need it. If doing
59
- * an equality check, it's almost always better to use `StrictEqualUsingTSInternalIdenticalToOperator`.
92
+ * __Note__: not very performant for complex types - this should only be used
93
+ * when you know you need it. If doing an equality check, it's almost always
94
+ * better to use {@linkcode StrictEqualUsingTSInternalIdenticalToOperator}.
60
95
  */
61
96
  declare type DeepBrand<T> = IsNever<T> extends true ? {
62
97
  type: 'never';
@@ -92,10 +127,19 @@ declare type DeepBrand<T> = IsNever<T> extends true ? {
92
127
  optional: OptionalKeys<T>;
93
128
  constructorParams: DeepBrand<ConstructorParams<T>>;
94
129
  };
130
+ /**
131
+ * Extracts the keys from a type that are required (not optional).
132
+ */
95
133
  declare type RequiredKeys<T> = Extract<{
96
134
  [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
97
135
  }[keyof T], keyof T>;
136
+ /**
137
+ * Gets the keys of an object type that are optional.
138
+ */
98
139
  declare type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
140
+ /**
141
+ * Extracts the keys from a type that are not readonly.
142
+ */
99
143
  declare type ReadonlyKeys<T> = Extract<{
100
144
  [K in keyof T]-?: ReadonlyEquivalent<{
101
145
  [_K in K]: T[K];
@@ -103,27 +147,66 @@ declare type ReadonlyKeys<T> = Extract<{
103
147
  -readonly [_K in K]: T[K];
104
148
  }> extends true ? never : K;
105
149
  }[keyof T], keyof T>;
150
+ /**
151
+ * Determines if two types, are equivalent in a `readonly` manner.
152
+ */
106
153
  declare type ReadonlyEquivalent<X, Y> = Extends<(<T>() => T extends X ? true : false), (<T>() => T extends Y ? true : false)>;
107
- /** Returns true if `L extends R`. Explicitly checks for `never` since that can give unexpected results. */
154
+ /**
155
+ * Checks if one type extends another.
156
+ */
108
157
  declare type Extends<L, R> = IsNever<L> extends true ? IsNever<R> : [L] extends [R] ? true : false;
109
158
  declare type ExtendsUsingBranding<L, R> = Extends<DeepBrand<L>, DeepBrand<R>>;
110
159
  declare type ExtendsExcludingAnyOrNever<L, R> = IsAny<L> extends true ? IsAny<R> : Extends<L, R>;
160
+ /**
161
+ * Checks if two types are strictly equal using
162
+ * the TypeScript internal identical-to operator.
163
+ *
164
+ * @see {@link https://github.com/microsoft/TypeScript/issues/55188#issuecomment-1656328122 much history}
165
+ */
111
166
  declare type StrictEqualUsingTSInternalIdenticalToOperator<L, R> = (<T>() => T extends (L & T) | T ? true : false) extends <T>() => T extends (R & T) | T ? true : false ? IsNever<L> extends IsNever<R> ? true : false : false;
167
+ /**
168
+ * Checks if two types are strictly equal using branding.
169
+ */
112
170
  declare type StrictEqualUsingBranding<Left, Right> = And<[
113
171
  ExtendsUsingBranding<Left, Right>,
114
172
  ExtendsUsingBranding<Right, Left>
115
173
  ]>;
116
- declare type Params<Actual> = Actual extends (...args: infer P) => any ? P : never;
174
+ /**
175
+ * Extracts the parameter types from a function type.
176
+ */
177
+ declare type Params<Actual> = Actual extends (...args: infer ParameterTypes) => any ? ParameterTypes : never;
178
+ /**
179
+ * Represents the constructor parameters of a class or constructor function.
180
+ * If the constructor takes no arguments, an empty array is returned.
181
+ */
117
182
  declare type ConstructorParams<Actual> = Actual extends new (...args: infer P) => any ? Actual extends new () => any ? P | [] : P : never;
118
183
  declare const mismatch: unique symbol;
119
184
  declare type Mismatch = {
120
185
  [mismatch]: 'mismatch';
121
186
  };
122
- /** A type which should match anything passed as a value but *doesn't* match `Mismatch` - helps TypeScript select the right overload for `toEqualTypeOf` and `toMatchTypeOf`. */
187
+ /**
188
+ * A type which should match anything passed as a value but *doesn't*
189
+ * match {@linkcode Mismatch}. It helps TypeScript select the right overload
190
+ * for {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`} and
191
+ * {@linkcode PositiveExpectTypeOf.toMatchTypeOf `.toMatchTypeOf()`}.
192
+ */
123
193
  declare const avalue: unique symbol;
194
+ /**
195
+ * Represents a value that can be of various types.
196
+ */
124
197
  declare type AValue = {
125
198
  [avalue]?: undefined;
126
199
  } | string | number | boolean | symbol | bigint | null | undefined | void;
200
+ /**
201
+ * Represents the type of mismatched arguments between
202
+ * the actual result and the expected result.
203
+ *
204
+ * If {@linkcode ActualResult} and {@linkcode ExpectedResult} are equivalent,
205
+ * the type resolves to an empty tuple `[]`, indicating no mismatch.
206
+ * If they are not equivalent, it resolves to a tuple containing the element
207
+ * {@linkcode Mismatch}, signifying a discrepancy between
208
+ * the expected and actual results.
209
+ */
127
210
  declare type MismatchArgs<ActualResult extends boolean, ExpectedResult extends boolean> = Eq<ActualResult, ExpectedResult> extends true ? [] : [Mismatch];
128
211
  declare const inverted: unique symbol;
129
212
  declare type Inverted<T> = {
@@ -199,85 +282,746 @@ declare type ExpectNullable<T> = {
199
282
  [expectNullable]: T;
200
283
  result: Not<StrictEqualUsingBranding<T, NonNullable<T>>>;
201
284
  };
285
+ /**
286
+ * Represents a scolder function that checks if the result of an expecter
287
+ * matches the specified options.
288
+ */
202
289
  declare type Scolder<Expecter extends {
203
290
  result: boolean;
204
291
  }, Options extends {
205
292
  positive: boolean;
206
293
  }> = Expecter['result'] extends Options['positive'] ? () => true : Options['positive'] extends true ? Expecter : Inverted<Expecter>;
294
+ /**
295
+ * Represents the positive assertion methods available for type checking in the
296
+ * {@linkcode expectTypeOf()} utility.
297
+ */
207
298
  interface PositiveExpectTypeOf<Actual> extends BaseExpectTypeOf<Actual, {
208
299
  positive: true;
209
300
  branded: false;
210
301
  }> {
211
302
  toEqualTypeOf: {
303
+ /**
304
+ * Uses TypeScript's internal technique to check for type "identicalness".
305
+ *
306
+ * It will check if the types are fully equal to each other.
307
+ * It will not fail if two objects have different values, but the same type.
308
+ * It will fail however if an object is missing a property.
309
+ *
310
+ * **_Unexpected failure_**? For a more permissive but less performant
311
+ * check that accommodates for equivalent intersection types,
312
+ * use {@linkcode branded `.branded.toEqualTypeOf()`}.
313
+ * @see {@link https://github.com/mmkal/expect-type#why-is-my-assertion-failing The documentation for details}.
314
+ *
315
+ * @example
316
+ * <caption>Using generic type argument syntax</caption>
317
+ * ```ts
318
+ * expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
319
+ *
320
+ * expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
321
+ * ```
322
+ *
323
+ * @example
324
+ * <caption>Using inferred type syntax by passing a value</caption>
325
+ * ```ts
326
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
327
+ *
328
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 })
329
+ * ```
330
+ *
331
+ * @param value - The value to compare against the expected type.
332
+ * @param MISMATCH - The mismatch arguments.
333
+ * @returns `true`.
334
+ */
212
335
  <Expected extends StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected> extends true ? unknown : MismatchInfo<Actual, Expected>>(value: Expected & AValue, // reason for `& AValue`: make sure this is only the selected overload when the end-user passes a value for an inferred typearg. The `Mismatch` type does match `AValue`.
213
336
  ...MISMATCH: MismatchArgs<StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected>, true>): true;
337
+ /**
338
+ * Uses TypeScript's internal technique to check for type "identicalness".
339
+ *
340
+ * It will check if the types are fully equal to each other.
341
+ * It will not fail if two objects have different values, but the same type.
342
+ * It will fail however if an object is missing a property.
343
+ *
344
+ * **_Unexpected failure_**? For a more permissive but less performant
345
+ * check that accommodates for equivalent intersection types,
346
+ * use {@linkcode branded `.branded.toEqualTypeOf()`}.
347
+ * @see {@link https://github.com/mmkal/expect-type#why-is-my-assertion-failing The documentation for details}.
348
+ *
349
+ * @example
350
+ * <caption>Using generic type argument syntax</caption>
351
+ * ```ts
352
+ * expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
353
+ *
354
+ * expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
355
+ * ```
356
+ *
357
+ * @example
358
+ * <caption>Using inferred type syntax by passing a value</caption>
359
+ * ```ts
360
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
361
+ *
362
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 })
363
+ * ```
364
+ *
365
+ * @param MISMATCH - The mismatch arguments.
366
+ * @returns `true`.
367
+ */
214
368
  <Expected extends StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected> extends true ? unknown : MismatchInfo<Actual, Expected>>(...MISMATCH: MismatchArgs<StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected>, true>): true;
215
369
  };
216
370
  toMatchTypeOf: {
371
+ /**
372
+ * A less strict version of {@linkcode toEqualTypeOf `.toEqualTypeOf()`}
373
+ * that allows for extra properties.
374
+ * This is roughly equivalent to an `extends` constraint
375
+ * in a function type argument.
376
+ *
377
+ * @example
378
+ * <caption>Using generic type argument syntax</caption>
379
+ * ```ts
380
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf<{ a: number }>()
381
+ * ```
382
+ *
383
+ * @example
384
+ * <caption>Using inferred type syntax by passing a value</caption>
385
+ * ```ts
386
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 2 })
387
+ * ```
388
+ *
389
+ * @param value - The value to compare against the expected type.
390
+ * @param MISMATCH - The mismatch arguments.
391
+ * @returns `true`.
392
+ */
217
393
  <Expected extends Extends<Actual, Expected> extends true ? unknown : MismatchInfo<Actual, Expected>>(value: Expected & AValue, // reason for `& AValue`: make sure this is only the selected overload when the end-user passes a value for an inferred typearg. The `Mismatch` type does match `AValue`.
218
394
  ...MISMATCH: MismatchArgs<Extends<Actual, Expected>, true>): true;
395
+ /**
396
+ * A less strict version of {@linkcode toEqualTypeOf `.toEqualTypeOf()`}
397
+ * that allows for extra properties.
398
+ * This is roughly equivalent to an `extends` constraint
399
+ * in a function type argument.
400
+ *
401
+ * @example
402
+ * <caption>Using generic type argument syntax</caption>
403
+ * ```ts
404
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf<{ a: number }>()
405
+ * ```
406
+ *
407
+ * @example
408
+ * <caption>Using inferred type syntax by passing a value</caption>
409
+ * ```ts
410
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 2 })
411
+ * ```
412
+ *
413
+ * @param MISMATCH - The mismatch arguments.
414
+ * @returns `true`.
415
+ */
219
416
  <Expected extends Extends<Actual, Expected> extends true ? unknown : MismatchInfo<Actual, Expected>>(...MISMATCH: MismatchArgs<Extends<Actual, Expected>, true>): true;
220
417
  };
221
- toHaveProperty: <K extends keyof Actual>(key: K, ...MISMATCH: MismatchArgs<Extends<K, keyof Actual>, true>) => K extends keyof Actual ? PositiveExpectTypeOf<Actual[K]> : true;
418
+ /**
419
+ * Checks whether an object has a given property.
420
+ *
421
+ * @example
422
+ * <caption>check that properties exist</caption>
423
+ * ```ts
424
+ * const obj = {a: 1, b: ''}
425
+ *
426
+ * expectTypeOf(obj).toHaveProperty('a')
427
+ *
428
+ * expectTypeOf(obj).not.toHaveProperty('c')
429
+ * ```
430
+ *
431
+ * @param key - The property key to check for.
432
+ * @param MISMATCH - The mismatch arguments.
433
+ * @returns `true`.
434
+ */
435
+ toHaveProperty: <KeyType extends keyof Actual>(key: KeyType, ...MISMATCH: MismatchArgs<Extends<KeyType, keyof Actual>, true>) => KeyType extends keyof Actual ? PositiveExpectTypeOf<Actual[KeyType]> : true;
436
+ /**
437
+ * Inverts the result of the following assertions.
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * expectTypeOf({ a: 1 }).not.toMatchTypeOf({ b: 1 })
442
+ * ```
443
+ */
222
444
  not: NegativeExpectTypeOf<Actual>;
445
+ /**
446
+ * Intersection types can cause issues with
447
+ * {@linkcode toEqualTypeOf `.toEqualTypeOf()`}:
448
+ * ```ts
449
+ * // ❌ The following line doesn't compile, even though the types are arguably the same.
450
+ * expectTypeOf<{ a: 1 } & { b: 2 }>().toEqualTypeOf<{ a: 1; b: 2 }>()
451
+ * ```
452
+ * This helper works around this problem by using
453
+ * a more permissive but less performant check.
454
+ *
455
+ * __Note__: This comes at a performance cost, and can cause the compiler
456
+ * to 'give up' if used with excessively deep types, so use sparingly.
457
+ *
458
+ * @see {@link https://github.com/mmkal/expect-type/pull/21 Reference}
459
+ */
223
460
  branded: {
461
+ /**
462
+ * Uses TypeScript's internal technique to check for type "identicalness".
463
+ *
464
+ * It will check if the types are fully equal to each other.
465
+ * It will not fail if two objects have different values, but the same type.
466
+ * It will fail however if an object is missing a property.
467
+ *
468
+ * **_Unexpected failure_**? For a more permissive but less performant
469
+ * check that accommodates for equivalent intersection types,
470
+ * use {@linkcode PositiveExpectTypeOf.branded `.branded.toEqualTypeOf()`}.
471
+ * @see {@link https://github.com/mmkal/expect-type#why-is-my-assertion-failing The documentation for details}.
472
+ *
473
+ * @example
474
+ * <caption>Using generic type argument syntax</caption>
475
+ * ```ts
476
+ * expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
477
+ *
478
+ * expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
479
+ * ```
480
+ *
481
+ * @example
482
+ * <caption>Using inferred type syntax by passing a value</caption>
483
+ * ```ts
484
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
485
+ *
486
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 })
487
+ * ```
488
+ *
489
+ * @param MISMATCH - The mismatch arguments.
490
+ * @returns `true`.
491
+ */
224
492
  toEqualTypeOf: <Expected extends StrictEqualUsingBranding<Actual, Expected> extends true ? unknown : MismatchInfo<Actual, Expected>>(...MISMATCH: MismatchArgs<StrictEqualUsingBranding<Actual, Expected>, true>) => true;
225
493
  };
226
494
  }
495
+ /**
496
+ * Represents the negative expectation type for the {@linkcode Actual} type.
497
+ */
227
498
  interface NegativeExpectTypeOf<Actual> extends BaseExpectTypeOf<Actual, {
228
499
  positive: false;
229
500
  }> {
230
501
  toEqualTypeOf: {
502
+ /**
503
+ * Uses TypeScript's internal technique to check for type "identicalness".
504
+ *
505
+ * It will check if the types are fully equal to each other.
506
+ * It will not fail if two objects have different values, but the same type.
507
+ * It will fail however if an object is missing a property.
508
+ *
509
+ * **_Unexpected failure_**? For a more permissive but less performant
510
+ * check that accommodates for equivalent intersection types,
511
+ * use {@linkcode PositiveExpectTypeOf.branded `.branded.toEqualTypeOf()`}.
512
+ * @see {@link https://github.com/mmkal/expect-type#why-is-my-assertion-failing The documentation for details}.
513
+ *
514
+ * @example
515
+ * <caption>Using generic type argument syntax</caption>
516
+ * ```ts
517
+ * expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
518
+ *
519
+ * expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
520
+ * ```
521
+ *
522
+ * @example
523
+ * <caption>Using inferred type syntax by passing a value</caption>
524
+ * ```ts
525
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
526
+ *
527
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 })
528
+ * ```
529
+ *
530
+ * @param value - The value to compare against the expected type.
531
+ * @param MISMATCH - The mismatch arguments.
532
+ * @returns `true`.
533
+ */
231
534
  <Expected>(value: Expected & AValue, ...MISMATCH: MismatchArgs<StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected>, false>): true;
535
+ /**
536
+ * Uses TypeScript's internal technique to check for type "identicalness".
537
+ *
538
+ * It will check if the types are fully equal to each other.
539
+ * It will not fail if two objects have different values, but the same type.
540
+ * It will fail however if an object is missing a property.
541
+ *
542
+ * **_Unexpected failure_**? For a more permissive but less performant
543
+ * check that accommodates for equivalent intersection types,
544
+ * use {@linkcode PositiveExpectTypeOf.branded `.branded.toEqualTypeOf()`}.
545
+ * @see {@link https://github.com/mmkal/expect-type#why-is-my-assertion-failing The documentation for details}.
546
+ *
547
+ * @example
548
+ * <caption>Using generic type argument syntax</caption>
549
+ * ```ts
550
+ * expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>()
551
+ *
552
+ * expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
553
+ * ```
554
+ *
555
+ * @example
556
+ * <caption>Using inferred type syntax by passing a value</caption>
557
+ * ```ts
558
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 })
559
+ *
560
+ * expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 })
561
+ * ```
562
+ *
563
+ * @param MISMATCH - The mismatch arguments.
564
+ * @returns `true`.
565
+ */
232
566
  <Expected>(...MISMATCH: MismatchArgs<StrictEqualUsingTSInternalIdenticalToOperator<Actual, Expected>, false>): true;
233
567
  };
234
568
  toMatchTypeOf: {
569
+ /**
570
+ * A less strict version of
571
+ * {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`}
572
+ * that allows for extra properties.
573
+ * This is roughly equivalent to an `extends` constraint
574
+ * in a function type argument.
575
+ *
576
+ * @example
577
+ * <caption>Using generic type argument syntax</caption>
578
+ * ```ts
579
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf<{ a: number }>()
580
+ * ```
581
+ *
582
+ * @example
583
+ * <caption>Using inferred type syntax by passing a value</caption>
584
+ * ```ts
585
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 2 })
586
+ * ```
587
+ *
588
+ * @param value - The value to compare against the expected type.
589
+ * @param MISMATCH - The mismatch arguments.
590
+ * @returns `true`.
591
+ */
235
592
  <Expected>(value: Expected & AValue, // reason for `& AValue`: make sure this is only the selected overload when the end-user passes a value for an inferred typearg. The `Mismatch` type does match `AValue`.
236
593
  ...MISMATCH: MismatchArgs<Extends<Actual, Expected>, false>): true;
594
+ /**
595
+ * A less strict version of
596
+ * {@linkcode PositiveExpectTypeOf.toEqualTypeOf `.toEqualTypeOf()`}
597
+ * that allows for extra properties.
598
+ * This is roughly equivalent to an `extends` constraint
599
+ * in a function type argument.
600
+ *
601
+ * @example
602
+ * <caption>Using generic type argument syntax</caption>
603
+ * ```ts
604
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf<{ a: number }>()
605
+ * ```
606
+ *
607
+ * @example
608
+ * <caption>Using inferred type syntax by passing a value</caption>
609
+ * ```ts
610
+ * expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 2 })
611
+ * ```
612
+ *
613
+ * @param MISMATCH - The mismatch arguments.
614
+ * @returns `true`.
615
+ */
237
616
  <Expected>(...MISMATCH: MismatchArgs<Extends<Actual, Expected>, false>): true;
238
617
  };
239
- toHaveProperty: <K extends string | number | symbol>(key: K, ...MISMATCH: MismatchArgs<Extends<K, keyof Actual>, false>) => true;
618
+ /**
619
+ * Checks whether an object has a given property.
620
+ *
621
+ * @example
622
+ * <caption>check that properties exist</caption>
623
+ * ```ts
624
+ * const obj = {a: 1, b: ''}
625
+ *
626
+ * expectTypeOf(obj).toHaveProperty('a')
627
+ *
628
+ * expectTypeOf(obj).not.toHaveProperty('c')
629
+ * ```
630
+ *
631
+ * @param key - The property key to check for.
632
+ * @param MISMATCH - The mismatch arguments.
633
+ * @returns `true`.
634
+ */
635
+ toHaveProperty: <KeyType extends string | number | symbol>(key: KeyType, ...MISMATCH: MismatchArgs<Extends<KeyType, keyof Actual>, false>) => true;
240
636
  }
637
+ /**
638
+ * Represents a conditional type that selects either
639
+ * {@linkcode PositiveExpectTypeOf} or {@linkcode NegativeExpectTypeOf} based
640
+ * on the value of the `positive` property in the {@linkcode Options} type.
641
+ */
241
642
  declare type ExpectTypeOf<Actual, Options extends {
242
643
  positive: boolean;
243
644
  }> = Options['positive'] extends true ? PositiveExpectTypeOf<Actual> : NegativeExpectTypeOf<Actual>;
645
+ /**
646
+ * Represents the base interface for the
647
+ * {@linkcode expectTypeOf()} function.
648
+ * Provides a set of assertion methods to perform type checks on a value.
649
+ */
244
650
  interface BaseExpectTypeOf<Actual, Options extends {
245
651
  positive: boolean;
246
652
  }> {
653
+ /**
654
+ * Checks whether the type of the value is `any`.
655
+ */
247
656
  toBeAny: Scolder<ExpectAny<Actual>, Options>;
657
+ /**
658
+ * Checks whether the type of the value is `unknown`.
659
+ */
248
660
  toBeUnknown: Scolder<ExpectUnknown<Actual>, Options>;
661
+ /**
662
+ * Checks whether the type of the value is `never`.
663
+ */
249
664
  toBeNever: Scolder<ExpectNever<Actual>, Options>;
665
+ /**
666
+ * Checks whether the type of the value is `function`.
667
+ */
250
668
  toBeFunction: Scolder<ExpectFunction<Actual>, Options>;
669
+ /**
670
+ * Checks whether the type of the value is `object`.
671
+ */
251
672
  toBeObject: Scolder<ExpectObject<Actual>, Options>;
673
+ /**
674
+ * Checks whether the type of the value is an {@linkcode Array}.
675
+ */
252
676
  toBeArray: Scolder<ExpectArray<Actual>, Options>;
677
+ /**
678
+ * Checks whether the type of the value is `number`.
679
+ */
253
680
  toBeNumber: Scolder<ExpectNumber<Actual>, Options>;
681
+ /**
682
+ * Checks whether the type of the value is `string`.
683
+ */
254
684
  toBeString: Scolder<ExpectString<Actual>, Options>;
685
+ /**
686
+ * Checks whether the type of the value is `boolean`.
687
+ */
255
688
  toBeBoolean: Scolder<ExpectBoolean<Actual>, Options>;
689
+ /**
690
+ * Checks whether the type of the value is `void`.
691
+ */
256
692
  toBeVoid: Scolder<ExpectVoid<Actual>, Options>;
693
+ /**
694
+ * Checks whether the type of the value is `symbol`.
695
+ */
257
696
  toBeSymbol: Scolder<ExpectSymbol<Actual>, Options>;
697
+ /**
698
+ * Checks whether the type of the value is `null`.
699
+ */
258
700
  toBeNull: Scolder<ExpectNull<Actual>, Options>;
701
+ /**
702
+ * Checks whether the type of the value is `undefined`.
703
+ */
259
704
  toBeUndefined: Scolder<ExpectUndefined<Actual>, Options>;
705
+ /**
706
+ * Checks whether the type of the value is `null` or `undefined`.
707
+ */
260
708
  toBeNullable: Scolder<ExpectNullable<Actual>, Options>;
709
+ /**
710
+ * Checks whether a function is callable with the given parameters.
711
+ *
712
+ * __Note__: You cannot negate this assertion with
713
+ * {@linkcode PositiveExpectTypeOf.not `.not`} you need to use
714
+ * `ts-expect-error` instead.
715
+ *
716
+ * @example
717
+ * ```ts
718
+ * const f = (a: number) => [a, a]
719
+ *
720
+ * expectTypeOf(f).toBeCallableWith(1)
721
+ * ```
722
+ *
723
+ * __Known Limitation__: This assertion will likely fail if you try to use it
724
+ * with a generic function or an overload.
725
+ * @see {@link https://github.com/mmkal/expect-type/issues/50 This issue} for an example and a workaround.
726
+ *
727
+ * @param args - The arguments to check for callability.
728
+ * @returns `true`.
729
+ */
261
730
  toBeCallableWith: Options['positive'] extends true ? (...args: Params<Actual>) => true : never;
731
+ /**
732
+ * Checks whether a class is constructible with the given parameters.
733
+ *
734
+ * @example
735
+ * ```ts
736
+ * expectTypeOf(Date).toBeConstructibleWith('1970')
737
+ *
738
+ * expectTypeOf(Date).toBeConstructibleWith(0)
739
+ *
740
+ * expectTypeOf(Date).toBeConstructibleWith(new Date())
741
+ *
742
+ * expectTypeOf(Date).toBeConstructibleWith()
743
+ * ```
744
+ *
745
+ * @param args - The arguments to check for constructibility.
746
+ * @returns `true`.
747
+ */
262
748
  toBeConstructibleWith: Options['positive'] extends true ? (...args: ConstructorParams<Actual>) => true : never;
749
+ /**
750
+ * Equivalent to the {@linkcode Extract} utility type.
751
+ * Helps narrow down complex union types.
752
+ *
753
+ * @example
754
+ * ```ts
755
+ * type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T }
756
+ *
757
+ * interface CSSProperties {
758
+ * margin?: string
759
+ * padding?: string
760
+ * }
761
+ *
762
+ * function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
763
+ * return {}
764
+ * }
765
+ *
766
+ * const cssProperties: CSSProperties = { margin: '1px', padding: '2px' }
767
+ *
768
+ * expectTypeOf(getResponsiveProp(cssProperties))
769
+ * .extract<{ xs?: any }>() // extracts the last type from a union
770
+ * .toEqualTypeOf<{
771
+ * xs?: CSSProperties
772
+ * sm?: CSSProperties
773
+ * md?: CSSProperties
774
+ * }>()
775
+ *
776
+ * expectTypeOf(getResponsiveProp(cssProperties))
777
+ * .extract<unknown[]>() // extracts an array from a union
778
+ * .toEqualTypeOf<CSSProperties[]>()
779
+ * ```
780
+ *
781
+ * __Note__: If no type is found in the union, it will return `never`.
782
+ *
783
+ * @param v - The type to extract from the union.
784
+ * @returns The type after extracting the type from the union.
785
+ */
263
786
  extract: <V>(v?: V) => ExpectTypeOf<Extract<Actual, V>, Options>;
787
+ /**
788
+ * Equivalent to the {@linkcode Exclude} utility type.
789
+ * Removes types from a union.
790
+ *
791
+ * @example
792
+ * ```ts
793
+ * type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T }
794
+ *
795
+ * interface CSSProperties {
796
+ * margin?: string
797
+ * padding?: string
798
+ * }
799
+ *
800
+ * function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
801
+ * return {}
802
+ * }
803
+ *
804
+ * const cssProperties: CSSProperties = { margin: '1px', padding: '2px' }
805
+ *
806
+ * expectTypeOf(getResponsiveProp(cssProperties))
807
+ * .exclude<unknown[]>()
808
+ * .exclude<{ xs?: unknown }>() // or just `.exclude<unknown[] | { xs?: unknown }>()`
809
+ * .toEqualTypeOf<CSSProperties>()
810
+ * ```
811
+ */
264
812
  exclude: <V>(v?: V) => ExpectTypeOf<Exclude<Actual, V>, Options>;
265
- parameter: <K extends keyof Params<Actual>>(number: K) => ExpectTypeOf<Params<Actual>[K], Options>;
813
+ /**
814
+ * Equivalent to the {@linkcode Pick} utility type.
815
+ * Helps select a subset of properties from an object type.
816
+ *
817
+ * @example
818
+ * ```ts
819
+ * interface Person {
820
+ * name: string
821
+ * age: number
822
+ * }
823
+ *
824
+ * expectTypeOf<Person>()
825
+ * .pick<'name'>()
826
+ * .toEqualTypeOf<{ name: string }>()
827
+ * ```
828
+ *
829
+ * @param keyToPick - The property key to pick.
830
+ * @returns The type after picking the property.
831
+ */
832
+ pick: <KeyToPick extends keyof Actual>(keyToPick?: KeyToPick) => ExpectTypeOf<Pick<Actual, KeyToPick>, Options>;
833
+ /**
834
+ * Equivalent to the {@linkcode Omit} utility type.
835
+ * Helps remove a subset of properties from an object type.
836
+ *
837
+ * @example
838
+ * ```ts
839
+ * interface Person {
840
+ * name: string
841
+ * age: number
842
+ * }
843
+ *
844
+ * expectTypeOf<Person>().omit<'name'>().toEqualTypeOf<{ age: number }>()
845
+ * ```
846
+ *
847
+ * @param keyToOmit - The property key to omit.
848
+ * @returns The type after omitting the property.
849
+ */
850
+ omit: <KeyToOmit extends keyof Actual | (PropertyKey & Record<never, never>)>(keyToOmit?: KeyToOmit) => ExpectTypeOf<Omit<Actual, KeyToOmit>, Options>;
851
+ /**
852
+ * Extracts a certain function argument with `.parameter(number)` call to
853
+ * perform other assertions on it.
854
+ *
855
+ * @example
856
+ * ```ts
857
+ * function foo(a: number, b: string) {
858
+ * return [a, b]
859
+ * }
860
+ *
861
+ * expectTypeOf(foo).parameter(0).toBeNumber()
862
+ *
863
+ * expectTypeOf(foo).parameter(1).toBeString()
864
+ * ```
865
+ *
866
+ * @param index - The index of the parameter to extract.
867
+ * @returns The extracted parameter type.
868
+ */
869
+ parameter: <Index extends keyof Params<Actual>>(index: Index) => ExpectTypeOf<Params<Actual>[Index], Options>;
870
+ /**
871
+ * Equivalent to the {@linkcode Parameters} utility type.
872
+ * Extracts function parameters to perform assertions on its value.
873
+ * Parameters are returned as an array.
874
+ *
875
+ * @example
876
+ * ```ts
877
+ * function noParam() {}
878
+ *
879
+ * function hasParam(s: string) {}
880
+ *
881
+ * expectTypeOf(noParam).parameters.toEqualTypeOf<[]>()
882
+ *
883
+ * expectTypeOf(hasParam).parameters.toEqualTypeOf<[string]>()
884
+ * ```
885
+ */
266
886
  parameters: ExpectTypeOf<Params<Actual>, Options>;
887
+ /**
888
+ * Equivalent to the {@linkcode ConstructorParameters} utility type.
889
+ * Extracts constructor parameters as an array of values and
890
+ * perform assertions on them with this method.
891
+ *
892
+ * @example
893
+ * ```ts
894
+ * expectTypeOf(Date).constructorParameters.toEqualTypeOf<
895
+ * [] | [string | number | Date]
896
+ * >()
897
+ * ```
898
+ */
267
899
  constructorParameters: ExpectTypeOf<ConstructorParams<Actual>, Options>;
900
+ /**
901
+ * Equivalent to the {@linkcode ThisParameterType} utility type.
902
+ * Extracts the `this` parameter of a function to
903
+ * perform assertions on its value.
904
+ *
905
+ * @example
906
+ * ```ts
907
+ * function greet(this: { name: string }, message: string) {
908
+ * return `Hello ${this.name}, here's your message: ${message}`
909
+ * }
910
+ *
911
+ * expectTypeOf(greet).thisParameter.toEqualTypeOf<{ name: string }>()
912
+ * ```
913
+ */
268
914
  thisParameter: ExpectTypeOf<ThisParameterType<Actual>, Options>;
915
+ /**
916
+ * Equivalent to the {@linkcode InstanceType} utility type.
917
+ * Extracts the instance type of a class to perform assertions on.
918
+ *
919
+ * @example
920
+ * ```ts
921
+ * expectTypeOf(Date).instance.toHaveProperty('toISOString')
922
+ * ```
923
+ */
269
924
  instance: Actual extends new (...args: any[]) => infer I ? ExpectTypeOf<I, Options> : never;
925
+ /**
926
+ * Equivalent to the {@linkcode ReturnType} utility type.
927
+ * Extracts the return type of a function.
928
+ *
929
+ * @example
930
+ * ```ts
931
+ * expectTypeOf(() => {}).returns.toBeVoid()
932
+ *
933
+ * expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2])
934
+ * ```
935
+ */
270
936
  returns: Actual extends (...args: any[]) => infer R ? ExpectTypeOf<R, Options> : never;
271
- resolves: Actual extends PromiseLike<infer R> ? ExpectTypeOf<R, Options> : never;
272
- items: Actual extends ArrayLike<infer R> ? ExpectTypeOf<R, Options> : never;
937
+ /**
938
+ * Extracts resolved value of a Promise,
939
+ * so you can perform other assertions on it.
940
+ *
941
+ * @example
942
+ * ```ts
943
+ * async function asyncFunc() {
944
+ * return 123
945
+ * }
946
+ *
947
+ * expectTypeOf(asyncFunc).returns.resolves.toBeNumber()
948
+ *
949
+ * expectTypeOf(Promise.resolve('string')).resolves.toBeString()
950
+ * ```
951
+ *
952
+ * Type Equivalent:
953
+ * ```ts
954
+ * type Resolves<PromiseType> = PromiseType extends PromiseLike<infer ResolvedType>
955
+ * ? ResolvedType
956
+ * : never
957
+ * ```
958
+ */
959
+ resolves: Actual extends PromiseLike<infer ResolvedType> ? ExpectTypeOf<ResolvedType, Options> : never;
960
+ /**
961
+ * Extracts array item type to perform assertions on.
962
+ *
963
+ * @example
964
+ * ```ts
965
+ * expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>()
966
+ *
967
+ * expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>()
968
+ * ```
969
+ *
970
+ * __Type Equivalent__:
971
+ * ```ts
972
+ * type Items<ArrayType> = ArrayType extends ArrayLike<infer ItemType>
973
+ * ? ItemType
974
+ * : never
975
+ * ```
976
+ */
977
+ items: Actual extends ArrayLike<infer ItemType> ? ExpectTypeOf<ItemType, Options> : never;
978
+ /**
979
+ * Extracts the type guarded by a function to perform assertions on.
980
+ *
981
+ * @example
982
+ * ```ts
983
+ * function isString(v: any): v is string {
984
+ * return typeof v === 'string'
985
+ * }
986
+ *
987
+ * expectTypeOf(isString).guards.toBeString()
988
+ * ```
989
+ */
273
990
  guards: Actual extends (v: any, ...args: any[]) => v is infer T ? ExpectTypeOf<T, Options> : never;
991
+ /**
992
+ * Extracts the type asserted by a function to perform assertions on.
993
+ *
994
+ * @example
995
+ * ```ts
996
+ * function assertNumber(v: any): asserts v is number {
997
+ * if (typeof v !== 'number')
998
+ * throw new TypeError('Nope !')
999
+ * }
1000
+ *
1001
+ * expectTypeOf(assertNumber).asserts.toBeNumber()
1002
+ * ```
1003
+ */
274
1004
  asserts: Actual extends (v: any, ...args: any[]) => asserts v is infer T ? unknown extends T ? never : ExpectTypeOf<T, Options> : never;
275
1005
  }
1006
+ /**
1007
+ * Represents a function that allows asserting the expected type of a value.
1008
+ */
276
1009
  declare type _ExpectTypeOf = {
1010
+ /**
1011
+ * Asserts the expected type of a value.
1012
+ *
1013
+ * @param actual - The actual value being asserted.
1014
+ * @returns An object representing the expected type assertion.
1015
+ */
277
1016
  <Actual>(actual: Actual): ExpectTypeOf<Actual, {
278
1017
  positive: true;
279
1018
  branded: false;
280
1019
  }>;
1020
+ /**
1021
+ * Asserts the expected type of a value without providing an actual value.
1022
+ *
1023
+ * @returns An object representing the expected type assertion.
1024
+ */
281
1025
  <Actual>(): ExpectTypeOf<Actual, {
282
1026
  positive: true;
283
1027
  branded: false;
@@ -519,14 +1263,16 @@ interface VitestUtils {
519
1263
  * @param path Path to the module. Can be aliased, if your Vitest config supports it
520
1264
  * @param factory Mocked module factory. The result of this function will be an exports object
521
1265
  */
522
- mock: (path: string, factory?: MockFactoryWithHelper) => void;
1266
+ mock(path: string, factory?: MockFactoryWithHelper): void;
1267
+ mock<T>(module: Promise<T>, factory?: MockFactoryWithHelper<T>): void;
523
1268
  /**
524
1269
  * Removes module from mocked registry. All calls to import will return the original module even if it was mocked before.
525
1270
  *
526
1271
  * This call is hoisted to the top of the file, so it will only unmock modules that were defined in `setupFiles`, for example.
527
1272
  * @param path Path to the module. Can be aliased, if your Vitest config supports it
528
1273
  */
529
- unmock: (path: string) => void;
1274
+ unmock(path: string): void;
1275
+ unmock(module: Promise<unknown>): void;
530
1276
  /**
531
1277
  * Mocks every subsequent [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) call.
532
1278
  *
@@ -536,14 +1282,16 @@ interface VitestUtils {
536
1282
  * @param path Path to the module. Can be aliased, if your Vitest config supports it
537
1283
  * @param factory Mocked module factory. The result of this function will be an exports object
538
1284
  */
539
- doMock: (path: string, factory?: MockFactoryWithHelper) => void;
1285
+ doMock(path: string, factory?: MockFactoryWithHelper): void;
1286
+ doMock<T>(module: Promise<T>, factory?: MockFactoryWithHelper<T>): void;
540
1287
  /**
541
1288
  * Removes module from mocked registry. All subsequent calls to import will return original module.
542
1289
  *
543
1290
  * Unlike [`vi.unmock`](https://vitest.dev/api/vi#vi-unmock), this method is not hoisted to the top of the file.
544
1291
  * @param path Path to the module. Can be aliased, if your Vitest config supports it
545
1292
  */
546
- doUnmock: (path: string) => void;
1293
+ doUnmock(path: string): void;
1294
+ doUnmock(module: Promise<unknown>): void;
547
1295
  /**
548
1296
  * Imports module, bypassing all checks if it should be mocked.
549
1297
  * Can be useful if you want to mock module partially.