index-6e18a03a.d.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { SpyImpl } from 'tinyspy';
  2. import { m as SuiteAPI, l as TestAPI, al as BenchmarkAPI, o as SuiteHooks, H as HookListener, s as TestContext, S as Suite, n as HookCleanupCallback, h as Test } from './global-e98f203b.js';
  3. interface MockResultReturn<T> {
  4. type: 'return';
  5. value: T;
  6. }
  7. interface MockResultIncomplete {
  8. type: 'incomplete';
  9. value: undefined;
  10. }
  11. interface MockResultThrow {
  12. type: 'throw';
  13. value: any;
  14. }
  15. declare type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
  16. interface MockContext<TArgs, TReturns> {
  17. calls: TArgs[];
  18. instances: TReturns[];
  19. invocationCallOrder: number[];
  20. results: MockResult<TReturns>[];
  21. lastCall: TArgs | undefined;
  22. }
  23. declare type Procedure = (...args: any[]) => any;
  24. declare type Methods<T> = {
  25. [K in keyof T]: T[K] extends Procedure ? K : never;
  26. }[keyof T] & (string | symbol);
  27. declare type Properties<T> = {
  28. [K in keyof T]: T[K] extends Procedure ? never : K;
  29. }[keyof T] & (string | symbol);
  30. declare type Classes<T> = {
  31. [K in keyof T]: T[K] extends new (...args: any[]) => any ? K : never;
  32. }[keyof T] & (string | symbol);
  33. interface SpyInstance<TArgs extends any[] = any[], TReturns = any> {
  34. getMockName(): string;
  35. mockName(n: string): this;
  36. mock: MockContext<TArgs, TReturns>;
  37. mockClear(): this;
  38. mockReset(): this;
  39. mockRestore(): void;
  40. getMockImplementation(): ((...args: TArgs) => TReturns) | undefined;
  41. mockImplementation(fn: ((...args: TArgs) => TReturns) | (() => Promise<TReturns>)): this;
  42. mockImplementationOnce(fn: ((...args: TArgs) => TReturns) | (() => Promise<TReturns>)): this;
  43. mockReturnThis(): this;
  44. mockReturnValue(obj: TReturns): this;
  45. mockReturnValueOnce(obj: TReturns): this;
  46. mockResolvedValue(obj: Awaited<TReturns>): this;
  47. mockResolvedValueOnce(obj: Awaited<TReturns>): this;
  48. mockRejectedValue(obj: any): this;
  49. mockRejectedValueOnce(obj: any): this;
  50. }
  51. interface MockInstance<A extends any[] = any[], R = any> extends SpyInstance<A, R> {
  52. }
  53. interface Mock<TArgs extends any[] = any, TReturns = any> extends SpyInstance<TArgs, TReturns> {
  54. new (...args: TArgs): TReturns;
  55. (...args: TArgs): TReturns;
  56. }
  57. interface PartialMock<TArgs extends any[] = any, TReturns = any> extends SpyInstance<TArgs, Partial<TReturns>> {
  58. new (...args: TArgs): TReturns;
  59. (...args: TArgs): TReturns;
  60. }
  61. declare type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? Mock<ConstructorParameters<T>, R> : T;
  62. declare type MockedFunction<T extends Procedure> = Mock<Parameters<T>, ReturnType<T>> & {
  63. [K in keyof T]: T[K];
  64. };
  65. declare type PartiallyMockedFunction<T extends Procedure> = PartialMock<Parameters<T>, ReturnType<T>> & {
  66. [K in keyof T]: T[K];
  67. };
  68. declare type MockedFunctionDeep<T extends Procedure> = Mock<Parameters<T>, ReturnType<T>> & MockedObjectDeep<T>;
  69. declare type PartiallyMockedFunctionDeep<T extends Procedure> = PartialMock<Parameters<T>, ReturnType<T>> & MockedObjectDeep<T>;
  70. declare type MockedObject<T> = MaybeMockedConstructor<T> & {
  71. [K in Methods<T>]: T[K] extends Procedure ? MockedFunction<T[K]> : T[K];
  72. } & {
  73. [K in Properties<T>]: T[K];
  74. };
  75. declare type MockedObjectDeep<T> = MaybeMockedConstructor<T> & {
  76. [K in Methods<T>]: T[K] extends Procedure ? MockedFunctionDeep<T[K]> : T[K];
  77. } & {
  78. [K in Properties<T>]: MaybeMockedDeep<T[K]>;
  79. };
  80. declare type MaybeMockedDeep<T> = T extends Procedure ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T;
  81. declare type MaybePartiallyMockedDeep<T> = T extends Procedure ? PartiallyMockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T;
  82. declare type MaybeMocked<T> = T extends Procedure ? MockedFunction<T> : T extends object ? MockedObject<T> : T;
  83. declare type MaybePartiallyMocked<T> = T extends Procedure ? PartiallyMockedFunction<T> : T extends object ? MockedObject<T> : T;
  84. interface Constructable {
  85. new (...args: any[]): any;
  86. }
  87. declare type MockedClass<T extends Constructable> = MockInstance<T extends new (...args: infer P) => any ? P : never, InstanceType<T>> & {
  88. prototype: T extends {
  89. prototype: any;
  90. } ? Mocked<T['prototype']> : never;
  91. } & T;
  92. declare type Mocked<T> = {
  93. [P in keyof T]: T[P] extends (...args: infer Args) => infer Returns ? MockInstance<Args, Returns> : T[P] extends Constructable ? MockedClass<T[P]> : T[P];
  94. } & T;
  95. declare type EnhancedSpy<TArgs extends any[] = any[], TReturns = any> = SpyInstance<TArgs, TReturns> & SpyImpl<TArgs, TReturns>;
  96. declare function spyOn<T, S extends Properties<Required<T>>>(obj: T, methodName: S, accessType: 'get'): SpyInstance<[], T[S]>;
  97. declare function spyOn<T, G extends Properties<Required<T>>>(obj: T, methodName: G, accessType: 'set'): SpyInstance<[T[G]], void>;
  98. declare function spyOn<T, M extends (Methods<Required<T>> | Classes<Required<T>>)>(obj: T, methodName: M): Required<T>[M] extends (...args: infer A) => infer R | (new (...args: infer A) => infer R) ? SpyInstance<A, R> : never;
  99. declare function fn<TArgs extends any[] = any[], R = any>(): Mock<TArgs, R>;
  100. declare function fn<TArgs extends any[] = any[], R = any>(implementation: (...args: TArgs) => R): Mock<TArgs, R>;
  101. declare const suite: SuiteAPI<{}>;
  102. declare const test: TestAPI<{}>;
  103. declare const bench: BenchmarkAPI;
  104. declare const describe: SuiteAPI<{}>;
  105. declare const it: TestAPI<{}>;
  106. declare const beforeAll: (fn: SuiteHooks['beforeAll'][0], timeout?: number) => void;
  107. declare const afterAll: (fn: SuiteHooks['afterAll'][0], timeout?: number) => void;
  108. declare const beforeEach: <ExtraContext = {}>(fn: HookListener<[TestContext & ExtraContext, Suite], HookCleanupCallback>, timeout?: number) => void;
  109. declare const afterEach: <ExtraContext = {}>(fn: HookListener<[TestContext & ExtraContext, Suite], void>, timeout?: number) => void;
  110. declare function createExpect(test?: Test): Vi.ExpectStatic;
  111. declare const globalExpect: Vi.ExpectStatic;
  112. export { EnhancedSpy as E, MaybeMockedDeep as M, SpyInstance as S, MaybeMocked as a, MaybePartiallyMocked as b, MaybePartiallyMockedDeep as c, suite as d, describe as e, fn as f, bench as g, beforeAll as h, it as i, afterAll as j, beforeEach as k, afterEach as l, globalExpect as m, createExpect as n, MockedFunction as o, MockedObject as p, MockInstance as q, Mock as r, spyOn as s, test as t, MockContext as u, Mocked as v, MockedClass as w };