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/reporter.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { t as Projects } from "./project-Cf9C54In.js";
|
|
2
|
+
import { inspect, styleText } from "node:util";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { relative, resolve, sep } from "node:path";
|
|
5
|
+
import { spec } from "node:test/reporters";
|
|
6
|
+
// source/reporter.ts
|
|
7
|
+
/**
|
|
8
|
+
* Whether Node prints colours, as it does to a terminal or with FORCE_COLOR:
|
|
9
|
+
* `styleText` leaves its text as it is where it doesn't
|
|
10
|
+
*/
|
|
11
|
+
const COLORS = styleText("red", "text") !== "text";
|
|
12
|
+
/** The backgrounds of projects' names, one of which a name picks */
|
|
13
|
+
const PROJECT_COLORS = [
|
|
14
|
+
"bgYellow",
|
|
15
|
+
"bgCyan",
|
|
16
|
+
"bgGreen",
|
|
17
|
+
"bgMagenta"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* The diagnostics in which node:test reports the run's totals, which the
|
|
21
|
+
* totals here replace
|
|
22
|
+
*/
|
|
23
|
+
const TOTAL = /^(?:tests|suites|pass|fail|cancelled|skipped|todo|duration_ms) [\d.]+$/u;
|
|
24
|
+
/** The mark of a failure's error, which is a block of colour where it has one */
|
|
25
|
+
const FAIL = COLORS ? styleText([
|
|
26
|
+
"bold",
|
|
27
|
+
"inverse",
|
|
28
|
+
"red"
|
|
29
|
+
], " FAIL ") : " FAIL";
|
|
30
|
+
/** How many tests had each result. A suite isn't a test. */
|
|
31
|
+
var Counts = class {
|
|
32
|
+
failed = 0;
|
|
33
|
+
passed = 0;
|
|
34
|
+
skipped = 0;
|
|
35
|
+
todo = 0;
|
|
36
|
+
total() {
|
|
37
|
+
return this.failed + this.passed + this.skipped + this.todo;
|
|
38
|
+
}
|
|
39
|
+
/** Counts a test as node:test does: one to do isn't failed, if it fails */
|
|
40
|
+
add({ type, data }) {
|
|
41
|
+
if (isSet(data.skip)) this.skipped++;
|
|
42
|
+
else if (isSet(data.todo)) this.todo++;
|
|
43
|
+
else if (type === "test:fail") this.failed++;
|
|
44
|
+
else this.passed++;
|
|
45
|
+
}
|
|
46
|
+
/** Adds the counts of a file's tests to a run's */
|
|
47
|
+
addAll(counts) {
|
|
48
|
+
this.failed += counts.failed;
|
|
49
|
+
this.passed += counts.passed;
|
|
50
|
+
this.skipped += counts.skipped;
|
|
51
|
+
this.todo += counts.todo;
|
|
52
|
+
}
|
|
53
|
+
/** Each count but zeroes, as `1 failed | 2 passed`, and then the total */
|
|
54
|
+
format() {
|
|
55
|
+
const parts = [
|
|
56
|
+
this.failed > 0 ? styleText(["bold", "red"], `${this.failed} failed`) : "",
|
|
57
|
+
this.passed > 0 ? styleText(["bold", "green"], `${this.passed} passed`) : "",
|
|
58
|
+
this.skipped > 0 ? styleText("yellow", `${this.skipped} skipped`) : "",
|
|
59
|
+
this.todo > 0 ? styleText("gray", `${this.todo} todo`) : ""
|
|
60
|
+
].filter((part) => part !== "");
|
|
61
|
+
const total = styleText("gray", `(${this.total()})`);
|
|
62
|
+
return parts.length === 0 ? total : `${parts.join(" | ")} ${total}`;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
/** Why node:test failed a test, which its error says, as `subtestsFailed` */
|
|
66
|
+
function failureType(error) {
|
|
67
|
+
return error.failureType;
|
|
68
|
+
}
|
|
69
|
+
/** Whether a test's `skip` or `todo` is true, or gives a reason */
|
|
70
|
+
function isSet(flag) {
|
|
71
|
+
return flag !== void 0 && flag !== false;
|
|
72
|
+
}
|
|
73
|
+
function formatDuration(milliseconds) {
|
|
74
|
+
return `${Math.round(milliseconds)}ms`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The name of a project, on a background that the name picks, or between
|
|
78
|
+
* bars without colours, as Vitest prints it
|
|
79
|
+
*/
|
|
80
|
+
function formatProject({ name }) {
|
|
81
|
+
if (!COLORS) return `|${name}|`;
|
|
82
|
+
let sum = 0;
|
|
83
|
+
for (let index = 0; index < name.length; index++) sum += (name.codePointAt(index) ?? 0) + index;
|
|
84
|
+
return styleText([PROJECT_COLORS[sum % PROJECT_COLORS.length], "black"], ` ${name} `);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* What a test failed with, as node:test's reporters print it: node:test
|
|
88
|
+
* wraps what the test threw in an error of its own, as its cause
|
|
89
|
+
*/
|
|
90
|
+
function formatError(error) {
|
|
91
|
+
const cause = error.cause ?? error;
|
|
92
|
+
return typeof cause === "string" ? cause : inspect(cause, { colors: COLORS });
|
|
93
|
+
}
|
|
94
|
+
/** The results of a test file's tests, which its row prints */
|
|
95
|
+
var FileResults = class {
|
|
96
|
+
#file;
|
|
97
|
+
#cwd;
|
|
98
|
+
#label;
|
|
99
|
+
#counts = new Counts();
|
|
100
|
+
#failures = [];
|
|
101
|
+
/** The names of the suites and the test that are running, by nesting */
|
|
102
|
+
#running = [];
|
|
103
|
+
/** Of its tests and suites that aren't in a suite */
|
|
104
|
+
#duration = 0;
|
|
105
|
+
/** Of the file at the absolute path, which is named from `cwd` */
|
|
106
|
+
constructor(file, project, cwd) {
|
|
107
|
+
const path = relative(project?.directory ?? cwd, file).replaceAll(sep, "/");
|
|
108
|
+
this.#file = file;
|
|
109
|
+
this.#cwd = cwd;
|
|
110
|
+
this.#label = project === void 0 ? path : `${formatProject(project)} ${path}`;
|
|
111
|
+
}
|
|
112
|
+
file() {
|
|
113
|
+
return this.#file;
|
|
114
|
+
}
|
|
115
|
+
counts() {
|
|
116
|
+
return this.#counts;
|
|
117
|
+
}
|
|
118
|
+
failed() {
|
|
119
|
+
return this.#failures.length > 0;
|
|
120
|
+
}
|
|
121
|
+
start({ name, nesting }) {
|
|
122
|
+
this.#running.length = nesting;
|
|
123
|
+
this.#running.push(name);
|
|
124
|
+
}
|
|
125
|
+
add(result) {
|
|
126
|
+
const { name, nesting, details } = result.data;
|
|
127
|
+
const isFile = this.#isFile(name, nesting);
|
|
128
|
+
if (nesting === 0) this.#duration += details.duration_ms;
|
|
129
|
+
if (details.type !== "suite" && !isFile) this.#counts.add(result);
|
|
130
|
+
if (result.type === "test:fail") this.#addFailure(result, isFile ? [] : [...this.#running.slice(0, nesting), name], details.type !== "suite" && !isFile);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Whether a result is of the file itself, rather than of a test in it.
|
|
134
|
+
* node:test reports a file that fails as it loads, or none of whose
|
|
135
|
+
* tests ran, as a test named by its path. It isn't counted as a test
|
|
136
|
+
* here: its failure fails the file, under the row's own title.
|
|
137
|
+
*/
|
|
138
|
+
#isFile(name, nesting) {
|
|
139
|
+
return nesting === 0 && resolve(this.#cwd, name) === this.#file;
|
|
140
|
+
}
|
|
141
|
+
#addFailure({ data }, titles, isTest) {
|
|
142
|
+
const { error, duration_ms: duration } = data.details;
|
|
143
|
+
if (failureType(error) === "subtestsFailed" || isSet(data.todo) || isSet(data.skip)) return;
|
|
144
|
+
this.#failures.push({
|
|
145
|
+
titles,
|
|
146
|
+
isTest,
|
|
147
|
+
duration,
|
|
148
|
+
error: formatError(error)
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** The file's row, and a line for each of its failures */
|
|
152
|
+
formatRow() {
|
|
153
|
+
const total = this.#counts.total();
|
|
154
|
+
const { failed, skipped, todo } = this.#counts;
|
|
155
|
+
const counts = [
|
|
156
|
+
`${total} ${total === 1 ? "test" : "tests"}`,
|
|
157
|
+
failed > 0 ? styleText("red", `${failed} failed`) : "",
|
|
158
|
+
skipped > 0 ? styleText("yellow", `${skipped} skipped`) : "",
|
|
159
|
+
todo > 0 ? styleText("gray", `${todo} todo`) : ""
|
|
160
|
+
].filter((part) => part !== "");
|
|
161
|
+
const mark = this.failed() ? styleText("yellow", "❯") : styleText("green", "✓");
|
|
162
|
+
const duration = styleText("gray", formatDuration(this.#duration));
|
|
163
|
+
return ` ${mark} ${this.#label} ${styleText("gray", `(${counts.join(" | ")})`)} ${duration}\n` + this.#failures.map((failure) => formatFailedTest(failure)).join("");
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The errors of the file's failed tests, or of its failed suites and of
|
|
167
|
+
* the file itself, each under its file and titles
|
|
168
|
+
*/
|
|
169
|
+
formatErrors(ofTests) {
|
|
170
|
+
return this.#failures.filter(({ isTest }) => isTest === ofTests).map(({ titles, error }) => {
|
|
171
|
+
const title = [this.#label, ...titles].join(" > ");
|
|
172
|
+
return `${FAIL} ${title}\n${error}\n`;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
/** The line of a failed test under its file's row. The file itself has none. */
|
|
177
|
+
function formatFailedTest({ titles, duration }) {
|
|
178
|
+
if (titles.length === 0) return "";
|
|
179
|
+
const time = styleText("gray", formatDuration(duration));
|
|
180
|
+
return ` ${styleText("red", "×")} ${titles.join(" > ")} ${time}\n`;
|
|
181
|
+
}
|
|
182
|
+
/** The test file of an event's test, or else the file that declared it */
|
|
183
|
+
function testFile(data) {
|
|
184
|
+
return data.entryFile ?? data.file;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The errors under a heading with their number, as `Failed Tests 2`, or
|
|
188
|
+
* nothing without errors
|
|
189
|
+
*/
|
|
190
|
+
function formatErrors(heading, errors) {
|
|
191
|
+
if (errors.length === 0) return "";
|
|
192
|
+
return `\n${styleText(["bold", "red"], ` ${heading} ${errors.length}`)}\n\n${errors.join("\n")}`;
|
|
193
|
+
}
|
|
194
|
+
/** A line of the totals, whose labels end in the same column */
|
|
195
|
+
function formatTotal(label, value) {
|
|
196
|
+
return `${styleText("gray", label.padStart(11))} ${value}\n`;
|
|
197
|
+
}
|
|
198
|
+
/** The report of a run, which takes node:test's events and gives the text to print */
|
|
199
|
+
var Report = class {
|
|
200
|
+
#cwd;
|
|
201
|
+
#projects = new Projects();
|
|
202
|
+
#files = new Counts();
|
|
203
|
+
#tests = new Counts();
|
|
204
|
+
/** Of the suites and the files that failed, which aren't failed tests */
|
|
205
|
+
#suiteErrors = [];
|
|
206
|
+
#testErrors = [];
|
|
207
|
+
/** The file whose tests are being reported */
|
|
208
|
+
#current;
|
|
209
|
+
/** Names the files that have no project from `cwd` */
|
|
210
|
+
constructor(cwd) {
|
|
211
|
+
this.#cwd = cwd;
|
|
212
|
+
}
|
|
213
|
+
/** What to print for the event, which is nothing for most */
|
|
214
|
+
print(event) {
|
|
215
|
+
switch (event.type) {
|
|
216
|
+
case "test:start": return this.#in(testFile(event.data), (results) => {
|
|
217
|
+
results.start(event.data);
|
|
218
|
+
});
|
|
219
|
+
case "test:pass":
|
|
220
|
+
case "test:fail": return this.#in(testFile(event.data), (results) => {
|
|
221
|
+
results.add(event);
|
|
222
|
+
});
|
|
223
|
+
case "test:summary": return this.#summarize(event.data);
|
|
224
|
+
case "test:diagnostic": {
|
|
225
|
+
const { file, message } = event.data;
|
|
226
|
+
return file === void 0 && TOTAL.test(message) ? "" : `${styleText("blue", `ℹ ${message}`)}\n`;
|
|
227
|
+
}
|
|
228
|
+
case "test:stdout":
|
|
229
|
+
case "test:stderr": return event.data.message;
|
|
230
|
+
case "test:coverage": return formatCoverage(event);
|
|
231
|
+
default: return "";
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Has `record` take an event of the file's results, after ending the
|
|
236
|
+
* file before it, whose row it returns
|
|
237
|
+
*/
|
|
238
|
+
#in(file, record) {
|
|
239
|
+
if (file === void 0) return "";
|
|
240
|
+
const ended = this.#current?.file() === file ? "" : this.#endFile();
|
|
241
|
+
this.#current ??= new FileResults(file, this.#projects.of(file), this.#cwd);
|
|
242
|
+
record(this.#current);
|
|
243
|
+
return ended;
|
|
244
|
+
}
|
|
245
|
+
/** The row of the current file, whose results go to the totals */
|
|
246
|
+
#endFile() {
|
|
247
|
+
const results = this.#current;
|
|
248
|
+
if (results === void 0) return "";
|
|
249
|
+
this.#current = void 0;
|
|
250
|
+
this.#tests.addAll(results.counts());
|
|
251
|
+
if (results.failed()) this.#files.failed++;
|
|
252
|
+
else this.#files.passed++;
|
|
253
|
+
this.#suiteErrors.push(...results.formatErrors(false));
|
|
254
|
+
this.#testErrors.push(...results.formatErrors(true));
|
|
255
|
+
return results.formatRow();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Ends the file that a summary is of, if its tests are the last
|
|
259
|
+
* reported, or the run, if the summary is of all files. Under
|
|
260
|
+
* `--watch`, another run follows, so the totals start over.
|
|
261
|
+
*/
|
|
262
|
+
#summarize({ file, duration_ms: duration }) {
|
|
263
|
+
if (file !== void 0) return this.#current?.file() === file ? this.#endFile() : "";
|
|
264
|
+
const row = this.#endFile();
|
|
265
|
+
const errors = formatErrors("Failed Suites", this.#suiteErrors) + formatErrors("Failed Tests", this.#testErrors);
|
|
266
|
+
const totals = formatTotal("Test Files", this.#files.format()) + formatTotal("Tests", this.#tests.format()) + formatTotal("Duration", formatDuration(duration));
|
|
267
|
+
this.#suiteErrors = [];
|
|
268
|
+
this.#testErrors = [];
|
|
269
|
+
this.#files = new Counts();
|
|
270
|
+
this.#tests = new Counts();
|
|
271
|
+
return `${row}${errors}\n${totals}\n`;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
/** The table of the run's coverage, which node:test's `spec` reporter formats */
|
|
275
|
+
function formatCoverage(event) {
|
|
276
|
+
const reporter = new spec();
|
|
277
|
+
reporter.write(event);
|
|
278
|
+
return reporter.read() ?? "";
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The reporter, as `--test-reporter=hypotes/reporter` loads it, and as the
|
|
282
|
+
* stream of node:test's `run()` composes it. Besides the files' rows, the
|
|
283
|
+
* errors and the totals, it prints what tests write to stdout and stderr,
|
|
284
|
+
* their diagnostics, and the coverage as node:test's `spec` does.
|
|
285
|
+
*/
|
|
286
|
+
async function* reporter(source) {
|
|
287
|
+
const report = new Report(process.cwd());
|
|
288
|
+
for await (const event of source) {
|
|
289
|
+
const text = report.print(event);
|
|
290
|
+
if (text !== "") yield text;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
export { reporter as default };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type * as MagicStringModule from 'magic-string';
|
|
2
|
+
type DecodedSourceMap = MagicStringModule.DecodedSourceMap;
|
|
3
|
+
/** The fields of a map of one module that chaining reads */
|
|
4
|
+
interface ModuleSourceMap {
|
|
5
|
+
readonly sources: ReadonlyArray<string | null>;
|
|
6
|
+
readonly mappings: string;
|
|
7
|
+
readonly names?: readonly string[];
|
|
8
|
+
readonly sourceRoot?: string;
|
|
9
|
+
readonly sourcesContent?: ReadonlyArray<string | null>;
|
|
10
|
+
}
|
|
11
|
+
/** A map of a part of a module, from where the part starts */
|
|
12
|
+
interface Section {
|
|
13
|
+
readonly offset: {
|
|
14
|
+
readonly line: number;
|
|
15
|
+
readonly column: number;
|
|
16
|
+
};
|
|
17
|
+
readonly map: ModuleSourceMap;
|
|
18
|
+
}
|
|
19
|
+
/** A map of a module in sections, each of which maps a part of it */
|
|
20
|
+
interface IndexSourceMap {
|
|
21
|
+
readonly sections: readonly Section[];
|
|
22
|
+
}
|
|
23
|
+
/** A source map that a hook after `hypotes/register` appended */
|
|
24
|
+
export type IncomingSourceMap = IndexSourceMap | ModuleSourceMap;
|
|
25
|
+
/** A module's code, and the inline source map that it ended with, if any */
|
|
26
|
+
export interface MappedCode {
|
|
27
|
+
readonly code: string;
|
|
28
|
+
readonly map: IncomingSourceMap | undefined;
|
|
29
|
+
}
|
|
30
|
+
/** A chained map, decoded, and the `sourceRoot` of its sources, if any */
|
|
31
|
+
export interface ChainedSourceMap extends DecodedSourceMap {
|
|
32
|
+
readonly sourceRoot?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The code without the inline source map at its end, and that map, unless
|
|
36
|
+
* it doesn't parse or isn't a source map. A map comment that isn't on the
|
|
37
|
+
* code's last line, but for whitespace, stays in the code.
|
|
38
|
+
*/
|
|
39
|
+
export declare function splitInlineMap(text: string): MappedCode;
|
|
40
|
+
/**
|
|
41
|
+
* The decoded map of the hoisted code, through the incoming map, to where
|
|
42
|
+
* that one maps it
|
|
43
|
+
*/
|
|
44
|
+
export declare function chainSourceMap(map: DecodedSourceMap, incoming: IncomingSourceMap): ChainedSourceMap;
|
|
45
|
+
export {};
|
package/dist/stubs.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** The names that Vitest's `vi.stubEnv()` takes a boolean for, as Vite's */
|
|
2
|
+
type BooleanEnv = 'DEV' | 'PROD' | 'SSR';
|
|
3
|
+
/**
|
|
4
|
+
* The members of `vi` that stub globals and environment variables. Those
|
|
5
|
+
* that Vitest's return `vi` from do here too, as `Vi`.
|
|
6
|
+
*/
|
|
7
|
+
export interface StubsAPI<Vi> {
|
|
8
|
+
/**
|
|
9
|
+
* Defines a global, whether or not there is one by the name, until
|
|
10
|
+
* `vi.unstubAllGlobals()`.
|
|
11
|
+
*/
|
|
12
|
+
stubGlobal(name: string | symbol | number, value: unknown): Vi;
|
|
13
|
+
/**
|
|
14
|
+
* Sets a variable of `process.env`, or deletes it if the value is
|
|
15
|
+
* undefined, until `vi.unstubAllEnvs()`. `PROD`, `DEV`, and `SSR` take a
|
|
16
|
+
* boolean, which is set as `'1'` or `''`.
|
|
17
|
+
*/
|
|
18
|
+
stubEnv<Name extends string>(name: Name, value: Name extends BooleanEnv ? boolean | undefined : string | undefined): Vi;
|
|
19
|
+
/**
|
|
20
|
+
* Leaves each global that `vi.stubGlobal()` defined as it was before the
|
|
21
|
+
* first stub of it.
|
|
22
|
+
*/
|
|
23
|
+
unstubAllGlobals(): Vi;
|
|
24
|
+
/**
|
|
25
|
+
* Leaves each variable that `vi.stubEnv()` set as it was before the first
|
|
26
|
+
* stub of it.
|
|
27
|
+
*/
|
|
28
|
+
unstubAllEnvs(): Vi;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The members of `vi` that stub globals and environment variables, which
|
|
32
|
+
* return what `vi` returns, for the calls that chain.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createStubsAPI<Vi>(vi: () => Vi): StubsAPI<Vi>;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ProjectFilter } from './project-filter.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The pattern of the files that node:test runs when it's given none, which
|
|
4
|
+
* its documentation lists. It takes TypeScript's extensions unless Node is
|
|
5
|
+
* told not to strip types.
|
|
6
|
+
*/
|
|
7
|
+
export declare function defaultPattern(stripsTypes: boolean): string;
|
|
8
|
+
/** The test files of projects under a working directory */
|
|
9
|
+
export declare class TestFiles {
|
|
10
|
+
#private;
|
|
11
|
+
/** Of the projects that the filter selects, under `cwd` */
|
|
12
|
+
constructor(cwd: string, filter: ProjectFilter);
|
|
13
|
+
/**
|
|
14
|
+
* The names of the projects under the working directory, and of the one
|
|
15
|
+
* that it's in, in order
|
|
16
|
+
*/
|
|
17
|
+
projectNames(): string[];
|
|
18
|
+
/**
|
|
19
|
+
* The files that match the glob patterns and are in a selected project,
|
|
20
|
+
* as patterns of their paths from the working directory, in order. A
|
|
21
|
+
* file that no package with a name is above is in no project.
|
|
22
|
+
*/
|
|
23
|
+
matching(patterns: readonly string[]): string[];
|
|
24
|
+
/**
|
|
25
|
+
* What names the test files of the selected projects to node:test: a
|
|
26
|
+
* pattern for a project's directory, or its files, as the header says.
|
|
27
|
+
* Nothing, if no project is selected, or none of the listed has a test
|
|
28
|
+
* file.
|
|
29
|
+
*/
|
|
30
|
+
ofProjects(pattern: string): string[];
|
|
31
|
+
}
|
package/dist/timers.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type * as SinonFakeTimers from '@sinonjs/fake-timers';
|
|
2
|
+
/** A time to set the clock to: a timestamp, or what has or parses to one */
|
|
3
|
+
type SystemTime = number | string | Date | SinonFakeTimers.TemporalTimelike;
|
|
4
|
+
/** The options of `vi.useFakeTimers()`: @sinonjs/fake-timers', and Vitest's */
|
|
5
|
+
export interface FakeTimersConfig extends SinonFakeTimers.Config {
|
|
6
|
+
/**
|
|
7
|
+
* Fakes the timers that are imported from `node:timers` and
|
|
8
|
+
* `node:timers/promises` too, rather than only the globals.
|
|
9
|
+
*
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
12
|
+
fakeNodeBuiltins?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The members of `vi` that fake timers. Those that Vitest's return `vi` from
|
|
16
|
+
* do here too, as `Vi`.
|
|
17
|
+
*
|
|
18
|
+
* Besides `useFakeTimers`, `useRealTimers`, `isFakeTimers`, `setSystemTime`,
|
|
19
|
+
* `getMockedSystemTime`, `getRealSystemTime`, and `clearAllTimers`, each
|
|
20
|
+
* throws unless the timers are fake.
|
|
21
|
+
*/
|
|
22
|
+
export interface TimersAPI<Vi> {
|
|
23
|
+
/**
|
|
24
|
+
* Replaces the timers and the clock with fake ones, which only the
|
|
25
|
+
* members of `vi` advance, until `vi.useRealTimers()`. It leaves
|
|
26
|
+
* `process.nextTick` and `queueMicrotask` alone, unless `toFake` names
|
|
27
|
+
* them.
|
|
28
|
+
*/
|
|
29
|
+
useFakeTimers(config?: FakeTimersConfig): Vi;
|
|
30
|
+
/** Restores the timers and the clock, and discards the fake timers set */
|
|
31
|
+
useRealTimers(): Vi;
|
|
32
|
+
/** Whether `vi.useFakeTimers()` has replaced the timers */
|
|
33
|
+
isFakeTimers(): boolean;
|
|
34
|
+
/** Runs the timers that are waiting, but not those that they set */
|
|
35
|
+
runOnlyPendingTimers(): Vi;
|
|
36
|
+
/** As `runOnlyPendingTimers`, and awaits the timers that are set as promises settle */
|
|
37
|
+
runOnlyPendingTimersAsync(): Promise<Vi>;
|
|
38
|
+
/**
|
|
39
|
+
* Runs timers until none wait, those that they set too. It throws after
|
|
40
|
+
* `loopLimit` timers, 10 000 by default, as an interval never ends.
|
|
41
|
+
*/
|
|
42
|
+
runAllTimers(): Vi;
|
|
43
|
+
/** As `runAllTimers`, and awaits the timers that are set as promises settle */
|
|
44
|
+
runAllTimersAsync(): Promise<Vi>;
|
|
45
|
+
/** Runs the callbacks of `process.nextTick`, where `toFake` names it */
|
|
46
|
+
runAllTicks(): Vi;
|
|
47
|
+
/** Moves the clock forward by milliseconds, and runs the timers due by then */
|
|
48
|
+
advanceTimersByTime(ms: number): Vi;
|
|
49
|
+
/** As `advanceTimersByTime`, and awaits the timers that are set as promises settle */
|
|
50
|
+
advanceTimersByTimeAsync(ms: number): Promise<Vi>;
|
|
51
|
+
/** Moves the clock forward to the next timer, and runs it */
|
|
52
|
+
advanceTimersToNextTimer(): Vi;
|
|
53
|
+
/** As `advanceTimersToNextTimer`, and awaits the timer if it was set as a promise settled */
|
|
54
|
+
advanceTimersToNextTimerAsync(): Promise<Vi>;
|
|
55
|
+
/** Moves the clock forward to the next frame of `requestAnimationFrame` */
|
|
56
|
+
advanceTimersToNextFrame(): Vi;
|
|
57
|
+
/** The number of timers that are waiting */
|
|
58
|
+
getTimerCount(): number;
|
|
59
|
+
/** Removes the timers that are waiting, which then never run */
|
|
60
|
+
clearAllTimers(): Vi;
|
|
61
|
+
/**
|
|
62
|
+
* Sets the clock, without running timers. While the timers are real, it
|
|
63
|
+
* fakes only the date, until `vi.useRealTimers()`.
|
|
64
|
+
*/
|
|
65
|
+
setSystemTime(time: SystemTime): Vi;
|
|
66
|
+
/** The fake clock's time, or null while the date is real */
|
|
67
|
+
getMockedSystemTime(): Date | null;
|
|
68
|
+
/** The real time, in milliseconds, while the clock is fake too */
|
|
69
|
+
getRealSystemTime(): number;
|
|
70
|
+
/**
|
|
71
|
+
* How the clock moves: only as `vi` advances it, which is `manual` and
|
|
72
|
+
* the default, to the next timer after each macrotask, or by an
|
|
73
|
+
* interval of milliseconds as real time passes.
|
|
74
|
+
*/
|
|
75
|
+
setTimerTickMode(mode: 'manual' | 'nextTimerAsync'): Vi;
|
|
76
|
+
setTimerTickMode(mode: 'interval', interval?: number): Vi;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The members of `vi` that fake timers, which return what `vi` returns, for
|
|
80
|
+
* the calls that chain.
|
|
81
|
+
*/
|
|
82
|
+
export declare function createTimersAPI<Vi>(vi: () => Vi): TimersAPI<Vi>;
|
|
83
|
+
export {};
|
package/dist/wait.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** How long `vi.waitFor()` and `vi.waitUntil()` wait, and what for */
|
|
2
|
+
export interface WaitOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The milliseconds between the calls of the function.
|
|
5
|
+
*
|
|
6
|
+
* @default 50
|
|
7
|
+
*/
|
|
8
|
+
interval?: number;
|
|
9
|
+
/**
|
|
10
|
+
* The milliseconds after which the wait fails.
|
|
11
|
+
*
|
|
12
|
+
* @default 1000
|
|
13
|
+
*/
|
|
14
|
+
timeout?: number;
|
|
15
|
+
/** What is waited for, which starts the message of the wait's error */
|
|
16
|
+
message?: string;
|
|
17
|
+
}
|
|
18
|
+
/** What `vi.waitUntil()` resolves with: a value that isn't falsy */
|
|
19
|
+
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
|
|
20
|
+
/** The members of `vi` that wait for a function to succeed */
|
|
21
|
+
export interface WaitAPI {
|
|
22
|
+
/**
|
|
23
|
+
* Calls the function until it returns or resolves, and resolves with
|
|
24
|
+
* its value. It rejects with the function's last error once the timeout
|
|
25
|
+
* passes. A number for the options is the timeout.
|
|
26
|
+
*/
|
|
27
|
+
waitFor<T>(callback: () => T | Promise<T>, options?: number | WaitOptions): Promise<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Calls the function until it returns or resolves with a value that
|
|
30
|
+
* isn't falsy, and resolves with it. It rejects with the function's
|
|
31
|
+
* error at once, or once the timeout passes. An error that is falsy
|
|
32
|
+
* counts as none, so the wait rejects with its timeout's at once, as
|
|
33
|
+
* Vitest's does. A number for the options is the timeout.
|
|
34
|
+
*/
|
|
35
|
+
waitUntil<T>(callback: () => T | Promise<T>, options?: number | WaitOptions): Promise<Truthy<T>>;
|
|
36
|
+
}
|
|
37
|
+
/** The fake timers, which a wait moves forward while they're in use */
|
|
38
|
+
interface FakeClock {
|
|
39
|
+
isFakeTimers(): boolean;
|
|
40
|
+
advanceTimersByTime(ms: number): unknown;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The members of `vi` that wait, which move the fake timers forward while
|
|
44
|
+
* a test uses them
|
|
45
|
+
*/
|
|
46
|
+
export declare function createWaitAPI(clock: FakeClock): WaitAPI;
|
|
47
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypotes",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Vitest's API on node:test. Same tests, no Vite.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"expect",
|
|
@@ -28,11 +28,16 @@
|
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=24.13.1"
|
|
30
30
|
},
|
|
31
|
+
"bin": "./dist/cli.js",
|
|
31
32
|
"exports": {
|
|
32
33
|
".": {
|
|
33
34
|
"types": "./dist/index.d.ts",
|
|
34
35
|
"default": "./dist/index.js"
|
|
35
36
|
},
|
|
37
|
+
"./cli": {
|
|
38
|
+
"types": "./dist/cli.d.ts",
|
|
39
|
+
"default": "./dist/cli.js"
|
|
40
|
+
},
|
|
36
41
|
"./fast-check": {
|
|
37
42
|
"types": "./dist/fast-check.d.ts",
|
|
38
43
|
"default": "./dist/fast-check.js"
|
|
@@ -44,6 +49,10 @@
|
|
|
44
49
|
"./register": {
|
|
45
50
|
"types": "./dist/register.d.ts",
|
|
46
51
|
"default": "./dist/register.js"
|
|
52
|
+
},
|
|
53
|
+
"./reporter": {
|
|
54
|
+
"types": "./dist/reporter.d.ts",
|
|
55
|
+
"default": "./dist/reporter.js"
|
|
47
56
|
}
|
|
48
57
|
},
|
|
49
58
|
"imports": {
|
|
@@ -52,11 +61,16 @@
|
|
|
52
61
|
}
|
|
53
62
|
},
|
|
54
63
|
"publishConfig": {
|
|
64
|
+
"bin": "./dist/cli.js",
|
|
55
65
|
"exports": {
|
|
56
66
|
".": {
|
|
57
67
|
"types": "./dist/index.d.ts",
|
|
58
68
|
"default": "./dist/index.js"
|
|
59
69
|
},
|
|
70
|
+
"./cli": {
|
|
71
|
+
"types": "./dist/cli.d.ts",
|
|
72
|
+
"default": "./dist/cli.js"
|
|
73
|
+
},
|
|
60
74
|
"./fast-check": {
|
|
61
75
|
"types": "./dist/fast-check.d.ts",
|
|
62
76
|
"default": "./dist/fast-check.js"
|
|
@@ -68,6 +82,10 @@
|
|
|
68
82
|
"./register": {
|
|
69
83
|
"types": "./dist/register.d.ts",
|
|
70
84
|
"default": "./dist/register.js"
|
|
85
|
+
},
|
|
86
|
+
"./reporter": {
|
|
87
|
+
"types": "./dist/reporter.d.ts",
|
|
88
|
+
"default": "./dist/reporter.js"
|
|
71
89
|
}
|
|
72
90
|
},
|
|
73
91
|
"imports": {
|
|
@@ -83,12 +101,13 @@
|
|
|
83
101
|
"lint:check": "oxlint",
|
|
84
102
|
"lint:write": "oxlint --fix",
|
|
85
103
|
"prepack": "node scripts/require-yarn.ts && yarn build",
|
|
86
|
-
"test": "node --import hypotes/register --test 'test/**/*.test.ts'
|
|
104
|
+
"test": "node --import hypotes/register --test 'test/**/*.test.ts'",
|
|
87
105
|
"test:dist": "yarn build && NODE_OPTIONS=--conditions=hypotes-dist yarn test",
|
|
88
|
-
"test:watch": "node --import hypotes/register --test --watch 'test/**/*.test.ts'
|
|
106
|
+
"test:watch": "node --import hypotes/register --test --watch 'test/**/*.test.ts'",
|
|
89
107
|
"typecheck": "tsc -p ."
|
|
90
108
|
},
|
|
91
109
|
"dependencies": {
|
|
110
|
+
"@sinonjs/fake-timers": "15.4.0",
|
|
92
111
|
"@vitest/expect": "^5.1.0",
|
|
93
112
|
"@vitest/spy": "^5.1.0",
|
|
94
113
|
"@vitest/utils": "^5.1.0",
|
|
@@ -115,6 +134,8 @@
|
|
|
115
134
|
}
|
|
116
135
|
},
|
|
117
136
|
"devDependencies": {
|
|
137
|
+
"@snovader/oxc-config": "^0.1.0",
|
|
138
|
+
"@snovader/tsconfig": "^0.1.0",
|
|
118
139
|
"@types/node": "^26.6.6",
|
|
119
140
|
"fast-check": "^4.10.2",
|
|
120
141
|
"magic-string": "^1.4.5",
|