hypotes 0.1.0 → 0.2.0
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/README.md +293 -18
- package/dist/chain.d.ts +4 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +289 -0
- package/dist/index.d.ts +23 -10
- package/dist/index.js +422 -9
- package/dist/project-Cf9C54In.js +46 -0
- package/dist/project-filter.d.ts +14 -0
- package/dist/project.d.ts +17 -0
- package/dist/register.js +365 -10
- package/dist/reporter.d.ts +8 -0
- package/dist/reporter.js +293 -0
- package/dist/source-map.d.ts +45 -0
- package/dist/stubs.d.ts +35 -0
- package/dist/test-files.d.ts +31 -0
- package/dist/timers.d.ts +83 -0
- package/dist/wait.d.ts +47 -0
- package/package.json +24 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fn, isMockFunction, spyOn, type MaybeMocked, type MaybeMockedDeep, type MaybePartiallyMocked, type MaybePartiallyMockedDeep } from '@vitest/spy';
|
|
2
2
|
import { doMock, doUnmock, hoisted, importActual, mock, unmock } from './mocks.ts';
|
|
3
|
+
import { type StubsAPI } from './stubs.ts';
|
|
4
|
+
import { type TimersAPI } from './timers.ts';
|
|
5
|
+
import { type WaitAPI } from './wait.ts';
|
|
3
6
|
export { expect, type Expect } from './expect.ts';
|
|
4
7
|
export { afterAll, afterEach, beforeAll, beforeEach, type AfterHook, type Hook } from './hooks.ts';
|
|
5
8
|
export { describe, it, suite, test, type Declare, type Each, type For, type Modifiers, type SuiteAPI, type SuiteContext, type SuiteFunction, type TestAPI, type TestContext, type TestFunction, type TestOptions } from './tests.ts';
|
|
6
9
|
export type { MockFactoryWithHelper } from './mocks.ts';
|
|
10
|
+
export type { FakeTimersConfig } from './timers.ts';
|
|
7
11
|
export type { Mock, MockInstance } from '@vitest/spy';
|
|
8
12
|
/**
|
|
9
13
|
* Returns the value, typed as its mock: a type helper, for what `vi.mock()`
|
|
@@ -29,13 +33,8 @@ declare function mocked<T>(item: T, options: {
|
|
|
29
33
|
partial: true;
|
|
30
34
|
deep: true;
|
|
31
35
|
}): MaybePartiallyMockedDeep<T>;
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
* and its module mocks, which node preloads with `--import hypotes/register`.
|
|
35
|
-
* node:test's `mock` mocks timers.
|
|
36
|
-
*/
|
|
37
|
-
export declare const vi: {
|
|
38
|
-
clearAllMocks: typeof clearAllMocks;
|
|
36
|
+
/** The members of `vi` that mock functions, objects' methods, and modules */
|
|
37
|
+
declare const mocks: {
|
|
39
38
|
doMock: typeof doMock;
|
|
40
39
|
doUnmock: typeof doUnmock;
|
|
41
40
|
fn: typeof fn;
|
|
@@ -44,8 +43,22 @@ export declare const vi: {
|
|
|
44
43
|
isMockFunction: typeof isMockFunction;
|
|
45
44
|
mock: typeof mock;
|
|
46
45
|
mocked: typeof mocked;
|
|
47
|
-
resetAllMocks: typeof resetAllMocks;
|
|
48
|
-
restoreAllMocks: typeof restoreAllMocks;
|
|
49
46
|
spyOn: typeof spyOn;
|
|
50
47
|
unmock: typeof unmock;
|
|
51
48
|
};
|
|
49
|
+
/** `vi`, which the members that Vitest's return `vi` from return */
|
|
50
|
+
export interface Vi extends Readonly<typeof mocks>, StubsAPI<Vi>, TimersAPI<Vi>, WaitAPI {
|
|
51
|
+
/** Clears the calls and results of every mock */
|
|
52
|
+
clearAllMocks(): Vi;
|
|
53
|
+
/** Clears every mock, and resets its implementation to its original */
|
|
54
|
+
resetAllMocks(): Vi;
|
|
55
|
+
/** Restores what every `vi.spyOn()` replaced */
|
|
56
|
+
restoreAllMocks(): Vi;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Vitest's spies, which its matchers, such as `toHaveBeenCalledWith`, check,
|
|
60
|
+
* its module mocks, which node preloads with `--import hypotes/register`,
|
|
61
|
+
* its fake timers, its stubs of globals and environment variables, and its
|
|
62
|
+
* waits.
|
|
63
|
+
*/
|
|
64
|
+
export declare const vi: Vi;
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,414 @@
|
|
|
1
1
|
import { a as hoisted, c as unmock, i as doUnmock, o as importActual, r as doMock, s as mock } from "./mocks-ciQvBYVw.js";
|
|
2
2
|
import { a as suite, n as describe, o as test, r as it } from "./tests-D4v1ZaoR.js";
|
|
3
|
+
import { createRequire, syncBuiltinESMExports } from "node:module";
|
|
3
4
|
import { clearAllMocks, fn, isMockFunction, resetAllMocks, restoreAllMocks, spyOn } from "@vitest/spy";
|
|
5
|
+
import nodeTimers from "node:timers";
|
|
6
|
+
import nodeTimersPromises from "node:timers/promises";
|
|
7
|
+
import { getSafeTimers, setSafeTimers } from "@vitest/utils/timers";
|
|
4
8
|
import { diff, inspect, styleText } from "node:util";
|
|
5
9
|
import { JestAsymmetricMatchers, JestChaiExpect, JestExtend, addCustomEqualityTesters, arrayBufferEquality, chai, customMatchers, getCustomEqualityTesters, isAsymmetric, iterableEquality, sparseArrayEquality, typeEquality } from "@vitest/expect";
|
|
6
10
|
import { disableDefaultColors } from "tinyrainbow";
|
|
7
11
|
import { Buffer } from "node:buffer";
|
|
8
12
|
import * as nodeTest from "node:test";
|
|
13
|
+
// source/chain.ts
|
|
14
|
+
/** A member of `vi` that runs, and returns `vi` */
|
|
15
|
+
function chained(vi, run) {
|
|
16
|
+
return (...args) => {
|
|
17
|
+
run(...args);
|
|
18
|
+
return vi();
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** A member of `vi` that runs, and returns a promise of `vi` */
|
|
22
|
+
function chainedAsync(vi, run) {
|
|
23
|
+
return async (...args) => {
|
|
24
|
+
await run(...args);
|
|
25
|
+
return vi();
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// source/stubs.ts
|
|
29
|
+
const BOOLEAN_ENVS = /* @__PURE__ */ new Set([
|
|
30
|
+
"PROD",
|
|
31
|
+
"DEV",
|
|
32
|
+
"SSR"
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* The members of `vi` that stub globals and environment variables, which
|
|
36
|
+
* return what `vi` returns, for the calls that chain.
|
|
37
|
+
*/
|
|
38
|
+
function createStubsAPI(vi) {
|
|
39
|
+
/** Each stubbed global's descriptor before its first stub, if it had one */
|
|
40
|
+
const globals = /* @__PURE__ */ new Map();
|
|
41
|
+
/** Each stubbed variable's value before its first stub, if it was set */
|
|
42
|
+
const envs = /* @__PURE__ */ new Map();
|
|
43
|
+
function stubGlobal(name, value) {
|
|
44
|
+
if (!globals.has(name)) globals.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
|
|
45
|
+
Object.defineProperty(globalThis, name, {
|
|
46
|
+
value,
|
|
47
|
+
writable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
enumerable: true
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function stubEnv(name, value) {
|
|
53
|
+
if (!envs.has(name)) envs.set(name, process.env[name]);
|
|
54
|
+
if (value === void 0) delete process.env[name];
|
|
55
|
+
else if (BOOLEAN_ENVS.has(name)) process.env[name] = value === false || value === "" ? "" : "1";
|
|
56
|
+
else process.env[name] = String(value);
|
|
57
|
+
}
|
|
58
|
+
function unstubAllGlobals() {
|
|
59
|
+
for (const [name, original] of globals) if (original === void 0) Reflect.deleteProperty(globalThis, name);
|
|
60
|
+
else Object.defineProperty(globalThis, name, original);
|
|
61
|
+
globals.clear();
|
|
62
|
+
}
|
|
63
|
+
function unstubAllEnvs() {
|
|
64
|
+
for (const [name, original] of envs) if (original === void 0) delete process.env[name];
|
|
65
|
+
else process.env[name] = original;
|
|
66
|
+
envs.clear();
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
stubEnv: chained(vi, stubEnv),
|
|
70
|
+
stubGlobal: chained(vi, stubGlobal),
|
|
71
|
+
unstubAllEnvs: chained(vi, unstubAllEnvs),
|
|
72
|
+
unstubAllGlobals: chained(vi, unstubAllGlobals)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// source/timers.ts
|
|
76
|
+
/** Vitest's defaults for its configuration's `fakeTimers` */
|
|
77
|
+
const DEFAULT_CONFIG = {
|
|
78
|
+
loopLimit: 1e4,
|
|
79
|
+
shouldClearNativeTimers: true
|
|
80
|
+
};
|
|
81
|
+
/** The timers that Node runs on itself, which stay real unless they're named */
|
|
82
|
+
const NODE_INTERNAL = ["nextTick", "queueMicrotask"];
|
|
83
|
+
const require = createRequire(import.meta.url);
|
|
84
|
+
const realNow = Date.now;
|
|
85
|
+
/** Gives a Node module the methods that installing a clock replaced */
|
|
86
|
+
function restoreMethods(module, replaced = []) {
|
|
87
|
+
for (const { methodName, original } of replaced) Reflect.set(module, methodName, original);
|
|
88
|
+
}
|
|
89
|
+
/** The time as @sinonjs/fake-timers takes it, which reads no strings */
|
|
90
|
+
function toClockTime(time) {
|
|
91
|
+
const normalized = typeof time === "object" && "epochMilliseconds" in time ? time.epochMilliseconds : time;
|
|
92
|
+
return typeof normalized === "string" ? new Date(normalized) : normalized;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The fake clock of a process: Vitest's `FakeTimers`, which fakes either the
|
|
96
|
+
* timers and the date, once `useFakeTimers()` is called, or only the date,
|
|
97
|
+
* once `setSystemTime()` is called while the timers are real.
|
|
98
|
+
*/
|
|
99
|
+
var FakeTimers = class {
|
|
100
|
+
#context;
|
|
101
|
+
/** The clock that is installed, while the timers or the date are fake */
|
|
102
|
+
#clock;
|
|
103
|
+
#fakingTime = false;
|
|
104
|
+
/** The date that `setSystemTime()` set, while only the date is fake */
|
|
105
|
+
#fakingDate;
|
|
106
|
+
#fakingNodeBuiltins = false;
|
|
107
|
+
isFakeTimers() {
|
|
108
|
+
return this.#fakingTime;
|
|
109
|
+
}
|
|
110
|
+
useFakeTimers(config = {}) {
|
|
111
|
+
const now = this.#fakingDate ?? Date.now();
|
|
112
|
+
this.useRealTimers();
|
|
113
|
+
const userConfig = {
|
|
114
|
+
...DEFAULT_CONFIG,
|
|
115
|
+
...config
|
|
116
|
+
};
|
|
117
|
+
let { toFake, toNotFake } = userConfig;
|
|
118
|
+
if (toFake === void 0 && toNotFake === void 0) toFake = this.#fakeable().filter((timer) => !NODE_INTERNAL.includes(timer));
|
|
119
|
+
else if (toFake === void 0 && toNotFake !== void 0) toNotFake = [.../* @__PURE__ */ new Set([...toNotFake, ...NODE_INTERNAL])];
|
|
120
|
+
this.#install({
|
|
121
|
+
now,
|
|
122
|
+
...userConfig,
|
|
123
|
+
...toFake && { toFake },
|
|
124
|
+
...toNotFake && { toNotFake },
|
|
125
|
+
ignoreMissingTimers: true
|
|
126
|
+
});
|
|
127
|
+
this.#fakingTime = true;
|
|
128
|
+
}
|
|
129
|
+
useRealTimers() {
|
|
130
|
+
if (this.#clock === void 0) return;
|
|
131
|
+
this.#clock.uninstall();
|
|
132
|
+
if (this.#fakingNodeBuiltins) syncBuiltinESMExports();
|
|
133
|
+
this.#clock = void 0;
|
|
134
|
+
this.#fakingTime = false;
|
|
135
|
+
this.#fakingDate = void 0;
|
|
136
|
+
this.#fakingNodeBuiltins = false;
|
|
137
|
+
}
|
|
138
|
+
runOnlyPendingTimers() {
|
|
139
|
+
this.#fakeClock().runToLast();
|
|
140
|
+
}
|
|
141
|
+
async runOnlyPendingTimersAsync() {
|
|
142
|
+
await this.#fakeClock().runToLastAsync();
|
|
143
|
+
}
|
|
144
|
+
runAllTimers() {
|
|
145
|
+
this.#fakeClock().runAll();
|
|
146
|
+
}
|
|
147
|
+
async runAllTimersAsync() {
|
|
148
|
+
await this.#fakeClock().runAllAsync();
|
|
149
|
+
}
|
|
150
|
+
runAllTicks() {
|
|
151
|
+
this.#fakeClock().runMicrotasks();
|
|
152
|
+
}
|
|
153
|
+
advanceTimersByTime(ms) {
|
|
154
|
+
this.#fakeClock().tick(ms);
|
|
155
|
+
}
|
|
156
|
+
async advanceTimersByTimeAsync(ms) {
|
|
157
|
+
await this.#fakeClock().tickAsync(ms);
|
|
158
|
+
}
|
|
159
|
+
advanceTimersToNextTimer() {
|
|
160
|
+
const clock = this.#fakeClock();
|
|
161
|
+
clock.next();
|
|
162
|
+
clock.tick(0);
|
|
163
|
+
}
|
|
164
|
+
async advanceTimersToNextTimerAsync() {
|
|
165
|
+
const clock = this.#fakeClock();
|
|
166
|
+
await clock.nextAsync();
|
|
167
|
+
clock.tick(0);
|
|
168
|
+
}
|
|
169
|
+
advanceTimersToNextFrame() {
|
|
170
|
+
this.#fakeClock().runToFrame();
|
|
171
|
+
}
|
|
172
|
+
getTimerCount() {
|
|
173
|
+
return this.#fakeClock().countTimers();
|
|
174
|
+
}
|
|
175
|
+
clearAllTimers() {
|
|
176
|
+
if (this.#fakingTime) this.#clock?.reset();
|
|
177
|
+
}
|
|
178
|
+
setSystemTime(time) {
|
|
179
|
+
const date = toClockTime(time);
|
|
180
|
+
if (this.#fakingTime) {
|
|
181
|
+
this.#clock?.setSystemTime(date);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const fakingDate = date === void 0 ? new Date(realNow()) : new Date(date);
|
|
185
|
+
if (this.#clock === void 0) this.#install({
|
|
186
|
+
now: fakingDate,
|
|
187
|
+
toFake: ["Date", "Temporal"],
|
|
188
|
+
ignoreMissingTimers: true
|
|
189
|
+
});
|
|
190
|
+
else this.#clock.setSystemTime(fakingDate);
|
|
191
|
+
this.#fakingDate = fakingDate;
|
|
192
|
+
}
|
|
193
|
+
getMockedSystemTime() {
|
|
194
|
+
if (this.#fakingTime && this.#clock !== void 0) return new Date(this.#clock.now);
|
|
195
|
+
return this.#fakingDate ?? null;
|
|
196
|
+
}
|
|
197
|
+
getRealSystemTime() {
|
|
198
|
+
return realNow();
|
|
199
|
+
}
|
|
200
|
+
setTimerTickMode(mode, interval) {
|
|
201
|
+
const clock = this.#fakeClock();
|
|
202
|
+
switch (mode) {
|
|
203
|
+
case "manual":
|
|
204
|
+
clock.setTickMode({ mode: "manual" });
|
|
205
|
+
break;
|
|
206
|
+
case "nextTimerAsync":
|
|
207
|
+
clock.setTickMode({ mode: "nextAsync" });
|
|
208
|
+
break;
|
|
209
|
+
case "interval":
|
|
210
|
+
clock.setTickMode({
|
|
211
|
+
mode: "interval",
|
|
212
|
+
delta: interval
|
|
213
|
+
});
|
|
214
|
+
break;
|
|
215
|
+
default: throw new Error(`Invalid tick mode: ${String(mode)}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** The clock, for the members that need the timers to be fake */
|
|
219
|
+
#fakeClock() {
|
|
220
|
+
if (!this.#fakingTime || this.#clock === void 0) throw new Error("A function to advance timers was called but the timers APIs are not mocked. Call `vi.useFakeTimers()` in the test file first.");
|
|
221
|
+
return this.#clock;
|
|
222
|
+
}
|
|
223
|
+
/** What @sinonjs/fake-timers can fake */
|
|
224
|
+
#fakeable() {
|
|
225
|
+
return Object.keys(this.#loadContext().timers);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Installs a clock, and leaves the exports of Node's timers modules as
|
|
229
|
+
* `fakeNodeBuiltins` says: fake, in their ES exports too, or real.
|
|
230
|
+
*/
|
|
231
|
+
#install({ fakeNodeBuiltins = false, ...config }) {
|
|
232
|
+
const clock = this.#loadContext().install(config);
|
|
233
|
+
if (fakeNodeBuiltins) syncBuiltinESMExports();
|
|
234
|
+
else {
|
|
235
|
+
restoreMethods(nodeTimers, clock.timersModuleMethods);
|
|
236
|
+
restoreMethods(nodeTimersPromises, clock.timersPromisesModuleMethods);
|
|
237
|
+
clock.timersModuleMethods = void 0;
|
|
238
|
+
clock.timersPromisesModuleMethods = void 0;
|
|
239
|
+
}
|
|
240
|
+
this.#clock = clock;
|
|
241
|
+
this.#fakingNodeBuiltins = fakeNodeBuiltins;
|
|
242
|
+
}
|
|
243
|
+
/** @sinonjs/fake-timers, which loads once a test fakes the timers or the date */
|
|
244
|
+
#loadContext() {
|
|
245
|
+
if (this.#context === void 0) {
|
|
246
|
+
const { withGlobal } = require("@sinonjs/fake-timers");
|
|
247
|
+
this.#context = withGlobal(globalThis);
|
|
248
|
+
}
|
|
249
|
+
return this.#context;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* The members of `vi` that fake timers, which return what `vi` returns, for
|
|
254
|
+
* the calls that chain.
|
|
255
|
+
*/
|
|
256
|
+
function createTimersAPI(vi) {
|
|
257
|
+
const timers = new FakeTimers();
|
|
258
|
+
return {
|
|
259
|
+
useFakeTimers: chained(vi, (config) => timers.useFakeTimers(config)),
|
|
260
|
+
useRealTimers: chained(vi, () => timers.useRealTimers()),
|
|
261
|
+
isFakeTimers: () => timers.isFakeTimers(),
|
|
262
|
+
runOnlyPendingTimers: chained(vi, () => timers.runOnlyPendingTimers()),
|
|
263
|
+
runOnlyPendingTimersAsync: chainedAsync(vi, () => timers.runOnlyPendingTimersAsync()),
|
|
264
|
+
runAllTimers: chained(vi, () => timers.runAllTimers()),
|
|
265
|
+
runAllTimersAsync: chainedAsync(vi, () => timers.runAllTimersAsync()),
|
|
266
|
+
runAllTicks: chained(vi, () => timers.runAllTicks()),
|
|
267
|
+
advanceTimersByTime: chained(vi, (ms) => timers.advanceTimersByTime(ms)),
|
|
268
|
+
advanceTimersByTimeAsync: chainedAsync(vi, (ms) => timers.advanceTimersByTimeAsync(ms)),
|
|
269
|
+
advanceTimersToNextTimer: chained(vi, () => timers.advanceTimersToNextTimer()),
|
|
270
|
+
advanceTimersToNextTimerAsync: chainedAsync(vi, () => timers.advanceTimersToNextTimerAsync()),
|
|
271
|
+
advanceTimersToNextFrame: chained(vi, () => timers.advanceTimersToNextFrame()),
|
|
272
|
+
getTimerCount: () => timers.getTimerCount(),
|
|
273
|
+
clearAllTimers: chained(vi, () => timers.clearAllTimers()),
|
|
274
|
+
setSystemTime: chained(vi, (time) => timers.setSystemTime(time)),
|
|
275
|
+
getMockedSystemTime: () => timers.getMockedSystemTime(),
|
|
276
|
+
getRealSystemTime: () => timers.getRealSystemTime(),
|
|
277
|
+
setTimerTickMode: chained(vi, (mode, interval) => timers.setTimerTickMode(mode, interval))
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
// source/wait.ts
|
|
281
|
+
const WAIT_FOR = {
|
|
282
|
+
name: "waitFor",
|
|
283
|
+
accepts: () => true,
|
|
284
|
+
retriesErrors: true
|
|
285
|
+
};
|
|
286
|
+
const WAIT_UNTIL = {
|
|
287
|
+
name: "waitUntil",
|
|
288
|
+
accepts: Boolean,
|
|
289
|
+
retriesErrors: false
|
|
290
|
+
};
|
|
291
|
+
setSafeTimers();
|
|
292
|
+
function isThenable(value) {
|
|
293
|
+
return value !== null && typeof value === "object" && typeof value.then === "function";
|
|
294
|
+
}
|
|
295
|
+
/** The error, with what was waited for before its message, where it can be */
|
|
296
|
+
function prefixMessage(error, message) {
|
|
297
|
+
if (message === void 0 || message === "" || !(error instanceof Error)) return error;
|
|
298
|
+
const original = error.message;
|
|
299
|
+
try {
|
|
300
|
+
error.message = `${message}: ${original}`;
|
|
301
|
+
} catch {
|
|
302
|
+
return error;
|
|
303
|
+
}
|
|
304
|
+
if (error.stack !== void 0) error.stack = error.stack.replace(original, error.message);
|
|
305
|
+
return error;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* A wait for a function, which calls it now, and then at each interval,
|
|
309
|
+
* until the policy accepts a value of it, its error ends the wait, or the
|
|
310
|
+
* timeout passes
|
|
311
|
+
*/
|
|
312
|
+
var Wait = class {
|
|
313
|
+
#callback;
|
|
314
|
+
#policy;
|
|
315
|
+
#clock;
|
|
316
|
+
#interval;
|
|
317
|
+
#timeout;
|
|
318
|
+
#message;
|
|
319
|
+
/** Made with the wait, so that its stack names where the wait was called */
|
|
320
|
+
#timedOut;
|
|
321
|
+
/** The real timers, which the wait keeps to while the timers are fake */
|
|
322
|
+
#timers = getSafeTimers();
|
|
323
|
+
#result = Promise.withResolvers();
|
|
324
|
+
#lastError;
|
|
325
|
+
/** Whether a promise of the function's is still to settle */
|
|
326
|
+
#pending = false;
|
|
327
|
+
#settled = false;
|
|
328
|
+
#timeoutId;
|
|
329
|
+
#intervalId;
|
|
330
|
+
constructor(callback, options, policy, clock) {
|
|
331
|
+
const { interval = 50, timeout = 1e3, message } = typeof options === "number" ? { timeout: options } : options;
|
|
332
|
+
this.#callback = callback;
|
|
333
|
+
this.#policy = policy;
|
|
334
|
+
this.#clock = clock;
|
|
335
|
+
this.#interval = interval;
|
|
336
|
+
this.#timeout = timeout;
|
|
337
|
+
this.#message = message;
|
|
338
|
+
this.#timedOut = /* @__PURE__ */ new Error(`Timed out in ${policy.name}!`);
|
|
339
|
+
}
|
|
340
|
+
/** Calls the function for the first time, and returns the wait's promise */
|
|
341
|
+
start() {
|
|
342
|
+
const { setTimeout, setInterval } = this.#timers;
|
|
343
|
+
const { promise, reject } = this.#result;
|
|
344
|
+
try {
|
|
345
|
+
this.#check();
|
|
346
|
+
} catch (error) {
|
|
347
|
+
reject(error);
|
|
348
|
+
return promise;
|
|
349
|
+
}
|
|
350
|
+
if (!this.#settled) {
|
|
351
|
+
this.#timeoutId = setTimeout(() => this.#onTimeout(), this.#timeout);
|
|
352
|
+
this.#intervalId = setInterval(() => this.#check(), this.#interval);
|
|
353
|
+
}
|
|
354
|
+
return promise;
|
|
355
|
+
}
|
|
356
|
+
/** Moves the fake timers forward, and calls the function if it's due */
|
|
357
|
+
#check() {
|
|
358
|
+
if (this.#clock.isFakeTimers()) this.#clock.advanceTimersByTime(this.#interval);
|
|
359
|
+
if (this.#pending) return;
|
|
360
|
+
try {
|
|
361
|
+
const result = this.#callback();
|
|
362
|
+
if (isThenable(result)) {
|
|
363
|
+
this.#pending = true;
|
|
364
|
+
result.then((value) => this.#onValue(value), (error) => this.#onError(error));
|
|
365
|
+
} else this.#onValue(result);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
this.#onError(error);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
#onValue(value) {
|
|
371
|
+
this.#pending = false;
|
|
372
|
+
if (this.#policy.accepts(value)) {
|
|
373
|
+
this.#stop();
|
|
374
|
+
this.#result.resolve(value);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
#onError(error) {
|
|
378
|
+
this.#pending = false;
|
|
379
|
+
if (this.#policy.retriesErrors) this.#lastError = error;
|
|
380
|
+
else this.#fail(error);
|
|
381
|
+
}
|
|
382
|
+
#onTimeout() {
|
|
383
|
+
this.#fail(this.#lastError);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Rejects the wait with the function's error, or with the timeout's if
|
|
387
|
+
* that is falsy, which counts as none, as in Vitest
|
|
388
|
+
*/
|
|
389
|
+
#fail(functionError) {
|
|
390
|
+
const error = functionError || this.#timedOut;
|
|
391
|
+
this.#stop();
|
|
392
|
+
this.#result.reject(prefixMessage(error, this.#message));
|
|
393
|
+
}
|
|
394
|
+
/** Ends the calls of the function, once the wait has its result */
|
|
395
|
+
#stop() {
|
|
396
|
+
const { clearTimeout, clearInterval } = this.#timers;
|
|
397
|
+
this.#settled = true;
|
|
398
|
+
clearTimeout(this.#timeoutId);
|
|
399
|
+
clearInterval(this.#intervalId);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* The members of `vi` that wait, which move the fake timers forward while
|
|
404
|
+
* a test uses them
|
|
405
|
+
*/
|
|
406
|
+
function createWaitAPI(clock) {
|
|
407
|
+
return {
|
|
408
|
+
waitFor: (callback, options = {}) => new Wait(callback, options, WAIT_FOR, clock).start(),
|
|
409
|
+
waitUntil: (callback, options = {}) => new Wait(callback, options, WAIT_UNTIL, clock).start()
|
|
410
|
+
};
|
|
411
|
+
}
|
|
9
412
|
// source/equals.ts
|
|
10
413
|
/**
|
|
11
414
|
* Nesting past which the tester leaves values to `equals`, which tells
|
|
@@ -251,13 +654,8 @@ function afterEach(fn, timeout) {
|
|
|
251
654
|
function mocked(item) {
|
|
252
655
|
return item;
|
|
253
656
|
}
|
|
254
|
-
/**
|
|
255
|
-
|
|
256
|
-
* and its module mocks, which node preloads with `--import hypotes/register`.
|
|
257
|
-
* node:test's `mock` mocks timers.
|
|
258
|
-
*/
|
|
259
|
-
const vi = {
|
|
260
|
-
clearAllMocks,
|
|
657
|
+
/** The members of `vi` that mock functions, objects' methods, and modules */
|
|
658
|
+
const mocks = {
|
|
261
659
|
doMock,
|
|
262
660
|
doUnmock,
|
|
263
661
|
fn,
|
|
@@ -266,9 +664,24 @@ const vi = {
|
|
|
266
664
|
isMockFunction,
|
|
267
665
|
mock,
|
|
268
666
|
mocked,
|
|
269
|
-
resetAllMocks,
|
|
270
|
-
restoreAllMocks,
|
|
271
667
|
spyOn,
|
|
272
668
|
unmock
|
|
273
669
|
};
|
|
670
|
+
const self = () => vi;
|
|
671
|
+
const timers = createTimersAPI(self);
|
|
672
|
+
/**
|
|
673
|
+
* Vitest's spies, which its matchers, such as `toHaveBeenCalledWith`, check,
|
|
674
|
+
* its module mocks, which node preloads with `--import hypotes/register`,
|
|
675
|
+
* its fake timers, its stubs of globals and environment variables, and its
|
|
676
|
+
* waits.
|
|
677
|
+
*/
|
|
678
|
+
const vi = {
|
|
679
|
+
...mocks,
|
|
680
|
+
clearAllMocks: chained(self, clearAllMocks),
|
|
681
|
+
resetAllMocks: chained(self, resetAllMocks),
|
|
682
|
+
restoreAllMocks: chained(self, restoreAllMocks),
|
|
683
|
+
...createStubsAPI(self),
|
|
684
|
+
...timers,
|
|
685
|
+
...createWaitAPI(timers)
|
|
686
|
+
};
|
|
274
687
|
export { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, suite, test, vi };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
// source/project.ts
|
|
4
|
+
/**
|
|
5
|
+
* The name of the package.json in the directory, or undefined if it has
|
|
6
|
+
* none: without the file, without a name in it, or if it can't be read or
|
|
7
|
+
* parsed, which is the package's to report, not a reader's of its name.
|
|
8
|
+
*/
|
|
9
|
+
function packageName(directory) {
|
|
10
|
+
try {
|
|
11
|
+
const { name } = JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));
|
|
12
|
+
return typeof name === "string" && name !== "" ? name : void 0;
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** The projects of files, which it reads once for each directory */
|
|
18
|
+
var Projects = class {
|
|
19
|
+
/** Null for a directory that has no project, which was looked for */
|
|
20
|
+
#byDirectory = /* @__PURE__ */ new Map();
|
|
21
|
+
/**
|
|
22
|
+
* The project of the file at the absolute path, or undefined if no
|
|
23
|
+
* directory above it has a package.json with a name. A package.json
|
|
24
|
+
* without one, such as one that only sets `type`, is passed over.
|
|
25
|
+
*/
|
|
26
|
+
of(file) {
|
|
27
|
+
return this.#in(dirname(file));
|
|
28
|
+
}
|
|
29
|
+
#in(directory) {
|
|
30
|
+
const cached = this.#byDirectory.get(directory);
|
|
31
|
+
if (cached !== void 0) return cached ?? void 0;
|
|
32
|
+
const project = this.#find(directory);
|
|
33
|
+
this.#byDirectory.set(directory, project ?? null);
|
|
34
|
+
return project;
|
|
35
|
+
}
|
|
36
|
+
#find(directory) {
|
|
37
|
+
const name = packageName(directory);
|
|
38
|
+
if (name !== void 0) return {
|
|
39
|
+
name,
|
|
40
|
+
directory
|
|
41
|
+
};
|
|
42
|
+
const parent = dirname(directory);
|
|
43
|
+
return parent === directory ? void 0 : this.#in(parent);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
export { Projects as t };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Selects projects by the patterns of `--project` */
|
|
2
|
+
export declare class ProjectFilter {
|
|
3
|
+
#private;
|
|
4
|
+
/**
|
|
5
|
+
* Of patterns such as `@acme/api`, `@acme/*` and `!@acme/web`. A
|
|
6
|
+
* pattern that is empty, or only `!`, matches no project.
|
|
7
|
+
*/
|
|
8
|
+
constructor(patterns: readonly string[]);
|
|
9
|
+
/**
|
|
10
|
+
* Whether a project of the name is selected: no pattern with `!` matches
|
|
11
|
+
* it, and one of the others does, if there are others
|
|
12
|
+
*/
|
|
13
|
+
matches(name: string): boolean;
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** A package that test files belong to */
|
|
2
|
+
export interface Project {
|
|
3
|
+
/** The `name` of its package.json */
|
|
4
|
+
readonly name: string;
|
|
5
|
+
/** The directory of its package.json */
|
|
6
|
+
readonly directory: string;
|
|
7
|
+
}
|
|
8
|
+
/** The projects of files, which it reads once for each directory */
|
|
9
|
+
export declare class Projects {
|
|
10
|
+
#private;
|
|
11
|
+
/**
|
|
12
|
+
* The project of the file at the absolute path, or undefined if no
|
|
13
|
+
* directory above it has a package.json with a name. A package.json
|
|
14
|
+
* without one, such as one that only sets `type`, is passed over.
|
|
15
|
+
*/
|
|
16
|
+
of(file: string): Project | undefined;
|
|
17
|
+
}
|