vitest 0.0.109 → 0.0.113

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.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { Formatter } from 'picocolors/types';
2
+ import { ViteDevServer } from 'vite';
2
3
  import { OptionsReceived } from 'pretty-format';
3
4
  import { MessagePort } from 'worker_threads';
4
- export { assert, default as chai, expect, should } from 'chai';
5
+ import { RawSourceMap } from 'source-map-js';
5
6
  export { Spy, SpyFn } from 'tinyspy';
7
+ export { assert, default as chai, should } from 'chai';
6
8
 
7
9
  declare const EXPECTED_COLOR: Formatter;
8
10
  declare const RECEIVED_COLOR: Formatter;
@@ -117,6 +119,7 @@ interface AsymmetricMatcherInterface {
117
119
  declare abstract class AsymmetricMatcher<T, State extends MatcherState = MatcherState> implements AsymmetricMatcherInterface {
118
120
  protected sample: T;
119
121
  protected inverse: boolean;
122
+ $$typeof: symbol;
120
123
  constructor(sample: T, inverse?: boolean);
121
124
  protected getMatcherContext(): State;
122
125
  abstract asymmetricMatch(other: unknown): boolean;
@@ -158,6 +161,124 @@ declare class StringMatching extends AsymmetricMatcher<RegExp> {
158
161
  getExpectedType(): string;
159
162
  }
160
163
 
164
+ declare class SnapshotManager {
165
+ config: ResolvedConfig;
166
+ summary: SnapshotSummary;
167
+ constructor(config: ResolvedConfig);
168
+ clear(): void;
169
+ add(result: SnapshotResult): void;
170
+ }
171
+
172
+ declare type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>;
173
+ interface WorkerPool {
174
+ runTests: RunWithFiles;
175
+ collectTests: RunWithFiles;
176
+ close: () => Promise<void>;
177
+ }
178
+
179
+ declare class StateManager {
180
+ filesMap: Record<string, File>;
181
+ idMap: Record<string, Task>;
182
+ taskFileMap: WeakMap<Task, File>;
183
+ getFiles(keys?: string[]): File[];
184
+ collectFiles(files: File[]): void;
185
+ updateId(task: Task): void;
186
+ updateTasks(packs: TaskResultPack[]): void;
187
+ }
188
+
189
+ declare class Vitest {
190
+ config: ResolvedConfig;
191
+ server: ViteDevServer;
192
+ state: StateManager;
193
+ snapshot: SnapshotManager;
194
+ reporters: Reporter[];
195
+ console: Console;
196
+ pool: WorkerPool | undefined;
197
+ invalidates: Set<string>;
198
+ changedTests: Set<string>;
199
+ runningPromise?: Promise<void>;
200
+ isFirstRun: boolean;
201
+ restartsCount: number;
202
+ private _onRestartListeners;
203
+ constructor();
204
+ setServer(options: UserConfig, server: ViteDevServer): void;
205
+ start(filters?: string[]): Promise<void>;
206
+ runFiles(files: string[]): Promise<void>;
207
+ log(...args: any[]): void;
208
+ error(...args: any[]): void;
209
+ private _rerunTimer;
210
+ private scheduleRerun;
211
+ private unregisterWatcher;
212
+ private registerWatcher;
213
+ private handleFileChanged;
214
+ close(): Promise<void>;
215
+ report<T extends keyof Reporter>(name: T, ...args: ArgumentsType<Reporter[T]>): Promise<void>;
216
+ globTestFiles(filters?: string[]): Promise<string[]>;
217
+ isTargetFile(id: string): boolean;
218
+ onServerRestarted(fn: () => void): void;
219
+ }
220
+
221
+ declare abstract class BaseReporter implements Reporter {
222
+ ctx: Vitest;
223
+ start: number;
224
+ end: number;
225
+ watchFilters?: string[];
226
+ isTTY: boolean;
227
+ constructor(ctx: Vitest);
228
+ relative(path: string): string;
229
+ onFinished(files?: File[]): Promise<void>;
230
+ onTaskUpdate(pack: TaskResultPack): void;
231
+ isFirstWatchRun: boolean;
232
+ onWatcherStart(): Promise<void>;
233
+ onWatcherRerun(files: string[], trigger: string): Promise<void>;
234
+ onUserConsoleLog(log: UserConsoleLog): void;
235
+ onServerRestart(): void;
236
+ reportSummary(files: File[]): Promise<void>;
237
+ private printTaskErrors;
238
+ }
239
+
240
+ interface ListRendererOptions {
241
+ renderSucceed?: boolean;
242
+ }
243
+ declare const createListRenderer: (_tasks: Task[], options?: ListRendererOptions) => {
244
+ start(): any;
245
+ update(_tasks: Task[]): any;
246
+ stop(): Promise<any>;
247
+ clear(): void;
248
+ };
249
+
250
+ declare class DefaultReporter extends BaseReporter {
251
+ renderer?: ReturnType<typeof createListRenderer>;
252
+ rendererOptions: ListRendererOptions;
253
+ onStart(): void;
254
+ onFinished(files?: File[]): Promise<void>;
255
+ onWatcherStart(): Promise<void>;
256
+ stopListRender(): Promise<void>;
257
+ onWatcherRerun(files: string[], trigger: string): Promise<void>;
258
+ onUserConsoleLog(log: UserConsoleLog): void;
259
+ }
260
+
261
+ declare class DotReporter extends BaseReporter {
262
+ renderer?: ReturnType<typeof createListRenderer>;
263
+ onStart(): void;
264
+ onFinished(files?: File[]): Promise<void>;
265
+ onWatcherStart(): Promise<void>;
266
+ stopListRender(): Promise<void>;
267
+ onWatcherRerun(files: string[], trigger: string): Promise<void>;
268
+ onUserConsoleLog(log: UserConsoleLog): void;
269
+ }
270
+
271
+ declare class VerboseReporter extends DefaultReporter {
272
+ constructor(ctx: Vitest);
273
+ }
274
+
275
+ declare const ReportersMap: {
276
+ default: typeof DefaultReporter;
277
+ verbose: typeof VerboseReporter;
278
+ dot: typeof DotReporter;
279
+ };
280
+ declare type BuiltinReporters = keyof typeof ReportersMap;
281
+
161
282
  declare type Awaitable<T> = T | PromiseLike<T>;
162
283
  declare type Nullable<T> = T | null | undefined;
163
284
  declare type Arrayable<T> = T | Array<T>;
@@ -179,6 +300,16 @@ interface UserConsoleLog {
179
300
  type: 'stdout' | 'stderr';
180
301
  taskId?: string;
181
302
  }
303
+ interface Position {
304
+ line: number;
305
+ column: number;
306
+ }
307
+ interface ParsedStack {
308
+ method: string;
309
+ file: string;
310
+ line: number;
311
+ column: number;
312
+ }
182
313
 
183
314
  declare type ChainableFunction<T extends string, Args extends any[], R = any> = {
184
315
  (...args: Args): R;
@@ -371,7 +502,7 @@ interface InlineConfig {
371
502
  /**
372
503
  * Custom reporter for output
373
504
  */
374
- reporters?: Reporter | Reporter[];
505
+ reporters?: Arrayable<BuiltinReporters | Reporter>;
375
506
  /**
376
507
  * Enable multi-threading
377
508
  *
@@ -489,6 +620,7 @@ interface WorkerContext {
489
620
  }
490
621
  interface RpcMap {
491
622
  fetch: [[id: string], string | undefined];
623
+ getSourceMap: [[id: string, force?: boolean], RawSourceMap | undefined];
492
624
  log: [[UserConsoleLog], void];
493
625
  processExit: [[code?: number], void];
494
626
  onCollected: [[files: File[]], void];
@@ -504,30 +636,18 @@ declare type RpcPayload<T extends keyof RpcMap = keyof RpcMap> = {
504
636
  args: RpcMap[T][0];
505
637
  };
506
638
 
507
- declare const suite: ChainableFunction<"skip" | "only" | "todo" | "concurrent", [name: string, factory?: TestFactory | undefined], SuiteCollector>;
639
+ declare const suite: ChainableFunction<"concurrent" | "skip" | "only" | "todo", [name: string, factory?: TestFactory | undefined], SuiteCollector>;
508
640
  declare const test: TestCollector;
509
- declare const describe: ChainableFunction<"skip" | "only" | "todo" | "concurrent", [name: string, factory?: TestFactory | undefined], SuiteCollector>;
641
+ declare const describe: ChainableFunction<"concurrent" | "skip" | "only" | "todo", [name: string, factory?: TestFactory | undefined], SuiteCollector>;
510
642
  declare const it: TestCollector;
511
- declare global {
512
- namespace NodeJS {
513
- interface Process {
514
- __vitest_worker__: {
515
- config: ResolvedConfig;
516
- rpc: RpcCall;
517
- send: RpcSend;
518
- current?: Test;
519
- filepath?: string;
520
- moduleCache: Map<string, ModuleCache>;
521
- };
522
- }
523
- }
524
- }
525
643
 
526
644
  declare const beforeAll: (fn: SuiteHooks['beforeAll'][0], timeout?: number | undefined) => void;
527
645
  declare const afterAll: (fn: SuiteHooks['afterAll'][0], timeout?: number | undefined) => void;
528
646
  declare const beforeEach: (fn: SuiteHooks['beforeEach'][0], timeout?: number | undefined) => void;
529
647
  declare const afterEach: (fn: SuiteHooks['afterEach'][0], timeout?: number | undefined) => void;
530
648
 
649
+ declare const expect: Chai.ExpectStatic;
650
+
531
651
  interface MockResultReturn<T> {
532
652
  type: 'return';
533
653
  value: T;
@@ -620,10 +740,60 @@ declare class VitestUtils {
620
740
  getMockedDate(): string | number | Date | null;
621
741
  spyOn: typeof spyOn;
622
742
  fn: typeof fn;
623
- mock(path: string): void;
743
+ /**
744
+ * Makes all `imports` to passed module to be mocked.
745
+ * - If there is a factory, will return it's result. The call to `vi.mock` is hoisted to the top of the file,
746
+ * so you don't have access to variables declared in the global file scope, if you didn't put them before imports!
747
+ * - If `__mocks__` folder with file of the same name exist, all imports will
748
+ * return it.
749
+ * - If there is no `__mocks__` folder or a file with the same name inside, will call original
750
+ * module and mock it.
751
+ * @param path Path to the module. Can be aliased, if your config suppors it
752
+ * @param factory Factory for the mocked module. Has the highest priority.
753
+ */
754
+ mock(path: string, factory?: () => any): void;
755
+ /**
756
+ * Removes module from mocked registry. All subsequent calls to import will
757
+ * return original module even if it was mocked.
758
+ * @param path Path to the module. Can be aliased, if your config suppors it
759
+ */
624
760
  unmock(path: string): void;
761
+ /**
762
+ * Imports module, bypassing all checks if it should be mocked.
763
+ * Can be useful if you want to mock module partially.
764
+ * @example
765
+ * vi.mock('./example', async () => {
766
+ * const axios = await vi.importActual('./example')
767
+ *
768
+ * return { ...axios, get: vi.fn() }
769
+ * })
770
+ * @param path Path to the module. Can be aliased, if your config suppors it
771
+ * @returns Actual module without spies
772
+ */
625
773
  importActual<T>(path: string): Promise<T>;
626
- importMock<T>(path: string): Promise<T>;
774
+ /**
775
+ * Imports a module with all of its properties and nested properties mocked.
776
+ * For the rules applied, see docs.
777
+ * @param path Path to the module. Can be aliased, if your config suppors it
778
+ * @returns Fully mocked module
779
+ */
780
+ importMock<T>(path: string): Promise<MaybeMockedDeep<T>>;
781
+ /**
782
+ * Type helpers for TypeScript. In reality just returns the object that was passed.
783
+ * @example
784
+ * import example from './example'
785
+ * vi.mock('./example')
786
+ *
787
+ * test('1+1 equals 2' async () => {
788
+ * vi.mocked(example.calc).mockRestore()
789
+ *
790
+ * const res = example.calc(1, '+', 1)
791
+ *
792
+ * expect(res).toBe(2)
793
+ * })
794
+ * @param item Anything that can be mocked
795
+ * @param deep If the object is deeply mocked
796
+ */
627
797
  mocked<T>(item: T, deep?: false): MaybeMocked<T>;
628
798
  mocked<T>(item: T, deep: true): MaybeMockedDeep<T>;
629
799
  isMockFunction(fn: any): any;
@@ -654,6 +824,7 @@ declare global {
654
824
  any(constructor: unknown): Any;
655
825
  arrayContaining(expected: any): ArrayContaining;
656
826
  stringMatching(expected: RegExp): StringMatching;
827
+ assertions(expected: number): void;
657
828
  }
658
829
  interface Assertion {
659
830
  chaiEqual(expected: any): void;
@@ -708,4 +879,4 @@ declare global {
708
879
  }
709
880
  }
710
881
 
711
- export { ArgumentsOf, ArgumentsType, Arrayable, Awaitable, BuiltinEnvironment, ComputeMode, ConstructorArgumentsOf, DoneCallback, Environment, EnvironmentReturn, File, HookListener, InlineConfig, JestMockCompat, JestMockCompatContext, JestMockCompatFn, MaybeMocked, MaybeMockedConstructor, MaybeMockedDeep, MethodKeysOf, MockWithArgs, MockableFunction, MockedFunction, MockedFunctionDeep, MockedObject, MockedObjectDeep, ModuleCache, Nullable, PropertyKeysOf, Reporter, ResolvedConfig, RpcCall, RpcMap, RpcPayload, RpcSend, RunMode, RuntimeContext, SnapshotData, SnapshotMatchOptions, SnapshotResult, SnapshotStateOptions, SnapshotSummary, SnapshotUpdateState, Suite, SuiteCollector, SuiteHooks, Task, TaskBase, TaskResult, TaskResultPack, TaskState, Test, TestCollector, TestFactory, TestFunction, UncheckedSnapshot, UserConfig, UserConsoleLog, WorkerContext, afterAll, afterEach, beforeAll, beforeEach, describe, fn, it, spies, spyOn, suite, test, vi, vitest };
882
+ export { ArgumentsOf, ArgumentsType, Arrayable, Awaitable, BuiltinEnvironment, ComputeMode, ConstructorArgumentsOf, DoneCallback, Environment, EnvironmentReturn, File, HookListener, InlineConfig, JestMockCompat, JestMockCompatContext, JestMockCompatFn, MaybeMocked, MaybeMockedConstructor, MaybeMockedDeep, MethodKeysOf, MockWithArgs, MockableFunction, MockedFunction, MockedFunctionDeep, MockedObject, MockedObjectDeep, ModuleCache, Nullable, ParsedStack, Position, PropertyKeysOf, Reporter, ResolvedConfig, RpcCall, RpcMap, RpcPayload, RpcSend, RunMode, RuntimeContext, SnapshotData, SnapshotMatchOptions, SnapshotResult, SnapshotStateOptions, SnapshotSummary, SnapshotUpdateState, Suite, SuiteCollector, SuiteHooks, Task, TaskBase, TaskResult, TaskResultPack, TaskState, Test, TestCollector, TestFactory, TestFunction, UncheckedSnapshot, UserConfig, UserConsoleLog, WorkerContext, afterAll, afterEach, beforeAll, beforeEach, describe, expect, fn, it, spies, spyOn, suite, test, vi, vitest };
package/dist/index.js CHANGED
@@ -1,10 +1,9 @@
1
- export { d as describe, i as it, s as suite, t as test, b as vi, v as vitest } from './vi-9754296d.js';
2
- export { a as afterAll, d as afterEach, b as beforeAll, c as beforeEach } from './index-0961cf69.js';
3
- export { assert, default as chai, expect, should } from 'chai';
4
- export { f as fn, s as spies, a as spyOn } from './jest-mock-8498c46d.js';
5
- import './utils-d97bd6d9.js';
1
+ export { d as describe, i as it, c as suite, t as test, e as vi, v as vitest } from './vi-51946984.js';
2
+ export { a as afterAll, d as afterEach, b as beforeAll, c as beforeEach, e as expect } from './index-09437c50.js';
3
+ export { f as fn, s as spies, a as spyOn } from './jest-mock-a57b745c.js';
4
+ export { assert, default as chai, should } from 'chai';
5
+ import './index-041e627e.js';
6
6
  import 'tty';
7
7
  import 'local-pkg';
8
- import 'path';
9
8
  import './_commonjsHelpers-c9e3b764.js';
10
9
  import 'tinyspy';
@@ -44,10 +44,7 @@ function enhanceSpy(spy) {
44
44
  }
45
45
  };
46
46
  let onceImplementations = [];
47
- let name = "";
48
- Object.defineProperty(stub, "name", {
49
- get: () => name
50
- });
47
+ let name = stub.name;
51
48
  stub.getMockName = () => name || "vi.fn()";
52
49
  stub.mockName = (n) => {
53
50
  name = n;