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/worker.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as slash, m as resolve, j as dirname$2, a as spyOn, q as mergeSlashes, s as spies, k as basename$2 } from './utils-49e5008c.js';
|
|
2
2
|
import { n as nanoid } from './index-9e71c815.js';
|
|
3
|
-
import { c as distDir } from './constants-
|
|
3
|
+
import { c as distDir } from './constants-c4dc2ff5.js';
|
|
4
4
|
import { builtinModules, createRequire } from 'module';
|
|
5
5
|
import { pathToFileURL, fileURLToPath as fileURLToPath$2, URL as URL$1 } from 'url';
|
|
6
|
+
import fs, { promises, realpathSync, statSync, Stats, readdirSync, existsSync } from 'fs';
|
|
6
7
|
import vm from 'vm';
|
|
7
8
|
import path from 'path';
|
|
8
|
-
import fs, { promises, realpathSync, statSync, Stats } from 'fs';
|
|
9
9
|
import assert from 'assert';
|
|
10
10
|
import { format as format$2, inspect } from 'util';
|
|
11
11
|
import { s as send } from './rpc-7de86f29.js';
|
|
12
12
|
import 'tty';
|
|
13
13
|
import 'local-pkg';
|
|
14
|
+
import 'chai';
|
|
15
|
+
import 'tinyspy';
|
|
14
16
|
|
|
15
17
|
const BUILTIN_MODULES$1 = new Set(builtinModules);
|
|
16
18
|
function normalizeSlash$1(str) {
|
|
@@ -9197,6 +9199,22 @@ async function isValidNodeImport(id, _opts = {}) {
|
|
|
9197
9199
|
return hasCJSSyntax(code) || !hasESMSyntax(code);
|
|
9198
9200
|
}
|
|
9199
9201
|
|
|
9202
|
+
var __defProp = Object.defineProperty;
|
|
9203
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9204
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9205
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9206
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9207
|
+
var __spreadValues = (a, b) => {
|
|
9208
|
+
for (var prop in b || (b = {}))
|
|
9209
|
+
if (__hasOwnProp.call(b, prop))
|
|
9210
|
+
__defNormalProp(a, prop, b[prop]);
|
|
9211
|
+
if (__getOwnPropSymbols)
|
|
9212
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
9213
|
+
if (__propIsEnum.call(b, prop))
|
|
9214
|
+
__defNormalProp(a, prop, b[prop]);
|
|
9215
|
+
}
|
|
9216
|
+
return a;
|
|
9217
|
+
};
|
|
9200
9218
|
const defaultInline = [
|
|
9201
9219
|
"vitest/dist",
|
|
9202
9220
|
/virtual:/,
|
|
@@ -9244,18 +9262,84 @@ async function interpretedImport(path, interpretDefault) {
|
|
|
9244
9262
|
}
|
|
9245
9263
|
return mod;
|
|
9246
9264
|
}
|
|
9265
|
+
function resolveMockPath(mockPath, root, nmName) {
|
|
9266
|
+
if (nmName) {
|
|
9267
|
+
const mockFolder = resolve(root, "__mocks__");
|
|
9268
|
+
const files = readdirSync(mockFolder);
|
|
9269
|
+
for (const file of files) {
|
|
9270
|
+
const [basename2] = file.split(".");
|
|
9271
|
+
if (basename2 === nmName)
|
|
9272
|
+
return resolve(mockFolder, file).replace(root, "");
|
|
9273
|
+
}
|
|
9274
|
+
return null;
|
|
9275
|
+
}
|
|
9276
|
+
const dir = dirname$2(mockPath);
|
|
9277
|
+
const baseId = basename$2(mockPath);
|
|
9278
|
+
const fullPath = resolve(dir, "__mocks__", baseId);
|
|
9279
|
+
return existsSync(fullPath) ? fullPath.replace(root, "") : null;
|
|
9280
|
+
}
|
|
9281
|
+
function mockObject(obj) {
|
|
9282
|
+
const newObj = __spreadValues({}, obj);
|
|
9283
|
+
for (const k in obj) {
|
|
9284
|
+
newObj[k] = obj[k];
|
|
9285
|
+
if (typeof obj[k] === "function" && !obj[k].__isSpy)
|
|
9286
|
+
spyOn(newObj, k);
|
|
9287
|
+
}
|
|
9288
|
+
return newObj;
|
|
9289
|
+
}
|
|
9247
9290
|
async function executeInViteNode(options) {
|
|
9248
|
-
const { moduleCache, root, files, fetch } = options;
|
|
9291
|
+
const { moduleCache, root, files, fetch, mockMap } = options;
|
|
9249
9292
|
const externalCache = /* @__PURE__ */ new Map();
|
|
9250
9293
|
builtinModules.forEach((m) => externalCache.set(m, true));
|
|
9251
9294
|
const result = [];
|
|
9252
9295
|
for (const file of files)
|
|
9253
9296
|
result.push(await cachedRequest(`/@fs/${slash(resolve(file))}`, []));
|
|
9254
9297
|
return result;
|
|
9298
|
+
function getSuiteFilepath() {
|
|
9299
|
+
var _a;
|
|
9300
|
+
return (_a = process.__vitest_worker__) == null ? void 0 : _a.filepath;
|
|
9301
|
+
}
|
|
9302
|
+
function getActualPath(path, nmName) {
|
|
9303
|
+
return nmName ? mergeSlashes(`/@fs/${path}`) : path.replace(root, "");
|
|
9304
|
+
}
|
|
9305
|
+
function unmockPath(path, nmName) {
|
|
9306
|
+
const suitefile = getSuiteFilepath();
|
|
9307
|
+
if (suitefile) {
|
|
9308
|
+
const fsPath = getActualPath(path, nmName);
|
|
9309
|
+
mockMap[suitefile] ?? (mockMap[suitefile] = {});
|
|
9310
|
+
delete mockMap[suitefile][fsPath];
|
|
9311
|
+
}
|
|
9312
|
+
}
|
|
9313
|
+
function mockPath(path, nmName) {
|
|
9314
|
+
const suitefile = getSuiteFilepath();
|
|
9315
|
+
if (suitefile) {
|
|
9316
|
+
const mockPath2 = resolveMockPath(path, root, nmName);
|
|
9317
|
+
const fsPath = getActualPath(path, nmName);
|
|
9318
|
+
mockMap[suitefile] ?? (mockMap[suitefile] = {});
|
|
9319
|
+
mockMap[suitefile][fsPath] = mockPath2;
|
|
9320
|
+
}
|
|
9321
|
+
}
|
|
9322
|
+
function clearMocks({ clearMocks: clearMocks2, mockReset, restoreMocks }) {
|
|
9323
|
+
if (!clearMocks2 && !mockReset && !restoreMocks)
|
|
9324
|
+
return;
|
|
9325
|
+
spies.forEach((s) => {
|
|
9326
|
+
if (restoreMocks)
|
|
9327
|
+
s.mockRestore();
|
|
9328
|
+
else if (mockReset)
|
|
9329
|
+
s.mockReset();
|
|
9330
|
+
else if (clearMocks2)
|
|
9331
|
+
s.mockClear();
|
|
9332
|
+
});
|
|
9333
|
+
}
|
|
9255
9334
|
async function directRequest(id, fsPath, callstack) {
|
|
9256
9335
|
callstack = [...callstack, id];
|
|
9257
|
-
const
|
|
9336
|
+
const suite = getSuiteFilepath();
|
|
9337
|
+
const request = async (dep, canMock = true) => {
|
|
9258
9338
|
var _a;
|
|
9339
|
+
const mocks2 = mockMap[suite || ""] || {};
|
|
9340
|
+
const mock = mocks2[dep];
|
|
9341
|
+
if (mock && canMock)
|
|
9342
|
+
dep = mock;
|
|
9259
9343
|
if (callstack.includes(dep)) {
|
|
9260
9344
|
const cacheKey = toFilePath(dep, root);
|
|
9261
9345
|
if (!((_a = moduleCache.get(cacheKey)) == null ? void 0 : _a.exports))
|
|
@@ -9284,12 +9368,28 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
|
|
|
9284
9368
|
return exports.default;
|
|
9285
9369
|
}
|
|
9286
9370
|
};
|
|
9371
|
+
const importActual = (path, nmName) => {
|
|
9372
|
+
return request(getActualPath(path, nmName), false);
|
|
9373
|
+
};
|
|
9374
|
+
const importMock = async (path, nmName) => {
|
|
9375
|
+
const mockPath2 = resolveMockPath(path, root, nmName);
|
|
9376
|
+
if (mockPath2 === null) {
|
|
9377
|
+
const exports2 = await request(getActualPath(path, nmName), false);
|
|
9378
|
+
return mockObject(exports2);
|
|
9379
|
+
}
|
|
9380
|
+
return request(mockPath2, true);
|
|
9381
|
+
};
|
|
9287
9382
|
const context = {
|
|
9288
9383
|
__vite_ssr_import__: request,
|
|
9289
9384
|
__vite_ssr_dynamic_import__: request,
|
|
9290
9385
|
__vite_ssr_exports__: exports,
|
|
9291
9386
|
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
9292
9387
|
__vite_ssr_import_meta__: { url },
|
|
9388
|
+
__vitest__mock__: mockPath,
|
|
9389
|
+
__vitest__unmock__: unmockPath,
|
|
9390
|
+
__vitest__importActual__: importActual,
|
|
9391
|
+
__vitest__importMock__: importMock,
|
|
9392
|
+
__vitest__clearMocks__: clearMocks,
|
|
9293
9393
|
require: createRequire(url),
|
|
9294
9394
|
exports,
|
|
9295
9395
|
module: moduleProxy,
|
|
@@ -9302,6 +9402,11 @@ ${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
|
|
|
9302
9402
|
lineOffset: 0
|
|
9303
9403
|
});
|
|
9304
9404
|
await fn(...Object.values(context));
|
|
9405
|
+
const mocks = suite ? mockMap[suite] : null;
|
|
9406
|
+
if (mocks) {
|
|
9407
|
+
if (mocks[id] === null)
|
|
9408
|
+
exportAll(exports, mockObject(exports));
|
|
9409
|
+
}
|
|
9305
9410
|
return exports;
|
|
9306
9411
|
}
|
|
9307
9412
|
function setCache(id, mod) {
|
|
@@ -9387,6 +9492,7 @@ function patchWindowsImportPath(path) {
|
|
|
9387
9492
|
|
|
9388
9493
|
let _viteNode;
|
|
9389
9494
|
const moduleCache = /* @__PURE__ */ new Map();
|
|
9495
|
+
const mockMap = {};
|
|
9390
9496
|
async function startViteNode(ctx) {
|
|
9391
9497
|
if (_viteNode)
|
|
9392
9498
|
return _viteNode;
|
|
@@ -9410,7 +9516,8 @@ async function startViteNode(ctx) {
|
|
|
9410
9516
|
inline: config.depsInline,
|
|
9411
9517
|
external: config.depsExternal,
|
|
9412
9518
|
interpretDefault: config.interpretDefault,
|
|
9413
|
-
moduleCache
|
|
9519
|
+
moduleCache,
|
|
9520
|
+
mockMap
|
|
9414
9521
|
}))[0];
|
|
9415
9522
|
_viteNode = { run: run2, collect: collect2 };
|
|
9416
9523
|
return _viteNode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.104",
|
|
4
4
|
"description": "A blazing fast unit test framework powered by Vite",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vite",
|
|
@@ -73,6 +73,7 @@
|
|
|
73
73
|
"happy-dom": "^2.25.0",
|
|
74
74
|
"jsdom": "^19.0.0",
|
|
75
75
|
"log-update": "^5.0.0",
|
|
76
|
+
"magic-string": "^0.25.7",
|
|
76
77
|
"micromatch": "^4.0.4",
|
|
77
78
|
"mlly": "^0.3.16",
|
|
78
79
|
"nanoid": "^3.1.30",
|
package/dist/index-a190f5a1.js
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
import { g as getCurrentSuite, w as withTimeout, a as getDefaultHookTimeout, s as suite, t as test, d as describe, i as it } from './suite-64b3e636.js';
|
|
2
|
-
import chai, { util, assert, should, expect } from 'chai';
|
|
3
|
-
import * as tinyspy from 'tinyspy';
|
|
4
|
-
import { spies } from 'tinyspy';
|
|
5
|
-
|
|
6
|
-
const beforeAll = (fn, timeout) => getCurrentSuite().on("beforeAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
|
|
7
|
-
const afterAll = (fn, timeout) => getCurrentSuite().on("afterAll", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
|
|
8
|
-
const beforeEach = (fn, timeout) => getCurrentSuite().on("beforeEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
|
|
9
|
-
const afterEach = (fn, timeout) => getCurrentSuite().on("afterEach", withTimeout(fn, timeout ?? getDefaultHookTimeout()));
|
|
10
|
-
|
|
11
|
-
function spyOn(obj, method, accessType) {
|
|
12
|
-
const dictionary = {
|
|
13
|
-
get: "getter",
|
|
14
|
-
set: "setter"
|
|
15
|
-
};
|
|
16
|
-
const objMethod = accessType ? { [dictionary[accessType]]: method } : method;
|
|
17
|
-
const stub = tinyspy.spyOn(obj, objMethod);
|
|
18
|
-
return enhanceSpy(stub);
|
|
19
|
-
}
|
|
20
|
-
function enhanceSpy(spy) {
|
|
21
|
-
const stub = spy;
|
|
22
|
-
let implementation;
|
|
23
|
-
const instances = [];
|
|
24
|
-
const mockContext = {
|
|
25
|
-
get calls() {
|
|
26
|
-
return stub.calls;
|
|
27
|
-
},
|
|
28
|
-
get instances() {
|
|
29
|
-
return instances;
|
|
30
|
-
},
|
|
31
|
-
get invocationCallOrder() {
|
|
32
|
-
return [];
|
|
33
|
-
},
|
|
34
|
-
get results() {
|
|
35
|
-
return stub.results.map(([callType, value]) => {
|
|
36
|
-
const type = callType === "error" ? "throw" : "return";
|
|
37
|
-
return { type, value };
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
let onceImplementations = [];
|
|
42
|
-
let name = "";
|
|
43
|
-
Object.defineProperty(stub, "name", {
|
|
44
|
-
get: () => name
|
|
45
|
-
});
|
|
46
|
-
stub.getMockName = () => name || "vi.fn()";
|
|
47
|
-
stub.mockName = (n) => {
|
|
48
|
-
name = n;
|
|
49
|
-
return stub;
|
|
50
|
-
};
|
|
51
|
-
stub.mockClear = () => {
|
|
52
|
-
stub.reset();
|
|
53
|
-
return stub;
|
|
54
|
-
};
|
|
55
|
-
stub.mockReset = () => {
|
|
56
|
-
stub.reset();
|
|
57
|
-
return stub;
|
|
58
|
-
};
|
|
59
|
-
stub.mockRestore = () => {
|
|
60
|
-
implementation = void 0;
|
|
61
|
-
onceImplementations = [];
|
|
62
|
-
stub.reset();
|
|
63
|
-
stub.restore();
|
|
64
|
-
return stub;
|
|
65
|
-
};
|
|
66
|
-
stub.getMockImplementation = () => implementation;
|
|
67
|
-
stub.mockImplementation = (fn2) => {
|
|
68
|
-
implementation = fn2;
|
|
69
|
-
return stub;
|
|
70
|
-
};
|
|
71
|
-
stub.mockImplementationOnce = (fn2) => {
|
|
72
|
-
onceImplementations.push(fn2);
|
|
73
|
-
return stub;
|
|
74
|
-
};
|
|
75
|
-
stub.mockReturnThis = () => stub.mockImplementation(function() {
|
|
76
|
-
return this;
|
|
77
|
-
});
|
|
78
|
-
stub.mockReturnValue = (val) => stub.mockImplementation(() => val);
|
|
79
|
-
stub.mockReturnValueOnce = (val) => stub.mockImplementationOnce(() => val);
|
|
80
|
-
stub.mockResolvedValue = (val) => stub.mockImplementation(() => Promise.resolve(val));
|
|
81
|
-
stub.mockResolvedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.resolve(val));
|
|
82
|
-
stub.mockRejectedValue = (val) => stub.mockImplementation(() => Promise.reject(val));
|
|
83
|
-
stub.mockRejectedValueOnce = (val) => stub.mockImplementation(() => Promise.reject(val));
|
|
84
|
-
util.addProperty(stub, "mock", () => mockContext);
|
|
85
|
-
stub.willCall(function(...args) {
|
|
86
|
-
instances.push(this);
|
|
87
|
-
const impl = onceImplementations.shift() || implementation || stub.getOriginal() || (() => {
|
|
88
|
-
});
|
|
89
|
-
return impl.apply(this, args);
|
|
90
|
-
});
|
|
91
|
-
return stub;
|
|
92
|
-
}
|
|
93
|
-
function fn(implementation) {
|
|
94
|
-
return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {
|
|
95
|
-
}) }, "fn"));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const originalSetTimeout = global.setTimeout;
|
|
99
|
-
const originalSetInterval = global.setInterval;
|
|
100
|
-
const originalClearTimeout = global.clearTimeout;
|
|
101
|
-
const originalClearInterval = global.clearInterval;
|
|
102
|
-
const MAX_LOOPS = 1e4;
|
|
103
|
-
const assertEvery = (assertions, message) => {
|
|
104
|
-
if (assertions.some((a) => !a))
|
|
105
|
-
throw new Error(message);
|
|
106
|
-
};
|
|
107
|
-
const assertMaxLoop = (times) => {
|
|
108
|
-
if (times >= MAX_LOOPS)
|
|
109
|
-
throw new Error("setTimeout/setInterval called 10 000 times. It's possible it stuck in an infinite loop.");
|
|
110
|
-
};
|
|
111
|
-
const getNodeTimeout = (id) => {
|
|
112
|
-
const timer = {
|
|
113
|
-
ref: () => timer,
|
|
114
|
-
unref: () => timer,
|
|
115
|
-
hasRef: () => true,
|
|
116
|
-
refresh: () => timer,
|
|
117
|
-
[Symbol.toPrimitive]: () => id
|
|
118
|
-
};
|
|
119
|
-
return timer;
|
|
120
|
-
};
|
|
121
|
-
class FakeTimers {
|
|
122
|
-
constructor() {
|
|
123
|
-
this._advancedTime = 0;
|
|
124
|
-
this._nestedTime = {};
|
|
125
|
-
this._scopeId = 0;
|
|
126
|
-
this._isNested = false;
|
|
127
|
-
this._isOnlyPending = false;
|
|
128
|
-
this._spyid = 0;
|
|
129
|
-
this._isMocked = false;
|
|
130
|
-
this._tasksQueue = [];
|
|
131
|
-
this._queueCount = 0;
|
|
132
|
-
}
|
|
133
|
-
useFakeTimers() {
|
|
134
|
-
this._isMocked = true;
|
|
135
|
-
this.reset();
|
|
136
|
-
const spyFactory = (spyType, resultBuilder) => {
|
|
137
|
-
return (cb, ms = 0) => {
|
|
138
|
-
const id = ++this._spyid;
|
|
139
|
-
const nestedTo = Object.entries(this._nestedTime).filter(([key]) => Number(key) <= this._scopeId);
|
|
140
|
-
const nestedMs = nestedTo.reduce((total, [, ms2]) => total + ms2, ms);
|
|
141
|
-
const call = { id, cb, ms, nestedMs, scopeId: this._scopeId };
|
|
142
|
-
const task = { type: spyType, call, nested: this._isNested };
|
|
143
|
-
this.pushTask(task);
|
|
144
|
-
return resultBuilder(id, cb);
|
|
145
|
-
};
|
|
146
|
-
};
|
|
147
|
-
this._setTimeout = spyOn(global, "setTimeout").mockImplementation(spyFactory("timeout" /* Timeout */, getNodeTimeout));
|
|
148
|
-
this._setInterval = spyOn(global, "setInterval").mockImplementation(spyFactory("interval" /* Interval */, getNodeTimeout));
|
|
149
|
-
const clearTimerFactory = (spyType) => (id) => {
|
|
150
|
-
if (id === void 0)
|
|
151
|
-
return;
|
|
152
|
-
const index = this._tasksQueue.findIndex(({ call, type }) => type === spyType && call.id === Number(id));
|
|
153
|
-
if (index !== -1)
|
|
154
|
-
this._tasksQueue.splice(index, 1);
|
|
155
|
-
};
|
|
156
|
-
this._clearTimeout = spyOn(global, "clearTimeout").mockImplementation(clearTimerFactory("timeout" /* Timeout */));
|
|
157
|
-
this._clearInterval = spyOn(global, "clearInterval").mockImplementation(clearTimerFactory("interval" /* Interval */));
|
|
158
|
-
}
|
|
159
|
-
useRealTimers() {
|
|
160
|
-
this._isMocked = false;
|
|
161
|
-
this.reset();
|
|
162
|
-
global.setTimeout = originalSetTimeout;
|
|
163
|
-
global.setInterval = originalSetInterval;
|
|
164
|
-
global.clearTimeout = originalClearTimeout;
|
|
165
|
-
global.clearInterval = originalClearInterval;
|
|
166
|
-
}
|
|
167
|
-
runOnlyPendingTimers() {
|
|
168
|
-
this.assertMocked();
|
|
169
|
-
this._isOnlyPending = true;
|
|
170
|
-
this.runQueue();
|
|
171
|
-
}
|
|
172
|
-
runAllTimers() {
|
|
173
|
-
this.assertMocked();
|
|
174
|
-
this.runQueue();
|
|
175
|
-
}
|
|
176
|
-
advanceTimersByTime(ms) {
|
|
177
|
-
this.assertMocked();
|
|
178
|
-
this._advancedTime += ms;
|
|
179
|
-
this.runQueue();
|
|
180
|
-
}
|
|
181
|
-
advanceTimersToNextTimer() {
|
|
182
|
-
throw new Error("advanceTimersToNextTimer is not implemented");
|
|
183
|
-
}
|
|
184
|
-
runAllTicks() {
|
|
185
|
-
throw new Error("runAllTicks is not implemented");
|
|
186
|
-
}
|
|
187
|
-
setSystemTime(now) {
|
|
188
|
-
throw new Error("setSystemTime is not implemented");
|
|
189
|
-
}
|
|
190
|
-
getRealSystemTime() {
|
|
191
|
-
return Date.now();
|
|
192
|
-
}
|
|
193
|
-
getTimerCount() {
|
|
194
|
-
this.assertMocked();
|
|
195
|
-
return this._tasksQueue.length;
|
|
196
|
-
}
|
|
197
|
-
reset() {
|
|
198
|
-
var _a, _b, _c, _d;
|
|
199
|
-
this._advancedTime = 0;
|
|
200
|
-
this._nestedTime = {};
|
|
201
|
-
this._isNested = false;
|
|
202
|
-
this._isOnlyPending = false;
|
|
203
|
-
this._spyid = 0;
|
|
204
|
-
this._queueCount = 0;
|
|
205
|
-
this._tasksQueue = [];
|
|
206
|
-
(_a = this._clearInterval) == null ? void 0 : _a.mockRestore();
|
|
207
|
-
(_b = this._clearTimeout) == null ? void 0 : _b.mockRestore();
|
|
208
|
-
(_c = this._setInterval) == null ? void 0 : _c.mockRestore();
|
|
209
|
-
(_d = this._setTimeout) == null ? void 0 : _d.mockRestore();
|
|
210
|
-
}
|
|
211
|
-
runQueue() {
|
|
212
|
-
var _a, _b;
|
|
213
|
-
let index = 0;
|
|
214
|
-
while (this._tasksQueue[index]) {
|
|
215
|
-
assertMaxLoop(this._queueCount);
|
|
216
|
-
const task = this._tasksQueue[index];
|
|
217
|
-
const { call, nested, type } = task;
|
|
218
|
-
if (this._advancedTime && call.nestedMs > this._advancedTime)
|
|
219
|
-
break;
|
|
220
|
-
if (this._isOnlyPending && nested) {
|
|
221
|
-
index++;
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
this._scopeId = call.id;
|
|
225
|
-
this._isNested = true;
|
|
226
|
-
(_a = this._nestedTime)[_b = call.id] ?? (_a[_b] = 0);
|
|
227
|
-
this._nestedTime[call.id] += call.ms;
|
|
228
|
-
if (type === "timeout") {
|
|
229
|
-
this.removeTask(index);
|
|
230
|
-
} else if (type === "interval") {
|
|
231
|
-
call.nestedMs += call.ms;
|
|
232
|
-
const nestedMs = call.nestedMs;
|
|
233
|
-
const closestTask = this._tasksQueue.findIndex(({ type: type2, call: call2 }) => type2 === "interval" && call2.nestedMs < nestedMs);
|
|
234
|
-
if (closestTask !== -1 && closestTask !== index)
|
|
235
|
-
this.ensureQueueOrder();
|
|
236
|
-
}
|
|
237
|
-
call.cb();
|
|
238
|
-
this._queueCount++;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
removeTask(index) {
|
|
242
|
-
if (index === 0)
|
|
243
|
-
this._tasksQueue.shift();
|
|
244
|
-
else
|
|
245
|
-
this._tasksQueue.splice(index, 1);
|
|
246
|
-
}
|
|
247
|
-
pushTask(task) {
|
|
248
|
-
this._tasksQueue.push(task);
|
|
249
|
-
this.ensureQueueOrder();
|
|
250
|
-
}
|
|
251
|
-
ensureQueueOrder() {
|
|
252
|
-
this._tasksQueue.sort((t1, t2) => {
|
|
253
|
-
const diff = t1.call.nestedMs - t2.call.nestedMs;
|
|
254
|
-
if (diff === 0) {
|
|
255
|
-
if (t1.type === "immediate" /* Immediate */ && t2.type !== "immediate" /* Immediate */)
|
|
256
|
-
return 1;
|
|
257
|
-
return 0;
|
|
258
|
-
}
|
|
259
|
-
return diff;
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
assertMocked() {
|
|
263
|
-
assertEvery([
|
|
264
|
-
this._isMocked,
|
|
265
|
-
this._setTimeout,
|
|
266
|
-
this._setInterval,
|
|
267
|
-
this._clearTimeout,
|
|
268
|
-
this._clearInterval
|
|
269
|
-
], 'timers are not mocked. try calling "vitest.useFakeTimers()" first');
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
class VitestUtils {
|
|
274
|
-
constructor() {
|
|
275
|
-
this.spyOn = spyOn;
|
|
276
|
-
this.fn = fn;
|
|
277
|
-
this.mock = (path) => path;
|
|
278
|
-
this._timers = new FakeTimers();
|
|
279
|
-
}
|
|
280
|
-
useFakeTimers() {
|
|
281
|
-
return this._timers.useFakeTimers();
|
|
282
|
-
}
|
|
283
|
-
useRealTimers() {
|
|
284
|
-
return this._timers.useRealTimers();
|
|
285
|
-
}
|
|
286
|
-
runOnlyPendingTimers() {
|
|
287
|
-
return this._timers.runOnlyPendingTimers();
|
|
288
|
-
}
|
|
289
|
-
runAllTimers() {
|
|
290
|
-
return this._timers.runAllTimers();
|
|
291
|
-
}
|
|
292
|
-
advanceTimersByTime(ms) {
|
|
293
|
-
return this._timers.advanceTimersByTime(ms);
|
|
294
|
-
}
|
|
295
|
-
advanceTimersToNextTimer() {
|
|
296
|
-
return this._timers.advanceTimersToNextTimer();
|
|
297
|
-
}
|
|
298
|
-
runAllTicks() {
|
|
299
|
-
return this._timers.runAllTicks();
|
|
300
|
-
}
|
|
301
|
-
setSystemTime(time) {
|
|
302
|
-
return this._timers.setSystemTime(time);
|
|
303
|
-
}
|
|
304
|
-
getRealSystemTime() {
|
|
305
|
-
return this._timers.getRealSystemTime();
|
|
306
|
-
}
|
|
307
|
-
getTimerCount() {
|
|
308
|
-
return this._timers.getTimerCount();
|
|
309
|
-
}
|
|
310
|
-
isMockFunction(fn2) {
|
|
311
|
-
return typeof fn2 === "function" && "__isSpy" in fn2 && fn2.__isSpy;
|
|
312
|
-
}
|
|
313
|
-
clearAllMocks() {
|
|
314
|
-
spies.forEach((spy) => {
|
|
315
|
-
spy.reset();
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
resetAllMocks() {
|
|
319
|
-
spies.forEach((spy) => {
|
|
320
|
-
spy.reset();
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
restoreAllMocks() {
|
|
324
|
-
spies.forEach((spy) => {
|
|
325
|
-
spy.restore();
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
const vitest = new VitestUtils();
|
|
330
|
-
const vi = vitest;
|
|
331
|
-
|
|
332
|
-
var index = /*#__PURE__*/Object.freeze({
|
|
333
|
-
__proto__: null,
|
|
334
|
-
suite: suite,
|
|
335
|
-
test: test,
|
|
336
|
-
describe: describe,
|
|
337
|
-
it: it,
|
|
338
|
-
beforeAll: beforeAll,
|
|
339
|
-
afterAll: afterAll,
|
|
340
|
-
beforeEach: beforeEach,
|
|
341
|
-
afterEach: afterEach,
|
|
342
|
-
assert: assert,
|
|
343
|
-
should: should,
|
|
344
|
-
expect: expect,
|
|
345
|
-
chai: chai,
|
|
346
|
-
spyOn: spyOn,
|
|
347
|
-
fn: fn,
|
|
348
|
-
vitest: vitest,
|
|
349
|
-
vi: vi
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
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 };
|