index.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { s as spyOn, f as fn, M as MaybeMockedDeep, a as MaybeMocked, b as MaybePartiallyMocked, c as MaybePartiallyMockedDeep, E as EnhancedSpy } from './index-6e18a03a.js';
  2. export { E as EnhancedSpy, r as Mock, u as MockContext, q as MockInstance, v as Mocked, w as MockedClass, o as MockedFunction, p as MockedObject, S as SpyInstance, j as afterAll, l as afterEach, h as beforeAll, k as beforeEach, g as bench, n as createExpect, e as describe, m as expect, i as it, d as suite, t as test } from './index-6e18a03a.js';
  3. import { D as DoneCallback, F as FakeTimerInstallOpts, a as File, T as TaskResultPack, R as ResolvedConfig, M as ModuleGraphData, b as Reporter } from './global-e98f203b.js';
  4. export { L as AfterSuiteRunMeta, A as ApiConfig, Y as ArgumentsType, X as Arrayable, P as Awaitable, ae as BaseCoverageOptions, ak as BenchFunction, ai as Benchmark, al as BenchmarkAPI, aj as BenchmarkResult, ah as BenchmarkUserOptions, B as BuiltinEnvironment, C as CSSModuleScopeStrategy, a0 as Constructable, ag as CoverageC8Options, af as CoverageIstanbulOptions, ac as CoverageOptions, a9 as CoverageProvider, aa as CoverageProviderModule, ab as CoverageReporter, _ as DeepMerge, D as DoneCallback, a3 as Environment, E as EnvironmentOptions, a2 as EnvironmentReturn, a7 as ErrorWithDiff, a as File, n as HookCleanupCallback, H as HookListener, I as InlineConfig, J as JSDOMOptions, Z as MergeInsertions, a1 as ModuleCache, M as ModuleGraphData, $ as MutableArray, Q as Nullable, a8 as OnServerRestartHandler, a6 as ParsedStack, a5 as Position, b as Reporter, K as ResolveIdFunction, R as ResolvedConfig, ad as ResolvedCoverageOptions, d as RunMode, r as RuntimeContext, u as SnapshotData, x as SnapshotMatchOptions, y as SnapshotResult, w as SnapshotStateOptions, G as SnapshotSummary, v as SnapshotUpdateState, S as Suite, m as SuiteAPI, p as SuiteCollector, q as SuiteFactory, o as SuiteHooks, i as Task, f as TaskBase, g as TaskResult, T as TaskResultPack, e as TaskState, h as Test, l as TestAPI, s as TestContext, j as TestFunction, k as TestOptions, z as UncheckedSnapshot, U as UserConfig, a4 as UserConsoleLog, t as Vitest, V as VitestEnvironment, c as VitestRunMode, W as WorkerContext, O as WorkerGlobalState, N as WorkerRPC } from './global-e98f203b.js';
  5. import { TransformResult } from 'vite';
  6. import * as chai from 'chai';
  7. export { chai };
  8. export { assert, should } from 'chai';
  9. export { Bench as BenchFactory, Options as BenchOptions, Task as BenchTask, TaskResult as BenchTaskResult } from 'tinybench';
  10. import 'tinyspy';
  11. import 'fs';
  12. import 'worker_threads';
  13. /**
  14. * A simple wrapper for converting callback style to promise
  15. */
  16. declare function withCallback(fn: (done: DoneCallback) => void): Promise<void>;
  17. /**
  18. * This utils allows computational intensive tasks to only be ran once
  19. * across test reruns to improve the watch mode performance.
  20. *
  21. * Currently only works with `isolate: false`
  22. *
  23. * @experimental
  24. */
  25. declare function runOnce<T>(fn: (() => T), key?: string): T;
  26. /**
  27. * Get a boolean indicates whether the task is running in the first time.
  28. * Could only be `false` in watch mode.
  29. *
  30. * Currently only works with `isolate: false`
  31. *
  32. * @experimental
  33. */
  34. declare function isFirstRun(): boolean;
  35. declare class VitestUtils {
  36. private _timers;
  37. private _mockedDate;
  38. private _mocker;
  39. constructor();
  40. useFakeTimers(config?: FakeTimerInstallOpts): this;
  41. useRealTimers(): this;
  42. runOnlyPendingTimers(): this;
  43. runAllTimers(): this;
  44. runAllTicks(): this;
  45. advanceTimersByTime(ms: number): this;
  46. advanceTimersToNextTimer(): this;
  47. getTimerCount(): number;
  48. setSystemTime(time: number | string | Date): this;
  49. getMockedSystemTime(): string | number | Date | null;
  50. getRealSystemTime(): number;
  51. clearAllTimers(): this;
  52. spyOn: typeof spyOn;
  53. fn: typeof fn;
  54. private getImporter;
  55. /**
  56. * Makes all `imports` to passed module to be mocked.
  57. * - If there is a factory, will return it's result. The call to `vi.mock` is hoisted to the top of the file,
  58. * so you don't have access to variables declared in the global file scope, if you didn't put them before imports!
  59. * - If `__mocks__` folder with file of the same name exist, all imports will
  60. * return it.
  61. * - If there is no `__mocks__` folder or a file with the same name inside, will call original
  62. * module and mock it.
  63. * @param path Path to the module. Can be aliased, if your config supports it
  64. * @param factory Factory for the mocked module. Has the highest priority.
  65. */
  66. mock(path: string, factory?: () => any): void;
  67. /**
  68. * Removes module from mocked registry. All subsequent calls to import will
  69. * return original module even if it was mocked.
  70. * @param path Path to the module. Can be aliased, if your config supports it
  71. */
  72. unmock(path: string): void;
  73. doMock(path: string, factory?: () => any): void;
  74. doUnmock(path: string): void;
  75. /**
  76. * Imports module, bypassing all checks if it should be mocked.
  77. * Can be useful if you want to mock module partially.
  78. * @example
  79. * vi.mock('./example', async () => {
  80. * const axios = await vi.importActual('./example')
  81. *
  82. * return { ...axios, get: vi.fn() }
  83. * })
  84. * @param path Path to the module. Can be aliased, if your config supports it
  85. * @returns Actual module without spies
  86. */
  87. importActual<T>(path: string): Promise<T>;
  88. /**
  89. * Imports a module with all of its properties and nested properties mocked.
  90. * For the rules applied, see docs.
  91. * @param path Path to the module. Can be aliased, if your config supports it
  92. * @returns Fully mocked module
  93. */
  94. importMock<T>(path: string): Promise<MaybeMockedDeep<T>>;
  95. /**
  96. * Type helpers for TypeScript. In reality just returns the object that was passed.
  97. *
  98. * When `partial` is `true` it will expect a `Partial<T>` as a return value.
  99. * @example
  100. * import example from './example'
  101. * vi.mock('./example')
  102. *
  103. * test('1+1 equals 2' async () => {
  104. * vi.mocked(example.calc).mockRestore()
  105. *
  106. * const res = example.calc(1, '+', 1)
  107. *
  108. * expect(res).toBe(2)
  109. * })
  110. * @param item Anything that can be mocked
  111. * @param deep If the object is deeply mocked
  112. * @param options If the object is partially or deeply mocked
  113. */
  114. mocked<T>(item: T, deep?: false): MaybeMocked<T>;
  115. mocked<T>(item: T, deep: true): MaybeMockedDeep<T>;
  116. mocked<T>(item: T, options: {
  117. partial?: false;
  118. deep?: false;
  119. }): MaybeMocked<T>;
  120. mocked<T>(item: T, options: {
  121. partial?: false;
  122. deep: true;
  123. }): MaybeMockedDeep<T>;
  124. mocked<T>(item: T, options: {
  125. partial: true;
  126. deep?: false;
  127. }): MaybePartiallyMocked<T>;
  128. mocked<T>(item: T, options: {
  129. partial: true;
  130. deep: true;
  131. }): MaybePartiallyMockedDeep<T>;
  132. isMockFunction(fn: any): fn is EnhancedSpy;
  133. clearAllMocks(): this;
  134. resetAllMocks(): this;
  135. restoreAllMocks(): this;
  136. /**
  137. * Will put a value on global scope. Useful, if you are
  138. * using jsdom/happy-dom and want to mock global variables, like
  139. * `IntersectionObserver`.
  140. */
  141. stubGlobal(name: string | symbol | number, value: any): this;
  142. resetModules(): this;
  143. /**
  144. * Wait for all imports to load.
  145. * Useful, if you have a synchronous call that starts
  146. * importing a module, that you cannot wait otherwise.
  147. */
  148. dynamicImportSettled(): Promise<void>;
  149. }
  150. declare const vitest: VitestUtils;
  151. declare const vi: VitestUtils;
  152. declare function getRunningMode(): "run" | "watch";
  153. declare function isWatchMode(): boolean;
  154. interface TransformResultWithSource extends TransformResult {
  155. source?: string;
  156. }
  157. interface WebSocketHandlers {
  158. onWatcherStart: () => Promise<void>;
  159. onFinished(files?: File[]): Promise<void>;
  160. onCollected(files?: File[]): Promise<void>;
  161. onTaskUpdate(packs: TaskResultPack[]): void;
  162. getFiles(): File[];
  163. getPaths(): Promise<string[]>;
  164. getConfig(): ResolvedConfig;
  165. getModuleGraph(id: string): Promise<ModuleGraphData>;
  166. getTransformResult(id: string): Promise<TransformResultWithSource | undefined>;
  167. readFile(id: string): Promise<string>;
  168. writeFile(id: string, content: string): Promise<void>;
  169. rerun(files: string[]): Promise<void>;
  170. updateSnapshot(file?: File): Promise<void>;
  171. }
  172. interface WebSocketEvents extends Pick<Reporter, 'onCollected' | 'onFinished' | 'onTaskUpdate' | 'onUserConsoleLog' | 'onPathsCollected'> {
  173. }
  174. export { TransformResultWithSource, WebSocketEvents, WebSocketHandlers, getRunningMode, isFirstRun, isWatchMode, runOnce, vi, vitest, withCallback };