@uniflowed/test 0.0.0-alpha.4 → 0.0.0-alpha.40
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/browser-worker.js +339 -0
- package/bun/index.js +305 -0
- package/in-source.js +169 -0
- package/index.js +13 -1
- package/internal/axe.js +362 -0
- package/internal/browser/node.js +375 -0
- package/internal/browser/page.js +258 -0
- package/internal/browser/server.js +841 -0
- package/internal/expect.js +435 -54
- package/internal/frames.js +87 -1
- package/internal/isolation.js +231 -0
- package/internal/modules.js +430 -0
- package/internal/namespace.js +98 -50
- package/internal/output.js +105 -38
- package/internal/registry.js +54 -3
- package/internal/run.js +188 -40
- package/internal/timers.js +2 -2
- package/internal/unsupported.js +30 -0
- package/package.json +29 -4
- package/worker.js +187 -20
package/internal/frames.js
CHANGED
|
@@ -16,11 +16,24 @@
|
|
|
16
16
|
/** A position in a source file, one-based line and column. */
|
|
17
17
|
export type Site = {| readonly line: number, readonly column: number |};
|
|
18
18
|
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Frames belonging to the testing libraries, which no test author wrote.
|
|
21
|
+
*
|
|
22
|
+
* `@uniflowed/react-testing` is here for exactly the reason `@uniflowed/test`
|
|
23
|
+
* is, and was missing because it is a different package. Every `getBy…`
|
|
24
|
+
* failure is *constructed* inside it, so the first surviving frame of the
|
|
25
|
+
* commonest failure a component test can produce was `internal/queries.js` —
|
|
26
|
+
* and since a site is reported as a line of the file being run, the reader was
|
|
27
|
+
* sent to a line their own file does not have. That is ubugeeei-prod/uf#319.
|
|
28
|
+
* A library that raises on the caller's behalf is the runner as far as the
|
|
29
|
+
* report is concerned.
|
|
30
|
+
*/
|
|
20
31
|
const INTERNAL_MARKERS = [
|
|
21
32
|
"/packages/test/internal/",
|
|
22
33
|
"/packages/test/worker.js",
|
|
34
|
+
"/packages/react-testing/",
|
|
23
35
|
"/@uniflowed/test/",
|
|
36
|
+
"/@uniflowed/react-testing/",
|
|
24
37
|
"node:internal/",
|
|
25
38
|
];
|
|
26
39
|
|
|
@@ -53,6 +66,49 @@ export function frameSite(frame: string): Site | null {
|
|
|
53
66
|
return { line: line.value, column: column.value };
|
|
54
67
|
}
|
|
55
68
|
|
|
69
|
+
/**
|
|
70
|
+
* The file a stack frame names, or `null`.
|
|
71
|
+
*
|
|
72
|
+
* The same scan as [`frameSite`], stopping one step earlier: everything before
|
|
73
|
+
* the `:line:column` is where the code is, and what that is depends on how V8
|
|
74
|
+
* wrote the frame — `at name (/path:1:2)` when it has a function name, and
|
|
75
|
+
* `at /path:1:2` or `at async file:///path:1:2` when it does not.
|
|
76
|
+
*
|
|
77
|
+
* What comes back is whatever the frame said, a path or a URL, because those
|
|
78
|
+
* are the two things it can be and a caller resolving a module specifier
|
|
79
|
+
* against it has to tell them apart anyway.
|
|
80
|
+
*/
|
|
81
|
+
export function frameFile(frame: string): string | null {
|
|
82
|
+
let end = frame.length;
|
|
83
|
+
while (end > 0 && (frame[end - 1] === " " || frame[end - 1] === ")")) {
|
|
84
|
+
end -= 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const column = digitsBefore(frame, end);
|
|
88
|
+
if (column == null || column.start === 0 || frame[column.start - 1] !== ":") {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
const line = digitsBefore(frame, column.start - 1);
|
|
92
|
+
if (line == null || line.start === 0 || frame[line.start - 1] !== ":") {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let text = frame.slice(0, line.start - 1);
|
|
97
|
+
const open = text.lastIndexOf("(");
|
|
98
|
+
if (open !== -1) {
|
|
99
|
+
text = text.slice(open + 1);
|
|
100
|
+
} else {
|
|
101
|
+
const at = text.lastIndexOf(" at ");
|
|
102
|
+
text = at === -1 ? text : text.slice(at + 4);
|
|
103
|
+
}
|
|
104
|
+
text = text.trim();
|
|
105
|
+
// `at async /path:1:2` — the marker belongs to the frame, not to the file.
|
|
106
|
+
if (text.startsWith("async ")) {
|
|
107
|
+
text = text.slice("async ".length).trim();
|
|
108
|
+
}
|
|
109
|
+
return text === "" ? null : text;
|
|
110
|
+
}
|
|
111
|
+
|
|
56
112
|
/** The run of digits ending at `end`, with where it starts. */
|
|
57
113
|
function digitsBefore(
|
|
58
114
|
text: string,
|
|
@@ -93,6 +149,36 @@ export function firstUserSite(
|
|
|
93
149
|
return null;
|
|
94
150
|
}
|
|
95
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The first position in `stack` that is in `file`, or `null`.
|
|
154
|
+
*
|
|
155
|
+
* The reporter draws a failure's position as `path:line:column`, and it takes
|
|
156
|
+
* the path from the file it asked the worker to run rather than from the
|
|
157
|
+
* frame. So a number lifted from any other file is not a vaguer answer than
|
|
158
|
+
* none — it is a wrong one, naming a line the reader can open and that has
|
|
159
|
+
* nothing to do with what failed. Restricting the search to the file that will
|
|
160
|
+
* be named makes the two halves of that position come from the same place.
|
|
161
|
+
*
|
|
162
|
+
* `null` when the file is on no frame, which is a failure raised from work the
|
|
163
|
+
* case left behind: no line of it is the one that failed, and printing none is
|
|
164
|
+
* the honest answer.
|
|
165
|
+
*/
|
|
166
|
+
export function siteInFile(stack: string | null | void, file: string): Site | null {
|
|
167
|
+
if (stack == null) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
for (const frame of stack.split("\n").slice(1)) {
|
|
171
|
+
if (!frame.includes(file)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const site = frameSite(frame);
|
|
175
|
+
if (site != null) {
|
|
176
|
+
return site;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
96
182
|
/**
|
|
97
183
|
* `stack` with the runner's own frames removed.
|
|
98
184
|
*
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
//
|
|
3
|
+
// Internal to `@uniflowed/test`: what a file is allowed to leave behind.
|
|
4
|
+
//
|
|
5
|
+
// A worker serves many files out of one process, one at a time (`../worker.js`
|
|
6
|
+
// says why). Everything a file registers lives in this package and is cleared
|
|
7
|
+
// with it; everything a file *reaches around* the package to change belongs to
|
|
8
|
+
// the process, outlives the file, and is handed to whichever file the schedule
|
|
9
|
+
// puts next in that worker.
|
|
10
|
+
//
|
|
11
|
+
// That second list is what this module is. It has been discovered four times,
|
|
12
|
+
// and each time the same way — a suite that passed alone and failed beside
|
|
13
|
+
// another, naming the file that read the value rather than the file that wrote
|
|
14
|
+
// it:
|
|
15
|
+
//
|
|
16
|
+
// * ubugeeei-prod/uf#417, `uft.stubEnv("NODE_ENV", …)` still set for the
|
|
17
|
+
// next file;
|
|
18
|
+
// * ubugeeei-prod/uf#581, a fake clock still installed, so the next file's
|
|
19
|
+
// `setTimeout` — including the one each case is raced against — never
|
|
20
|
+
// fired and the file hung with nothing on screen;
|
|
21
|
+
// * ubugeeei-prod/uf#607, `document.body` still holding the markup a
|
|
22
|
+
// hydration test wrote into it, so the next file's "there is one image on
|
|
23
|
+
// the page" found six;
|
|
24
|
+
// * ubugeeei-prod/uf#944, the window around that body: a `matchMedia` a file
|
|
25
|
+
// removed, and the document's `FormData` left in place of Node's. That one is not on the list below, because this package does not
|
|
26
|
+
// make the window — `@uniflowed/react-testing` does, and registers how to
|
|
27
|
+
// put it back (see `registeredElsewhere`).
|
|
28
|
+
//
|
|
29
|
+
// Which files share a worker is decided by `.uf/test-timings.json`, so a leak
|
|
30
|
+
// makes the *result* of a suite depend on how the machine was loaded the last
|
|
31
|
+
// time it ran. That is the failure this module exists to end, and the reason
|
|
32
|
+
// it is a list in one place rather than three calls in `../worker.js`: the
|
|
33
|
+
// question "what else does a file share with the next one" now has somewhere
|
|
34
|
+
// to be answered, and a fourth answer is one entry rather than one more thing
|
|
35
|
+
// to remember.
|
|
36
|
+
//
|
|
37
|
+
// # What belongs here
|
|
38
|
+
//
|
|
39
|
+
// State that (a) the process shares, (b) a test can change from inside a file,
|
|
40
|
+
// and (c) nothing else puts back. Anything a file merely *reads* does not
|
|
41
|
+
// belong here, and neither does anything the registry already clears — a spy
|
|
42
|
+
// is not on this list because `reset()` is what a spy lives in.
|
|
43
|
+
//
|
|
44
|
+
// # When it runs
|
|
45
|
+
//
|
|
46
|
+
// Before the next file is imported, not after the previous one is run. A file
|
|
47
|
+
// that throws while loading is still a file that has run code, and it still
|
|
48
|
+
// hands the next one whatever that code changed; putting things back on the
|
|
49
|
+
// way *in* covers the load failure and the crash as well as the ordinary end.
|
|
50
|
+
// It also means the first file in a worker starts from the same state as the
|
|
51
|
+
// tenth.
|
|
52
|
+
|
|
53
|
+
import { reset } from "./registry.js";
|
|
54
|
+
import { resetModuleState } from "./modules.js";
|
|
55
|
+
import { unstubAllEnvs, unstubAllGlobals } from "./namespace.js";
|
|
56
|
+
// Renamed at the door, for two reasons that agree. It reads as the resets
|
|
57
|
+
// beside it do — `reset`, `unstubAllEnvs`, `resetModuleState` are all
|
|
58
|
+
// verb-first, and so is what this does to the clock.
|
|
59
|
+
import { restoreRealClock } from "./timers.js";
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* One piece of process-wide state a file can change, and how to put it back.
|
|
63
|
+
*
|
|
64
|
+
* `what` is written for a person reading this list rather than for any code:
|
|
65
|
+
* nothing branches on it, and it is here because a list of five bare function
|
|
66
|
+
* references is a list nobody can check against the paragraph above it.
|
|
67
|
+
*/
|
|
68
|
+
type Shared = {|
|
|
69
|
+
readonly what: string,
|
|
70
|
+
readonly restore: () => void,
|
|
71
|
+
|};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Everything a file shares with the file after it, in the order it goes back.
|
|
75
|
+
*
|
|
76
|
+
* The order matters in one place and is harmless everywhere else: the clock
|
|
77
|
+
* goes back before the module stand-ins do, because a stand-in's factory runs
|
|
78
|
+
* on the next import and a factory that schedules anything under a leaked fake
|
|
79
|
+
* clock would schedule it into a clock nobody is going to advance.
|
|
80
|
+
*/
|
|
81
|
+
const SHARED: $ReadOnlyArray<Shared> = [
|
|
82
|
+
{
|
|
83
|
+
what: "the tests, hooks and spies this package registered",
|
|
84
|
+
restore: reset,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
// `process.env` belongs to the process. `uft.stubEnv("NODE_ENV",
|
|
88
|
+
// "production")` in one file is still set when the next one imports, and
|
|
89
|
+
// the file that fails is the one that read it.
|
|
90
|
+
what: "environment variables `uft.stubEnv` replaced",
|
|
91
|
+
restore: unstubAllEnvs,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
// `globalThis` likewise, and worse: a stubbed `fetch` makes the next file
|
|
95
|
+
// talk to a stand-in that does not know about it.
|
|
96
|
+
what: "globals `uft.stubGlobal` replaced",
|
|
97
|
+
restore: unstubAllGlobals,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
// Worse than a leaked value, and worse in a way that hides it. A leaked
|
|
101
|
+
// stub makes the next file read something wrong, which arrives as an
|
|
102
|
+
// assertion naming the value. A leaked clock makes the next file's
|
|
103
|
+
// `setTimeout` never fire — including the one `withTimeout` races each
|
|
104
|
+
// case against — so the file hangs with nothing on screen until `uf`'s own
|
|
105
|
+
// deadline kills the worker, and the report names the file that waited
|
|
106
|
+
// rather than the file that stopped time.
|
|
107
|
+
what: "the clock, whatever `uft.useFakeTimers` did to it",
|
|
108
|
+
restore: restoreRealClock,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
// A worker serves many files out of one module registry, so this is the
|
|
112
|
+
// difference between "one file at a time" and "one file's mocks at a
|
|
113
|
+
// time".
|
|
114
|
+
what: "modules `uft.mock` stood in for",
|
|
115
|
+
restore: resetModuleState,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
what: "the document, if this process has one",
|
|
119
|
+
restore: restoreDocument,
|
|
120
|
+
},
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Hand the next file a document nobody else has written to.
|
|
125
|
+
*
|
|
126
|
+
* The document is process-wide in a way that is easy to miss, because nothing
|
|
127
|
+
* in this package installs it: `@uniflowed/react-testing` puts one on the
|
|
128
|
+
* global object the first time a test renders and deliberately keeps it for
|
|
129
|
+
* the life of the process — replacing it would strand every React root already
|
|
130
|
+
* mounted in the old one. So one document serves every file a worker runs, and
|
|
131
|
+
* what a file leaves in it is what the next file queries.
|
|
132
|
+
*
|
|
133
|
+
* Its *contents* are put back rather than the document itself, and "back"
|
|
134
|
+
* means empty: a file is handed the body it would have had if it had installed
|
|
135
|
+
* the document itself. `cleanup()` already unmounts what `render` mounted, and
|
|
136
|
+
* that is not the leak — the leak is markup a test wrote into the body by
|
|
137
|
+
* hand, which a hydration test must do because hydration is React attaching to
|
|
138
|
+
* markup that is already there. `rsc-split.test.js` and `streaming.test.js`
|
|
139
|
+
* both `replaceChildren` into the body, and the file after them in that worker
|
|
140
|
+
* started with somebody else's page.
|
|
141
|
+
*
|
|
142
|
+
* Read through `globalThis` and guarded by `typeof`, because a worker running
|
|
143
|
+
* a suite that never renders has no `document` at all and must not pay for
|
|
144
|
+
* one — and because on a host that *is* a browser this is the page, whose body
|
|
145
|
+
* a run of `uf test` has no business emptying. It only ever clears a body that
|
|
146
|
+
* a test process is using as scratch space, which is every case this can
|
|
147
|
+
* reach: the shim's document, or a page the project chose to run its own tests
|
|
148
|
+
* in.
|
|
149
|
+
*/
|
|
150
|
+
function restoreDocument(): void {
|
|
151
|
+
if (typeof globalThis.document === "undefined") {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const body = globalThis.document.body;
|
|
155
|
+
if (body == null) {
|
|
156
|
+
// A document a parser produced need not have one, and a document with no
|
|
157
|
+
// body is a document with nothing to put back.
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
body.replaceChildren();
|
|
161
|
+
// The attributes too, and not for tidiness: `<body class="dark">` is how a
|
|
162
|
+
// theme test says what it is testing, and a file that leaves one behind
|
|
163
|
+
// makes the next file's "the page is in light mode" false for a reason it
|
|
164
|
+
// cannot see. Read into an array first — removing an attribute while
|
|
165
|
+
// iterating a live list is the loop that skips every other entry.
|
|
166
|
+
for (const name of [...body.getAttributeNames()]) {
|
|
167
|
+
body.removeAttribute(name);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Where other packages register process-wide state of their own.
|
|
173
|
+
*
|
|
174
|
+
* A package this one does not depend on can still install something a file
|
|
175
|
+
* changes and the next file reads: `@uniflowed/react-testing` installs a window
|
|
176
|
+
* on the first render and keeps it for the process. Only that package knows
|
|
177
|
+
* what the window looked like when it made it, so it registers how to put it
|
|
178
|
+
* back — a name for the message below, and a function — in a `Map` under this
|
|
179
|
+
* symbol, and every entry runs after the list above.
|
|
180
|
+
*
|
|
181
|
+
* A symbol from the global registry rather than an export, so the package that
|
|
182
|
+
* registers needs no import of this one: under another runner the entry is
|
|
183
|
+
* never read and costs nothing.
|
|
184
|
+
*/
|
|
185
|
+
const SHARED_STATE: symbol = Symbol.for("@uniflowed/test/shared-state");
|
|
186
|
+
|
|
187
|
+
/** Every entry another package registered, as this module's own entries are shaped. */
|
|
188
|
+
function registeredElsewhere(): $ReadOnlyArray<Shared> {
|
|
189
|
+
const registry: mixed = Reflect.get(globalThis, SHARED_STATE);
|
|
190
|
+
if (!(registry instanceof Map)) {
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
const entries: Array<Shared> = [];
|
|
194
|
+
for (const [what, restore] of registry) {
|
|
195
|
+
if (typeof what === "string" && typeof restore === "function") {
|
|
196
|
+
entries.push({ what, restore: () => void restore() });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return entries;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Put back everything the file that just ran may have changed.
|
|
204
|
+
*
|
|
205
|
+
* Called by `../worker.js` before it imports the next file. Nothing here
|
|
206
|
+
* reports a file for having changed any of this: a file is *allowed* to — that
|
|
207
|
+
* is what the `uft` namespace is for — and the contract is that the change
|
|
208
|
+
* does not outlive the file, not that it never happened.
|
|
209
|
+
*
|
|
210
|
+
* Every entry is attempted even after one has thrown, and the first failure is
|
|
211
|
+
* raised afterwards under the name of what it could not put back. Stopping at
|
|
212
|
+
* the first would leave the four entries behind it un-restored, which is the
|
|
213
|
+
* defect this module exists to prevent arriving by a new route — and a bare
|
|
214
|
+
* throw from one of these used to say only that something in the runner failed
|
|
215
|
+
* between two files.
|
|
216
|
+
*/
|
|
217
|
+
export function restoreSharedState(): void {
|
|
218
|
+
let failure: { readonly what: string, readonly thrown: mixed } | null = null;
|
|
219
|
+
for (const shared of [...SHARED, ...registeredElsewhere()]) {
|
|
220
|
+
try {
|
|
221
|
+
shared.restore();
|
|
222
|
+
} catch (thrown) {
|
|
223
|
+
failure ??= { what: shared.what, thrown };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (failure != null) {
|
|
227
|
+
const cause = failure.thrown;
|
|
228
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
229
|
+
throw new Error(`@uniflowed/test could not put back ${failure.what}: ${message}`);
|
|
230
|
+
}
|
|
231
|
+
}
|