vitest 0.0.100 → 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.
@@ -1,351 +0,0 @@
1
- import { g as getCurrentSuite, w as withTimeout, a as getDefaultHookTimeout, u as util, s as suite, t as test, d as describe, i as it, b as assert, c as should, e as expect, f as chai } from './index-0b2be7f7.js';
2
- import * as tinyspy from 'tinyspy';
3
- import { spies } from 'tinyspy';
4
-
5
- const beforeAll = (fn, timeout) => getCurrentSuite().on("beforeAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
6
- const afterAll = (fn, timeout) => getCurrentSuite().on("afterAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
7
- const beforeEach = (fn, timeout) => getCurrentSuite().on("beforeEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
8
- const afterEach = (fn, timeout) => getCurrentSuite().on("afterEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
9
-
10
- function spyOn(obj, method, accessType) {
11
- const dictionary = {
12
- get: "getter",
13
- set: "setter"
14
- };
15
- const objMethod = accessType ? { [dictionary[accessType]]: method } : method;
16
- const stub = tinyspy.spyOn(obj, objMethod);
17
- return enhanceSpy(stub);
18
- }
19
- function enhanceSpy(spy) {
20
- const stub = spy;
21
- let implementation;
22
- const instances = [];
23
- const mockContext = {
24
- get calls() {
25
- return stub.calls;
26
- },
27
- get instances() {
28
- return instances;
29
- },
30
- get invocationCallOrder() {
31
- return [];
32
- },
33
- get results() {
34
- return stub.results.map(([callType, value]) => {
35
- const type = callType === "error" ? "throw" : "return";
36
- return { type, value };
37
- });
38
- }
39
- };
40
- let onceImplementations = [];
41
- let name = "";
42
- Object.defineProperty(stub, "name", {
43
- get: () => name
44
- });
45
- stub.getMockName = () => name || "vi.fn()";
46
- stub.mockName = (n) => {
47
- name = n;
48
- return stub;
49
- };
50
- stub.mockClear = () => {
51
- stub.reset();
52
- return stub;
53
- };
54
- stub.mockReset = () => {
55
- stub.reset();
56
- return stub;
57
- };
58
- stub.mockRestore = () => {
59
- implementation = void 0;
60
- onceImplementations = [];
61
- stub.reset();
62
- stub.restore();
63
- return stub;
64
- };
65
- stub.getMockImplementation = () => implementation;
66
- stub.mockImplementation = (fn2) => {
67
- implementation = fn2;
68
- return stub;
69
- };
70
- stub.mockImplementationOnce = (fn2) => {
71
- onceImplementations.push(fn2);
72
- return stub;
73
- };
74
- stub.mockReturnThis = () => stub.mockImplementation(function() {
75
- return this;
76
- });
77
- stub.mockReturnValue = (val) => stub.mockImplementation(() => val);
78
- stub.mockReturnValueOnce = (val) => stub.mockImplementationOnce(() => val);
79
- stub.mockResolvedValue = (val) => stub.mockImplementation(() => Promise.resolve(val));
80
- stub.mockResolvedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.resolve(val));
81
- stub.mockRejectedValue = (val) => stub.mockImplementation(() => Promise.reject(val));
82
- stub.mockRejectedValueOnce = (val) => stub.mockImplementation(() => Promise.reject(val));
83
- util.addProperty(stub, "mock", () => mockContext);
84
- stub.willCall(function(...args) {
85
- instances.push(this);
86
- const impl = onceImplementations.shift() || implementation || stub.getOriginal() || (() => {
87
- });
88
- return impl.apply(this, args);
89
- });
90
- return stub;
91
- }
92
- function fn(implementation) {
93
- return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {
94
- }) }, "fn"));
95
- }
96
-
97
- const originalSetTimeout = global.setTimeout;
98
- const originalSetInterval = global.setInterval;
99
- const originalClearTimeout = global.clearTimeout;
100
- const originalClearInterval = global.clearInterval;
101
- const MAX_LOOPS = 1e4;
102
- const assertEvery = (assertions, message) => {
103
- if (assertions.some((a) => !a))
104
- throw new Error(message);
105
- };
106
- const assertMaxLoop = (times) => {
107
- if (times >= MAX_LOOPS)
108
- throw new Error("setTimeout/setInterval called 10 000 times. It's possible it stuck in an infinite loop.");
109
- };
110
- const getNodeTimeout = (id) => {
111
- const timer = {
112
- ref: () => timer,
113
- unref: () => timer,
114
- hasRef: () => true,
115
- refresh: () => timer,
116
- [Symbol.toPrimitive]: () => id
117
- };
118
- return timer;
119
- };
120
- class FakeTimers {
121
- constructor() {
122
- this._advancedTime = 0;
123
- this._nestedTime = {};
124
- this._scopeId = 0;
125
- this._isNested = false;
126
- this._isOnlyPending = false;
127
- this._spyid = 0;
128
- this._isMocked = false;
129
- this._tasksQueue = [];
130
- this._queueCount = 0;
131
- }
132
- useFakeTimers() {
133
- this._isMocked = true;
134
- this.reset();
135
- const spyFactory = (spyType, resultBuilder) => {
136
- return (cb, ms = 0) => {
137
- const id = ++this._spyid;
138
- const nestedTo = Object.entries(this._nestedTime).filter(([key]) => Number(key) <= this._scopeId);
139
- const nestedMs = nestedTo.reduce((total, [, ms2]) => total + ms2, ms);
140
- const call = { id, cb, ms, nestedMs, scopeId: this._scopeId };
141
- const task = { type: spyType, call, nested: this._isNested };
142
- this.pushTask(task);
143
- return resultBuilder(id, cb);
144
- };
145
- };
146
- this._setTimeout = spyOn(global, "setTimeout").mockImplementation(spyFactory("timeout" /* Timeout */, getNodeTimeout));
147
- this._setInterval = spyOn(global, "setInterval").mockImplementation(spyFactory("interval" /* Interval */, getNodeTimeout));
148
- const clearTimerFactory = (spyType) => (id) => {
149
- if (id === void 0)
150
- return;
151
- const index = this._tasksQueue.findIndex(({ call, type }) => type === spyType && call.id === Number(id));
152
- if (index !== -1)
153
- this._tasksQueue.splice(index, 1);
154
- };
155
- this._clearTimeout = spyOn(global, "clearTimeout").mockImplementation(clearTimerFactory("timeout" /* Timeout */));
156
- this._clearInterval = spyOn(global, "clearInterval").mockImplementation(clearTimerFactory("interval" /* Interval */));
157
- }
158
- useRealTimers() {
159
- this._isMocked = false;
160
- this.reset();
161
- global.setTimeout = originalSetTimeout;
162
- global.setInterval = originalSetInterval;
163
- global.clearTimeout = originalClearTimeout;
164
- global.clearInterval = originalClearInterval;
165
- }
166
- runOnlyPendingTimers() {
167
- this.assertMocked();
168
- this._isOnlyPending = true;
169
- this.runQueue();
170
- }
171
- runAllTimers() {
172
- this.assertMocked();
173
- this.runQueue();
174
- }
175
- advanceTimersByTime(ms) {
176
- this.assertMocked();
177
- this._advancedTime += ms;
178
- this.runQueue();
179
- }
180
- advanceTimersToNextTimer() {
181
- throw new Error("advanceTimersToNextTimer is not implemented");
182
- }
183
- runAllTicks() {
184
- throw new Error("runAllTicks is not implemented");
185
- }
186
- setSystemTime(now) {
187
- throw new Error("setSystemTime is not implemented");
188
- }
189
- getRealSystemTime() {
190
- return Date.now();
191
- }
192
- getTimerCount() {
193
- this.assertMocked();
194
- return this._tasksQueue.length;
195
- }
196
- reset() {
197
- var _a, _b, _c, _d;
198
- this._advancedTime = 0;
199
- this._nestedTime = {};
200
- this._isNested = false;
201
- this._isOnlyPending = false;
202
- this._spyid = 0;
203
- this._queueCount = 0;
204
- this._tasksQueue = [];
205
- (_a = this._clearInterval) == null ? void 0 : _a.mockRestore();
206
- (_b = this._clearTimeout) == null ? void 0 : _b.mockRestore();
207
- (_c = this._setInterval) == null ? void 0 : _c.mockRestore();
208
- (_d = this._setTimeout) == null ? void 0 : _d.mockRestore();
209
- }
210
- runQueue() {
211
- var _a, _b;
212
- let index = 0;
213
- while (this._tasksQueue[index]) {
214
- assertMaxLoop(this._queueCount);
215
- const task = this._tasksQueue[index];
216
- const { call, nested, type } = task;
217
- if (this._advancedTime && call.nestedMs > this._advancedTime)
218
- break;
219
- if (this._isOnlyPending && nested) {
220
- index++;
221
- continue;
222
- }
223
- this._scopeId = call.id;
224
- this._isNested = true;
225
- (_a = this._nestedTime)[_b = call.id] ?? (_a[_b] = 0);
226
- this._nestedTime[call.id] += call.ms;
227
- if (type === "timeout") {
228
- this.removeTask(index);
229
- } else if (type === "interval") {
230
- call.nestedMs += call.ms;
231
- const nestedMs = call.nestedMs;
232
- const closestTask = this._tasksQueue.findIndex(({ type: type2, call: call2 }) => type2 === "interval" && call2.nestedMs < nestedMs);
233
- if (closestTask !== -1 && closestTask !== index)
234
- this.ensureQueueOrder();
235
- }
236
- call.cb();
237
- this._queueCount++;
238
- }
239
- }
240
- removeTask(index) {
241
- if (index === 0)
242
- this._tasksQueue.shift();
243
- else
244
- this._tasksQueue.splice(index, 1);
245
- }
246
- pushTask(task) {
247
- this._tasksQueue.push(task);
248
- this.ensureQueueOrder();
249
- }
250
- ensureQueueOrder() {
251
- this._tasksQueue.sort((t1, t2) => {
252
- const diff = t1.call.nestedMs - t2.call.nestedMs;
253
- if (diff === 0) {
254
- if (t1.type === "immediate" /* Immediate */ && t2.type !== "immediate" /* Immediate */)
255
- return 1;
256
- return 0;
257
- }
258
- return diff;
259
- });
260
- }
261
- assertMocked() {
262
- assertEvery([
263
- this._isMocked,
264
- this._setTimeout,
265
- this._setInterval,
266
- this._clearTimeout,
267
- this._clearInterval
268
- ], 'timers are not mocked. try calling "vitest.useFakeTimers()" first');
269
- }
270
- }
271
-
272
- class VitestUtils {
273
- constructor() {
274
- this.spyOn = spyOn;
275
- this.fn = fn;
276
- this.mock = (path) => path;
277
- this._timers = new FakeTimers();
278
- }
279
- useFakeTimers() {
280
- return this._timers.useFakeTimers();
281
- }
282
- useRealTimers() {
283
- return this._timers.useRealTimers();
284
- }
285
- runOnlyPendingTimers() {
286
- return this._timers.runOnlyPendingTimers();
287
- }
288
- runAllTimers() {
289
- return this._timers.runAllTimers();
290
- }
291
- advanceTimersByTime(ms) {
292
- return this._timers.advanceTimersByTime(ms);
293
- }
294
- advanceTimersToNextTimer() {
295
- return this._timers.advanceTimersToNextTimer();
296
- }
297
- runAllTicks() {
298
- return this._timers.runAllTicks();
299
- }
300
- setSystemTime(time) {
301
- return this._timers.setSystemTime(time);
302
- }
303
- getRealSystemTime() {
304
- return this._timers.getRealSystemTime();
305
- }
306
- getTimerCount() {
307
- return this._timers.getTimerCount();
308
- }
309
- isMockFunction(fn2) {
310
- return typeof fn2 === "function" && "__isSpy" in fn2 && fn2.__isSpy;
311
- }
312
- clearAllMocks() {
313
- spies.forEach((spy) => {
314
- spy.reset();
315
- });
316
- }
317
- resetAllMocks() {
318
- spies.forEach((spy) => {
319
- spy.reset();
320
- });
321
- }
322
- restoreAllMocks() {
323
- spies.forEach((spy) => {
324
- spy.restore();
325
- });
326
- }
327
- }
328
- const vitest = new VitestUtils();
329
- const vi = vitest;
330
-
331
- var index = /*#__PURE__*/Object.freeze({
332
- __proto__: null,
333
- suite: suite,
334
- test: test,
335
- describe: describe,
336
- it: it,
337
- beforeAll: beforeAll,
338
- afterAll: afterAll,
339
- beforeEach: beforeEach,
340
- afterEach: afterEach,
341
- assert: assert,
342
- should: should,
343
- expect: expect,
344
- chai: chai,
345
- spyOn: spyOn,
346
- fn: fn,
347
- vitest: vitest,
348
- vi: vi
349
- });
350
-
351
- export { afterAll as a, beforeAll as b, beforeEach as c, afterEach as d, vi as e, fn as f, index as i, spyOn as s, vitest as v };