vitest 0.0.103 → 0.0.104
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/LICENSE.md +44 -0
- package/dist/cli.js +7 -5
- package/dist/{constants-a1417084.js → constants-c4dc2ff5.js} +1 -1
- package/dist/entry.js +29 -4
- package/dist/{error-0e2f75a4.js → error-796962c6.js} +1 -1
- package/dist/{global-12f52a87.js → global-166f6789.js} +5 -5
- package/dist/index-145f6f09.js +31 -0
- package/dist/{index-6f17b444.js → index-40564dba.js} +1139 -21
- package/dist/index.d.ts +64 -8
- package/dist/index.js +3 -3
- package/dist/{middleware-05e03d95.js → middleware-0627688d.js} +4 -2
- package/dist/node.d.ts +15 -0
- package/dist/node.js +6 -4
- package/dist/{suite-64b3e636.js → suite-8d666d5a.js} +1 -1
- package/dist/{utils-c8e62373.js → utils-49e5008c.js} +339 -1
- package/dist/utils.js +3 -1
- package/dist/worker.js +113 -6
- package/package.json +2 -1
- package/dist/index-a190f5a1.js +0 -352
package/dist/index.d.ts
CHANGED
|
@@ -150,6 +150,12 @@ declare class Any extends AsymmetricMatcher<any> {
|
|
|
150
150
|
getExpectedType(): string;
|
|
151
151
|
toAsymmetricMatcher(): string;
|
|
152
152
|
}
|
|
153
|
+
declare class StringMatching extends AsymmetricMatcher<RegExp> {
|
|
154
|
+
constructor(sample: string | RegExp, inverse?: boolean);
|
|
155
|
+
asymmetricMatch(other: string): boolean;
|
|
156
|
+
toString(): string;
|
|
157
|
+
getExpectedType(): string;
|
|
158
|
+
}
|
|
153
159
|
|
|
154
160
|
declare type Awaitable<T> = T | PromiseLike<T>;
|
|
155
161
|
declare type Nullable<T> = T | null | undefined;
|
|
@@ -426,6 +432,21 @@ interface InlineConfig {
|
|
|
426
432
|
* @default false
|
|
427
433
|
*/
|
|
428
434
|
api?: boolean | number;
|
|
435
|
+
/**
|
|
436
|
+
* Will call `.mockClear()` on all spies before each test
|
|
437
|
+
* @default false
|
|
438
|
+
*/
|
|
439
|
+
clearMocks?: boolean;
|
|
440
|
+
/**
|
|
441
|
+
* Will call `.mockReset()` on all spies before each test
|
|
442
|
+
* @default false
|
|
443
|
+
*/
|
|
444
|
+
mockReset?: boolean;
|
|
445
|
+
/**
|
|
446
|
+
* Will call `.mockRestore()` on all spies before each test
|
|
447
|
+
* @default false
|
|
448
|
+
*/
|
|
449
|
+
restoreMocks?: boolean;
|
|
429
450
|
}
|
|
430
451
|
interface UserConfig extends InlineConfig {
|
|
431
452
|
/**
|
|
@@ -494,6 +515,7 @@ declare global {
|
|
|
494
515
|
rpc: RpcCall;
|
|
495
516
|
send: RpcSend;
|
|
496
517
|
current?: Test;
|
|
518
|
+
filepath?: string;
|
|
497
519
|
moduleCache: Map<string, ModuleCache>;
|
|
498
520
|
};
|
|
499
521
|
}
|
|
@@ -545,6 +567,37 @@ interface JestMockCompat<TArgs extends any[] = any[], TReturns = any> {
|
|
|
545
567
|
interface JestMockCompatFn<TArgs extends any[] = any, TReturns = any> extends JestMockCompat<TArgs, TReturns> {
|
|
546
568
|
(...args: TArgs): TReturns;
|
|
547
569
|
}
|
|
570
|
+
declare type MockableFunction = (...args: Array<any>) => any;
|
|
571
|
+
declare type MethodKeysOf<T> = {
|
|
572
|
+
[K in keyof T]: T[K] extends MockableFunction ? K : never;
|
|
573
|
+
}[keyof T];
|
|
574
|
+
declare type PropertyKeysOf<T> = {
|
|
575
|
+
[K in keyof T]: T[K] extends MockableFunction ? never : K;
|
|
576
|
+
}[keyof T];
|
|
577
|
+
declare type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;
|
|
578
|
+
declare type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;
|
|
579
|
+
declare type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? JestMockCompatFn<ConstructorArgumentsOf<T>, R> : T;
|
|
580
|
+
declare type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & {
|
|
581
|
+
[K in keyof T]: T[K];
|
|
582
|
+
};
|
|
583
|
+
declare type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>;
|
|
584
|
+
declare type MockedObject<T> = MaybeMockedConstructor<T> & {
|
|
585
|
+
[K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunction<T[K]> : T[K];
|
|
586
|
+
} & {
|
|
587
|
+
[K in PropertyKeysOf<T>]: T[K];
|
|
588
|
+
};
|
|
589
|
+
declare type MockedObjectDeep<T> = MaybeMockedConstructor<T> & {
|
|
590
|
+
[K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunctionDeep<T[K]> : T[K];
|
|
591
|
+
} & {
|
|
592
|
+
[K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]>;
|
|
593
|
+
};
|
|
594
|
+
declare type MaybeMockedDeep<T> = T extends MockableFunction ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T;
|
|
595
|
+
declare type MaybeMocked<T> = T extends MockableFunction ? MockedFunction<T> : T extends object ? MockedObject<T> : T;
|
|
596
|
+
interface MockWithArgs<T extends MockableFunction> extends JestMockCompatFn<ArgumentsOf<T>, ReturnType<T>> {
|
|
597
|
+
new (...args: ConstructorArgumentsOf<T>): T;
|
|
598
|
+
(...args: ArgumentsOf<T>): ReturnType<T>;
|
|
599
|
+
}
|
|
600
|
+
declare const spies: Set<JestMockCompat<any[], any>>;
|
|
548
601
|
declare function spyOn<T, K extends keyof T>(obj: T, method: K, accessType?: 'get' | 'set'): T[K] extends (...args: infer TArgs) => infer TReturnValue ? JestMockCompat<TArgs, TReturnValue> : JestMockCompat;
|
|
549
602
|
declare type Awaited<T> = T extends Promise<infer R> ? R : never;
|
|
550
603
|
declare function fn<TArgs extends any[] = any[], R = any>(): JestMockCompatFn<TArgs, R>;
|
|
@@ -559,17 +612,19 @@ declare class VitestUtils {
|
|
|
559
612
|
runAllTimers(): void | Promise<void>;
|
|
560
613
|
advanceTimersByTime(ms: number): void | Promise<void>;
|
|
561
614
|
advanceTimersToNextTimer(): void | Promise<void>;
|
|
562
|
-
runAllTicks(): void | Promise<void>;
|
|
563
|
-
setSystemTime(time?: number | Date): void;
|
|
564
|
-
getRealSystemTime(): number;
|
|
565
615
|
getTimerCount(): number;
|
|
566
616
|
spyOn: typeof spyOn;
|
|
567
617
|
fn: typeof fn;
|
|
568
|
-
mock
|
|
618
|
+
mock(path: string): void;
|
|
619
|
+
unmock(path: string): void;
|
|
620
|
+
importActual<T>(path: string): Promise<T>;
|
|
621
|
+
importMock<T>(path: string): Promise<T>;
|
|
622
|
+
mocked<T>(item: T, deep?: false): MaybeMocked<T>;
|
|
623
|
+
mocked<T>(item: T, deep: true): MaybeMockedDeep<T>;
|
|
569
624
|
isMockFunction(fn: any): any;
|
|
570
|
-
clearAllMocks():
|
|
571
|
-
resetAllMocks():
|
|
572
|
-
restoreAllMocks():
|
|
625
|
+
clearAllMocks(): this;
|
|
626
|
+
resetAllMocks(): this;
|
|
627
|
+
restoreAllMocks(): this;
|
|
573
628
|
}
|
|
574
629
|
declare const vitest: VitestUtils;
|
|
575
630
|
declare const vi: VitestUtils;
|
|
@@ -593,6 +648,7 @@ declare global {
|
|
|
593
648
|
objectContaining(expected: any): ObjectContaining;
|
|
594
649
|
any(constructor: unknown): Any;
|
|
595
650
|
arrayContaining(expected: any): ArrayContaining;
|
|
651
|
+
stringMatching(expected: RegExp): StringMatching;
|
|
596
652
|
}
|
|
597
653
|
interface Assertion {
|
|
598
654
|
chaiEqual(expected: any): void;
|
|
@@ -647,4 +703,4 @@ declare global {
|
|
|
647
703
|
}
|
|
648
704
|
}
|
|
649
705
|
|
|
650
|
-
export { ArgumentsType, Arrayable, Awaitable, BuiltinEnvironment, ComputeMode, DoneCallback, Environment, EnvironmentReturn, File, HookListener, InlineConfig, JestMockCompat, JestMockCompatContext, JestMockCompatFn, ModuleCache, Nullable, 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, spyOn, suite, test, vi, vitest };
|
|
706
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { d as describe, i as it, s as suite, t as test } from './suite-
|
|
2
|
-
export { a as afterAll, d as afterEach, b as beforeAll, c as beforeEach
|
|
1
|
+
export { d as describe, i as it, s as suite, t as test } from './suite-8d666d5a.js';
|
|
2
|
+
export { a as afterAll, d as afterEach, b as beforeAll, c as beforeEach } from './index-145f6f09.js';
|
|
3
3
|
export { assert, default as chai, expect, should } from 'chai';
|
|
4
|
+
export { f as fn, s as spies, a as spyOn, b as vi, v as vitest } from './utils-49e5008c.js';
|
|
4
5
|
import './index-9e71c815.js';
|
|
5
|
-
import './utils-c8e62373.js';
|
|
6
6
|
import 'tty';
|
|
7
7
|
import 'local-pkg';
|
|
8
8
|
import 'path';
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { A as API_PATH } from './constants-
|
|
1
|
+
import { A as API_PATH } from './constants-c4dc2ff5.js';
|
|
2
2
|
import 'url';
|
|
3
|
-
import './utils-
|
|
3
|
+
import './utils-49e5008c.js';
|
|
4
4
|
import 'tty';
|
|
5
5
|
import 'local-pkg';
|
|
6
6
|
import 'path';
|
|
7
|
+
import 'chai';
|
|
8
|
+
import 'tinyspy';
|
|
7
9
|
|
|
8
10
|
/*! (c) 2020 Andrea Giammarchi */
|
|
9
11
|
|
package/dist/node.d.ts
CHANGED
|
@@ -218,6 +218,21 @@ interface InlineConfig {
|
|
|
218
218
|
* @default false
|
|
219
219
|
*/
|
|
220
220
|
api?: boolean | number;
|
|
221
|
+
/**
|
|
222
|
+
* Will call `.mockClear()` on all spies before each test
|
|
223
|
+
* @default false
|
|
224
|
+
*/
|
|
225
|
+
clearMocks?: boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Will call `.mockReset()` on all spies before each test
|
|
228
|
+
* @default false
|
|
229
|
+
*/
|
|
230
|
+
mockReset?: boolean;
|
|
231
|
+
/**
|
|
232
|
+
* Will call `.mockRestore()` on all spies before each test
|
|
233
|
+
* @default false
|
|
234
|
+
*/
|
|
235
|
+
restoreMocks?: boolean;
|
|
221
236
|
}
|
|
222
237
|
interface UserConfig extends InlineConfig {
|
|
223
238
|
/**
|
package/dist/node.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export { c as createVitest } from './index-
|
|
2
|
-
import './utils-
|
|
1
|
+
export { c as createVitest } from './index-40564dba.js';
|
|
2
|
+
import './utils-49e5008c.js';
|
|
3
3
|
import 'tty';
|
|
4
4
|
import 'local-pkg';
|
|
5
5
|
import 'path';
|
|
6
|
+
import 'chai';
|
|
7
|
+
import 'tinyspy';
|
|
6
8
|
import 'vite';
|
|
7
9
|
import 'process';
|
|
8
10
|
import 'fs';
|
|
@@ -10,10 +12,10 @@ import 'os';
|
|
|
10
12
|
import 'util';
|
|
11
13
|
import 'stream';
|
|
12
14
|
import 'events';
|
|
13
|
-
import './constants-
|
|
15
|
+
import './constants-c4dc2ff5.js';
|
|
14
16
|
import 'url';
|
|
15
17
|
import 'perf_hooks';
|
|
16
|
-
import './error-
|
|
18
|
+
import './error-796962c6.js';
|
|
17
19
|
import './index-825cb54c.js';
|
|
18
20
|
import './_commonjsHelpers-bdec4bbd.js';
|
|
19
21
|
import 'assert';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import require$$0 from 'tty';
|
|
2
2
|
import { isPackageExists } from 'local-pkg';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import { util } from 'chai';
|
|
5
|
+
import * as tinyspy from 'tinyspy';
|
|
4
6
|
|
|
5
7
|
var picocolors = {exports: {}};
|
|
6
8
|
|
|
@@ -248,6 +250,330 @@ const index = {
|
|
|
248
250
|
..._path
|
|
249
251
|
};
|
|
250
252
|
|
|
253
|
+
const spies = /* @__PURE__ */ new Set();
|
|
254
|
+
function spyOn(obj, method, accessType) {
|
|
255
|
+
const dictionary = {
|
|
256
|
+
get: "getter",
|
|
257
|
+
set: "setter"
|
|
258
|
+
};
|
|
259
|
+
const objMethod = accessType ? { [dictionary[accessType]]: method } : method;
|
|
260
|
+
const stub = tinyspy.spyOn(obj, objMethod);
|
|
261
|
+
return enhanceSpy(stub);
|
|
262
|
+
}
|
|
263
|
+
function enhanceSpy(spy) {
|
|
264
|
+
const stub = spy;
|
|
265
|
+
let implementation;
|
|
266
|
+
const instances = [];
|
|
267
|
+
const mockContext = {
|
|
268
|
+
get calls() {
|
|
269
|
+
return stub.calls;
|
|
270
|
+
},
|
|
271
|
+
get instances() {
|
|
272
|
+
return instances;
|
|
273
|
+
},
|
|
274
|
+
get invocationCallOrder() {
|
|
275
|
+
return [];
|
|
276
|
+
},
|
|
277
|
+
get results() {
|
|
278
|
+
return stub.results.map(([callType, value]) => {
|
|
279
|
+
const type = callType === "error" ? "throw" : "return";
|
|
280
|
+
return { type, value };
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
let onceImplementations = [];
|
|
285
|
+
let name = "";
|
|
286
|
+
Object.defineProperty(stub, "name", {
|
|
287
|
+
get: () => name
|
|
288
|
+
});
|
|
289
|
+
stub.getMockName = () => name || "vi.fn()";
|
|
290
|
+
stub.mockName = (n) => {
|
|
291
|
+
name = n;
|
|
292
|
+
return stub;
|
|
293
|
+
};
|
|
294
|
+
stub.mockClear = () => {
|
|
295
|
+
stub.reset();
|
|
296
|
+
return stub;
|
|
297
|
+
};
|
|
298
|
+
stub.mockReset = () => {
|
|
299
|
+
stub.reset();
|
|
300
|
+
implementation = () => void 0;
|
|
301
|
+
onceImplementations = [];
|
|
302
|
+
return stub;
|
|
303
|
+
};
|
|
304
|
+
stub.mockRestore = () => {
|
|
305
|
+
stub.mockReset();
|
|
306
|
+
implementation = void 0;
|
|
307
|
+
return stub;
|
|
308
|
+
};
|
|
309
|
+
stub.getMockImplementation = () => implementation;
|
|
310
|
+
stub.mockImplementation = (fn2) => {
|
|
311
|
+
implementation = fn2;
|
|
312
|
+
return stub;
|
|
313
|
+
};
|
|
314
|
+
stub.mockImplementationOnce = (fn2) => {
|
|
315
|
+
onceImplementations.push(fn2);
|
|
316
|
+
return stub;
|
|
317
|
+
};
|
|
318
|
+
stub.mockReturnThis = () => stub.mockImplementation(function() {
|
|
319
|
+
return this;
|
|
320
|
+
});
|
|
321
|
+
stub.mockReturnValue = (val) => stub.mockImplementation(() => val);
|
|
322
|
+
stub.mockReturnValueOnce = (val) => stub.mockImplementationOnce(() => val);
|
|
323
|
+
stub.mockResolvedValue = (val) => stub.mockImplementation(() => Promise.resolve(val));
|
|
324
|
+
stub.mockResolvedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.resolve(val));
|
|
325
|
+
stub.mockRejectedValue = (val) => stub.mockImplementation(() => Promise.reject(val));
|
|
326
|
+
stub.mockRejectedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.reject(val));
|
|
327
|
+
util.addProperty(stub, "mock", () => mockContext);
|
|
328
|
+
stub.willCall(function(...args) {
|
|
329
|
+
instances.push(this);
|
|
330
|
+
const impl = onceImplementations.shift() || implementation || stub.getOriginal() || (() => {
|
|
331
|
+
});
|
|
332
|
+
return impl.apply(this, args);
|
|
333
|
+
});
|
|
334
|
+
spies.add(stub);
|
|
335
|
+
return stub;
|
|
336
|
+
}
|
|
337
|
+
function fn(implementation) {
|
|
338
|
+
return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {
|
|
339
|
+
}) }, "fn"));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const originalSetTimeout = global.setTimeout;
|
|
343
|
+
const originalSetInterval = global.setInterval;
|
|
344
|
+
const originalClearTimeout = global.clearTimeout;
|
|
345
|
+
const originalClearInterval = global.clearInterval;
|
|
346
|
+
const MAX_LOOPS = 1e4;
|
|
347
|
+
const assertEvery = (assertions, message) => {
|
|
348
|
+
if (assertions.some((a) => !a))
|
|
349
|
+
throw new Error(message);
|
|
350
|
+
};
|
|
351
|
+
const assertMaxLoop = (times) => {
|
|
352
|
+
if (times >= MAX_LOOPS)
|
|
353
|
+
throw new Error("setTimeout/setInterval called 10 000 times. It's possible it stuck in an infinite loop.");
|
|
354
|
+
};
|
|
355
|
+
const getNodeTimeout = (id) => {
|
|
356
|
+
const timer = {
|
|
357
|
+
ref: () => timer,
|
|
358
|
+
unref: () => timer,
|
|
359
|
+
hasRef: () => true,
|
|
360
|
+
refresh: () => timer,
|
|
361
|
+
[Symbol.toPrimitive]: () => id
|
|
362
|
+
};
|
|
363
|
+
return timer;
|
|
364
|
+
};
|
|
365
|
+
class FakeTimers {
|
|
366
|
+
constructor() {
|
|
367
|
+
this._advancedTime = 0;
|
|
368
|
+
this._nestedTime = {};
|
|
369
|
+
this._scopeId = 0;
|
|
370
|
+
this._isNested = false;
|
|
371
|
+
this._isOnlyPending = false;
|
|
372
|
+
this._spyid = 0;
|
|
373
|
+
this._isMocked = false;
|
|
374
|
+
this._tasksQueue = [];
|
|
375
|
+
this._queueCount = 0;
|
|
376
|
+
}
|
|
377
|
+
useFakeTimers() {
|
|
378
|
+
this._isMocked = true;
|
|
379
|
+
this.reset();
|
|
380
|
+
const spyFactory = (spyType, resultBuilder) => {
|
|
381
|
+
return (cb, ms = 0) => {
|
|
382
|
+
const id = ++this._spyid;
|
|
383
|
+
const nestedTo = Object.entries(this._nestedTime).filter(([key]) => Number(key) <= this._scopeId);
|
|
384
|
+
const nestedMs = nestedTo.reduce((total, [, ms2]) => total + ms2, ms);
|
|
385
|
+
const call = { id, cb, ms, nestedMs, scopeId: this._scopeId };
|
|
386
|
+
const task = { type: spyType, call, nested: this._isNested };
|
|
387
|
+
this.pushTask(task);
|
|
388
|
+
return resultBuilder(id, cb);
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
this._setTimeout = spyOn(global, "setTimeout").mockImplementation(spyFactory("timeout" /* Timeout */, getNodeTimeout));
|
|
392
|
+
this._setInterval = spyOn(global, "setInterval").mockImplementation(spyFactory("interval" /* Interval */, getNodeTimeout));
|
|
393
|
+
const clearTimerFactory = (spyType) => (id) => {
|
|
394
|
+
if (id === void 0)
|
|
395
|
+
return;
|
|
396
|
+
const index = this._tasksQueue.findIndex(({ call, type }) => type === spyType && call.id === Number(id));
|
|
397
|
+
if (index !== -1)
|
|
398
|
+
this._tasksQueue.splice(index, 1);
|
|
399
|
+
};
|
|
400
|
+
this._clearTimeout = spyOn(global, "clearTimeout").mockImplementation(clearTimerFactory("timeout" /* Timeout */));
|
|
401
|
+
this._clearInterval = spyOn(global, "clearInterval").mockImplementation(clearTimerFactory("interval" /* Interval */));
|
|
402
|
+
}
|
|
403
|
+
useRealTimers() {
|
|
404
|
+
this._isMocked = false;
|
|
405
|
+
this.reset();
|
|
406
|
+
global.setTimeout = originalSetTimeout;
|
|
407
|
+
global.setInterval = originalSetInterval;
|
|
408
|
+
global.clearTimeout = originalClearTimeout;
|
|
409
|
+
global.clearInterval = originalClearInterval;
|
|
410
|
+
}
|
|
411
|
+
runOnlyPendingTimers() {
|
|
412
|
+
this.assertMocked();
|
|
413
|
+
this._isOnlyPending = true;
|
|
414
|
+
this.runQueue();
|
|
415
|
+
}
|
|
416
|
+
runAllTimers() {
|
|
417
|
+
this.assertMocked();
|
|
418
|
+
this.runQueue();
|
|
419
|
+
}
|
|
420
|
+
advanceTimersByTime(ms) {
|
|
421
|
+
this.assertMocked();
|
|
422
|
+
this._advancedTime += ms;
|
|
423
|
+
this.runQueue();
|
|
424
|
+
}
|
|
425
|
+
advanceTimersToNextTimer() {
|
|
426
|
+
this.assertMocked();
|
|
427
|
+
this.callQueueItem(0);
|
|
428
|
+
}
|
|
429
|
+
getTimerCount() {
|
|
430
|
+
this.assertMocked();
|
|
431
|
+
return this._tasksQueue.length;
|
|
432
|
+
}
|
|
433
|
+
reset() {
|
|
434
|
+
var _a, _b, _c, _d;
|
|
435
|
+
this._advancedTime = 0;
|
|
436
|
+
this._nestedTime = {};
|
|
437
|
+
this._isNested = false;
|
|
438
|
+
this._isOnlyPending = false;
|
|
439
|
+
this._spyid = 0;
|
|
440
|
+
this._queueCount = 0;
|
|
441
|
+
this._tasksQueue = [];
|
|
442
|
+
(_a = this._clearInterval) == null ? void 0 : _a.mockRestore();
|
|
443
|
+
(_b = this._clearTimeout) == null ? void 0 : _b.mockRestore();
|
|
444
|
+
(_c = this._setInterval) == null ? void 0 : _c.mockRestore();
|
|
445
|
+
(_d = this._setTimeout) == null ? void 0 : _d.mockRestore();
|
|
446
|
+
}
|
|
447
|
+
callQueueItem(index) {
|
|
448
|
+
var _a, _b;
|
|
449
|
+
const task = this._tasksQueue[index];
|
|
450
|
+
if (!task)
|
|
451
|
+
return;
|
|
452
|
+
const { call, type } = task;
|
|
453
|
+
this._scopeId = call.id;
|
|
454
|
+
this._isNested = true;
|
|
455
|
+
(_a = this._nestedTime)[_b = call.id] ?? (_a[_b] = 0);
|
|
456
|
+
this._nestedTime[call.id] += call.ms;
|
|
457
|
+
if (type === "timeout") {
|
|
458
|
+
this.removeTask(index);
|
|
459
|
+
} else if (type === "interval") {
|
|
460
|
+
call.nestedMs += call.ms;
|
|
461
|
+
const nestedMs = call.nestedMs;
|
|
462
|
+
const closestTask = this._tasksQueue.findIndex(({ type: type2, call: call2 }) => type2 === "interval" && call2.nestedMs < nestedMs);
|
|
463
|
+
if (closestTask !== -1 && closestTask !== index)
|
|
464
|
+
this.ensureQueueOrder();
|
|
465
|
+
}
|
|
466
|
+
call.cb();
|
|
467
|
+
this._queueCount++;
|
|
468
|
+
}
|
|
469
|
+
runQueue() {
|
|
470
|
+
let index = 0;
|
|
471
|
+
while (this._tasksQueue[index]) {
|
|
472
|
+
assertMaxLoop(this._queueCount);
|
|
473
|
+
const { call, nested } = this._tasksQueue[index];
|
|
474
|
+
if (this._advancedTime && call.nestedMs > this._advancedTime)
|
|
475
|
+
break;
|
|
476
|
+
if (this._isOnlyPending && nested) {
|
|
477
|
+
index++;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
this.callQueueItem(index);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
removeTask(index) {
|
|
484
|
+
if (index === 0)
|
|
485
|
+
this._tasksQueue.shift();
|
|
486
|
+
else
|
|
487
|
+
this._tasksQueue.splice(index, 1);
|
|
488
|
+
}
|
|
489
|
+
pushTask(task) {
|
|
490
|
+
this._tasksQueue.push(task);
|
|
491
|
+
this.ensureQueueOrder();
|
|
492
|
+
}
|
|
493
|
+
ensureQueueOrder() {
|
|
494
|
+
this._tasksQueue.sort((t1, t2) => {
|
|
495
|
+
const diff = t1.call.nestedMs - t2.call.nestedMs;
|
|
496
|
+
if (diff === 0) {
|
|
497
|
+
if (t1.type === "immediate" /* Immediate */ && t2.type !== "immediate" /* Immediate */)
|
|
498
|
+
return 1;
|
|
499
|
+
return 0;
|
|
500
|
+
}
|
|
501
|
+
return diff;
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
assertMocked() {
|
|
505
|
+
assertEvery([
|
|
506
|
+
this._isMocked,
|
|
507
|
+
this._setTimeout,
|
|
508
|
+
this._setInterval,
|
|
509
|
+
this._clearTimeout,
|
|
510
|
+
this._clearInterval
|
|
511
|
+
], 'timers are not mocked. try calling "vitest.useFakeTimers()" first');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
class VitestUtils {
|
|
516
|
+
constructor() {
|
|
517
|
+
this.spyOn = spyOn;
|
|
518
|
+
this.fn = fn;
|
|
519
|
+
this._timers = new FakeTimers();
|
|
520
|
+
}
|
|
521
|
+
useFakeTimers() {
|
|
522
|
+
return this._timers.useFakeTimers();
|
|
523
|
+
}
|
|
524
|
+
useRealTimers() {
|
|
525
|
+
return this._timers.useRealTimers();
|
|
526
|
+
}
|
|
527
|
+
runOnlyPendingTimers() {
|
|
528
|
+
return this._timers.runOnlyPendingTimers();
|
|
529
|
+
}
|
|
530
|
+
runAllTimers() {
|
|
531
|
+
return this._timers.runAllTimers();
|
|
532
|
+
}
|
|
533
|
+
advanceTimersByTime(ms) {
|
|
534
|
+
return this._timers.advanceTimersByTime(ms);
|
|
535
|
+
}
|
|
536
|
+
advanceTimersToNextTimer() {
|
|
537
|
+
return this._timers.advanceTimersToNextTimer();
|
|
538
|
+
}
|
|
539
|
+
getTimerCount() {
|
|
540
|
+
return this._timers.getTimerCount();
|
|
541
|
+
}
|
|
542
|
+
mock(path) {
|
|
543
|
+
}
|
|
544
|
+
unmock(path) {
|
|
545
|
+
}
|
|
546
|
+
async importActual(path) {
|
|
547
|
+
return {};
|
|
548
|
+
}
|
|
549
|
+
async importMock(path) {
|
|
550
|
+
return {};
|
|
551
|
+
}
|
|
552
|
+
mocked(item, _deep = false) {
|
|
553
|
+
return item;
|
|
554
|
+
}
|
|
555
|
+
isMockFunction(fn2) {
|
|
556
|
+
return typeof fn2 === "function" && "__isSpy" in fn2 && fn2.__isSpy;
|
|
557
|
+
}
|
|
558
|
+
clearAllMocks() {
|
|
559
|
+
__vitest__clearMocks__({ clearMocks: true });
|
|
560
|
+
spies.forEach((spy) => spy.mockClear());
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
563
|
+
resetAllMocks() {
|
|
564
|
+
__vitest__clearMocks__({ mockReset: true });
|
|
565
|
+
spies.forEach((spy) => spy.mockReset());
|
|
566
|
+
return this;
|
|
567
|
+
}
|
|
568
|
+
restoreAllMocks() {
|
|
569
|
+
__vitest__clearMocks__({ restoreMocks: true });
|
|
570
|
+
spies.forEach((spy) => spy.mockRestore());
|
|
571
|
+
return this;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
const vitest = new VitestUtils();
|
|
575
|
+
const vi = vitest;
|
|
576
|
+
|
|
251
577
|
function toArray(array) {
|
|
252
578
|
array = array || [];
|
|
253
579
|
if (Array.isArray(array))
|
|
@@ -260,6 +586,9 @@ function notNullish(v) {
|
|
|
260
586
|
function slash(str) {
|
|
261
587
|
return str.replace(/\\/g, "/");
|
|
262
588
|
}
|
|
589
|
+
function mergeSlashes(str) {
|
|
590
|
+
return str.replace(/\/\//g, "/");
|
|
591
|
+
}
|
|
263
592
|
const noop = () => {
|
|
264
593
|
};
|
|
265
594
|
function partitionSuiteChildren(suite) {
|
|
@@ -342,5 +671,14 @@ async function ensurePackageInstalled(dependency, promptInstall = !process.env.C
|
|
|
342
671
|
}
|
|
343
672
|
return false;
|
|
344
673
|
}
|
|
674
|
+
function clearModuleMocks() {
|
|
675
|
+
const { clearMocks, mockReset, restoreMocks } = process.__vitest_worker__.config;
|
|
676
|
+
if (restoreMocks)
|
|
677
|
+
vi.restoreAllMocks();
|
|
678
|
+
else if (mockReset)
|
|
679
|
+
vi.resetAllMocks();
|
|
680
|
+
else if (clearMocks)
|
|
681
|
+
vi.clearAllMocks();
|
|
682
|
+
}
|
|
345
683
|
|
|
346
|
-
export {
|
|
684
|
+
export { getTasks as A, spyOn as a, vi as b, c, slash as d, ensurePackageInstalled as e, fn as f, getNames as g, getTests as h, isAbsolute as i, dirname as j, basename as k, getSuites as l, resolve as m, noop as n, hasFailed as o, notNullish as p, mergeSlashes as q, relative as r, spies as s, toArray as t, index as u, vitest as v, interpretOnlyMode as w, partitionSuiteChildren as x, hasTests as y, clearModuleMocks as z };
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { e as ensurePackageInstalled, g as getNames,
|
|
1
|
+
export { z as clearModuleMocks, e as ensurePackageInstalled, g as getNames, l as getSuites, A as getTasks, h as getTests, o as hasFailed, y as hasTests, w as interpretOnlyMode, q as mergeSlashes, n as noop, p as notNullish, x as partitionSuiteChildren, m as resolvePath, d as slash, t as toArray } from './utils-49e5008c.js';
|
|
2
2
|
import 'local-pkg';
|
|
3
3
|
import 'tty';
|
|
4
4
|
import 'path';
|
|
5
|
+
import 'chai';
|
|
6
|
+
import 'tinyspy';
|