版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

197 строки
8.2 KiB

  1. import { Awaitable } from '@vitest/utils';
  2. type ChainableFunction<T extends string, Args extends any[], R = any, E = {}> = {
  3. (...args: Args): R;
  4. } & {
  5. [x in T]: ChainableFunction<T, Args, R, E>;
  6. } & {
  7. fn: (this: Record<T, boolean | undefined>, ...args: Args) => R;
  8. } & E;
  9. declare function createChainable<T extends string, Args extends any[], R = any, E = {}>(keys: T[], fn: (this: Record<T, boolean | undefined>, ...args: Args) => R): ChainableFunction<T, Args, R, E>;
  10. interface ParsedStack {
  11. method: string;
  12. file: string;
  13. line: number;
  14. column: number;
  15. }
  16. interface ErrorWithDiff extends Error {
  17. name: string;
  18. nameStr?: string;
  19. stack?: string;
  20. stackStr?: string;
  21. stacks?: ParsedStack[];
  22. showDiff?: boolean;
  23. actual?: any;
  24. expected?: any;
  25. operator?: string;
  26. type?: string;
  27. frame?: string;
  28. }
  29. declare function serializeError(val: any, seen?: WeakMap<object, any>): any;
  30. interface ProcessErrorOptions {
  31. outputDiffMaxSize?: number;
  32. }
  33. declare function processError(err: any, options?: ProcessErrorOptions): any;
  34. declare function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced?: WeakSet<object>, expectedReplaced?: WeakSet<object>): {
  35. replacedActual: any;
  36. replacedExpected: any;
  37. };
  38. type RunMode = 'run' | 'skip' | 'only' | 'todo';
  39. type TaskState = RunMode | 'pass' | 'fail';
  40. interface TaskBase {
  41. id: string;
  42. name: string;
  43. mode: RunMode;
  44. concurrent?: boolean;
  45. shuffle?: boolean;
  46. suite?: Suite;
  47. file?: File;
  48. result?: TaskResult;
  49. retry?: number;
  50. meta?: any;
  51. }
  52. interface TaskCustom extends TaskBase {
  53. type: 'custom';
  54. }
  55. interface TaskResult {
  56. state: TaskState;
  57. duration?: number;
  58. startTime?: number;
  59. heap?: number;
  60. /**
  61. * @deprecated Use "errors" instead
  62. */
  63. error?: ErrorWithDiff;
  64. errors?: ErrorWithDiff[];
  65. htmlError?: string;
  66. hooks?: Partial<Record<keyof SuiteHooks, TaskState>>;
  67. retryCount?: number;
  68. }
  69. type TaskResultPack = [id: string, result: TaskResult | undefined];
  70. interface Suite extends TaskBase {
  71. type: 'suite';
  72. tasks: Task[];
  73. filepath?: string;
  74. projectName?: string;
  75. }
  76. interface File extends Suite {
  77. filepath: string;
  78. collectDuration?: number;
  79. setupDuration?: number;
  80. }
  81. interface Test<ExtraContext = {}> extends TaskBase {
  82. type: 'test';
  83. suite: Suite;
  84. result?: TaskResult;
  85. fails?: boolean;
  86. context: TestContext & ExtraContext;
  87. onFailed?: OnTestFailedHandler[];
  88. }
  89. type Task = Test | Suite | TaskCustom | File;
  90. type DoneCallback = (error?: any) => void;
  91. type TestFunction<ExtraContext = {}> = (context: TestContext & ExtraContext) => Awaitable<any> | void;
  92. type ExtractEachCallbackArgs<T extends ReadonlyArray<any>> = {
  93. 1: [T[0]];
  94. 2: [T[0], T[1]];
  95. 3: [T[0], T[1], T[2]];
  96. 4: [T[0], T[1], T[2], T[3]];
  97. 5: [T[0], T[1], T[2], T[3], T[4]];
  98. 6: [T[0], T[1], T[2], T[3], T[4], T[5]];
  99. 7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6]];
  100. 8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7]];
  101. 9: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8]];
  102. 10: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9]];
  103. fallback: Array<T extends ReadonlyArray<infer U> ? U : any>;
  104. }[T extends Readonly<[any]> ? 1 : T extends Readonly<[any, any]> ? 2 : T extends Readonly<[any, any, any]> ? 3 : T extends Readonly<[any, any, any, any]> ? 4 : T extends Readonly<[any, any, any, any, any]> ? 5 : T extends Readonly<[any, any, any, any, any, any]> ? 6 : T extends Readonly<[any, any, any, any, any, any, any]> ? 7 : T extends Readonly<[any, any, any, any, any, any, any, any]> ? 8 : T extends Readonly<[any, any, any, any, any, any, any, any, any]> ? 9 : T extends Readonly<[any, any, any, any, any, any, any, any, any, any]> ? 10 : 'fallback'];
  105. interface SuiteEachFunction {
  106. <T extends any[] | [any]>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T) => Awaitable<void>) => void;
  107. <T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (name: string, fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>) => void;
  108. <T>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => Awaitable<void>) => void;
  109. }
  110. interface TestEachFunction {
  111. <T extends any[] | [any]>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T) => Awaitable<void>, options?: number | TestOptions) => void;
  112. <T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (name: string, fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>, options?: number | TestOptions) => void;
  113. <T>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => Awaitable<void>, options?: number | TestOptions) => void;
  114. (...args: [TemplateStringsArray, ...any]): (name: string, fn: (...args: any[]) => Awaitable<void>, options?: number | TestOptions) => void;
  115. }
  116. type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<'concurrent' | 'only' | 'skip' | 'todo' | 'fails', [
  117. name: string,
  118. fn?: TestFunction<ExtraContext>,
  119. options?: number | TestOptions
  120. ], void, {
  121. each: TestEachFunction;
  122. <T extends ExtraContext>(name: string, fn?: TestFunction<T>, options?: number | TestOptions): void;
  123. }>;
  124. interface TestOptions {
  125. /**
  126. * Test timeout.
  127. */
  128. timeout?: number;
  129. /**
  130. * Times to retry the test if fails. Useful for making flaky tests more stable.
  131. * When retries is up, the last test error will be thrown.
  132. *
  133. * @default 1
  134. */
  135. retry?: number;
  136. }
  137. type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {
  138. each: TestEachFunction;
  139. skipIf(condition: any): ChainableTestAPI<ExtraContext>;
  140. runIf(condition: any): ChainableTestAPI<ExtraContext>;
  141. };
  142. type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle', [
  143. name: string,
  144. factory?: SuiteFactory<ExtraContext>,
  145. options?: number | TestOptions
  146. ], SuiteCollector<ExtraContext>, {
  147. each: TestEachFunction;
  148. <T extends ExtraContext>(name: string, factory?: SuiteFactory<T>): SuiteCollector<T>;
  149. }>;
  150. type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI<ExtraContext> & {
  151. each: SuiteEachFunction;
  152. skipIf(condition: any): ChainableSuiteAPI<ExtraContext>;
  153. runIf(condition: any): ChainableSuiteAPI<ExtraContext>;
  154. };
  155. type HookListener<T extends any[], Return = void> = (...args: T) => Awaitable<Return>;
  156. type HookCleanupCallback = (() => Awaitable<unknown>) | void;
  157. interface SuiteHooks<ExtraContext = {}> {
  158. beforeAll: HookListener<[Suite | File], HookCleanupCallback>[];
  159. afterAll: HookListener<[Suite | File]>[];
  160. beforeEach: HookListener<[TestContext & ExtraContext, Suite], HookCleanupCallback>[];
  161. afterEach: HookListener<[TestContext & ExtraContext, Suite]>[];
  162. }
  163. interface SuiteCollector<ExtraContext = {}> {
  164. readonly name: string;
  165. readonly mode: RunMode;
  166. type: 'collector';
  167. test: TestAPI<ExtraContext>;
  168. tasks: (Suite | TaskCustom | Test | SuiteCollector<ExtraContext>)[];
  169. custom: (name: string) => TaskCustom;
  170. collect: (file?: File) => Promise<Suite>;
  171. clear: () => void;
  172. on: <T extends keyof SuiteHooks<ExtraContext>>(name: T, ...fn: SuiteHooks<ExtraContext>[T]) => void;
  173. }
  174. type SuiteFactory<ExtraContext = {}> = (test: (name: string, fn: TestFunction<ExtraContext>) => void) => Awaitable<void>;
  175. interface RuntimeContext {
  176. tasks: (SuiteCollector | Test)[];
  177. currentSuite: SuiteCollector | null;
  178. }
  179. interface TestContext {
  180. /**
  181. * Metadata of the current test
  182. */
  183. meta: Readonly<Test>;
  184. /**
  185. * Extract hooks on test failed
  186. */
  187. onTestFailed: (fn: OnTestFailedHandler) => void;
  188. }
  189. type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>;
  190. type SequenceHooks = 'stack' | 'list' | 'parallel';
  191. type SequenceSetupFiles = 'list' | 'parallel';
  192. export { ChainableFunction as C, DoneCallback as D, ErrorWithDiff as E, File as F, HookListener as H, OnTestFailedHandler as O, ParsedStack as P, RunMode as R, SuiteAPI as S, Task as T, TestAPI as a, SuiteCollector as b, SuiteHooks as c, TestContext as d, Suite as e, HookCleanupCallback as f, Test as g, TaskState as h, TaskBase as i, TaskCustom as j, TaskResult as k, TaskResultPack as l, TestFunction as m, TestOptions as n, SuiteFactory as o, RuntimeContext as p, SequenceHooks as q, SequenceSetupFiles as r, createChainable as s, serializeError as t, processError as u, replaceAsymmetricMatcher as v };