Skip to content

Commit 5670fea

Browse files
evanyeungmeta-codesync[bot]
authored andcommitted
Add // expected-to-throw fixtures; pin abstract client edge GC bug
Differential Revision: D131013633 fbshipit-source-id: 5d73a86c6e10de0c69b2e4ed979747272c9f6aea
1 parent 2ad6ec9 commit 5670fea

6 files changed

Lines changed: 427 additions & 3 deletions

File tree

‎packages/relay-e2e-test/.llms/skills/relay-e2e-test/references/writing-fixtures.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ Every fixture runs twice, on graphql 16 and on graphql 17 (see `jest.config.js`)
166166

167167
Always commit the `.snap.md` alongside the fixture. Jest treats a CI environment as `--ci` automatically (via `ci-info`, which checks `CI` among others), so a fixture landed without its snapshot fails there rather than silently writing one and passing forever.
168168

169+
### Expected runtime errors
170+
171+
An uncaught error fails the test: for example, an error that React reports when it renders after an update or a Suspense retry, or an error thrown by an event handler. To pin a known runtime bug instead, make this the first line of the fixture, like the comment that marks a Relay compiler fixture that expects an error:
172+
173+
```
174+
// expected-to-throw
175+
```
176+
177+
A marked fixture passes only if an uncaught error occurs while the app renders or runs its steps. The harness writes the error into a `## Runtime Error` section of the snapshot, after `## Type Errors`:
178+
179+
````markdown
180+
## Runtime Error
181+
182+
```
183+
Invariant Violation: Expected to find record for live resolver.
184+
```
185+
````
186+
187+
If no uncaught error occurs, the fixture fails. So when the bug is fixed, remove the first line and update the snapshot. The marker counts only on the first line.
188+
189+
After the error, React has unmounted the app, so the harness does not report a step that fails after it. Write the steps for the behavior you expect once the bug is fixed (for example, end with a `wait` for the correct result), so the error occurs while they run. Compile errors, type errors, and a step that fails before the error are reported as usual. An error that React throws back into the test inside `act()` (for example, from the first render) also fails the test as usual, and an error that an error boundary catches is not uncaught.
190+
191+
`fixtures/gc/abstract-client-edge.md` is the worked example.
192+
169193
## Relay docs
170194

171195
To understand how Relay APIs work, consult the Relay docs source files:

‎packages/relay-e2e-test/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Each fixture is typechecked with `tsc` after the Relay compiler runs, against th
6363

6464
The repo's other TypeScript check, `yarn typecheck:ts`, only verifies that `relay-runtime`'s declarations are internally well-formed. The e2e suite is what checks they are actually *usable*.
6565

66+
## Expected runtime errors
67+
68+
An uncaught error fails a fixture. A fixture whose first line is `// expected-to-throw` (the comment that marks a Relay compiler fixture that expects an error) passes only if one occurs, and the error is written into a `## Runtime Error` section of its snapshot. Use it to pin a known runtime bug until it is fixed — see the [writing guide](.llms/skills/relay-e2e-test/references/writing-fixtures.md#expected-runtime-errors).
69+
6670
## Writing fixtures
6771

6872
For the complete writing guide including fixture format, server/client code patterns, the interaction DSL, and snapshot behavior, see [`.llms/skills/relay-e2e-test/SKILL.md`](.llms/skills/relay-e2e-test/SKILL.md).

‎packages/relay-e2e-test/__tests__/fixtures-test.js‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ for (const run of fixtureFiles.flatMap(getRuns)) {
9999
test(testName, async () => {
100100
const markdown = readFileSync(path.join(fixturesDir, file), 'utf-8');
101101
const {codeBlocks, hosts, steps} = parseMarkdown(markdown);
102+
103+
// As in the compiler's fixtures, a first line of `// expected-to-throw`
104+
// means the fixture expects an error: here, an uncaught error while the
105+
// app runs. jest-environment-jsdom fails the test on an uncaught error
106+
// unless the test listens for `error` events itself, so a marked fixture
107+
// listens and snapshots the error instead.
108+
const expectsRuntimeError =
109+
markdown.split('\n', 1)[0].trim() === '// expected-to-throw';
110+
const runtimeErrors: Array<string> = [];
111+
const onRuntimeError = (event: ErrorEvent) => {
112+
event.preventDefault();
113+
runtimeErrors.push(String(event.error ?? event.message));
114+
};
115+
102116
const tempDir = await setupTempDir(codeBlocks, {
103117
relayRscMode: hosts.length > 0 ? rscHost : null,
104118
});
@@ -119,6 +133,9 @@ for (const run of fixtureFiles.flatMap(getRuns)) {
119133
.mockImplementation((...args: Array<mixed>) => {
120134
logs.push(`error: ${formatConsoleArgs(args, rscHost != null)}`);
121135
});
136+
if (expectsRuntimeError) {
137+
window.addEventListener('error', onRuntimeError);
138+
}
122139

123140
let rsc = null;
124141
try {
@@ -151,7 +168,22 @@ for (const run of fixtureFiles.flatMap(getRuns)) {
151168

152169
// Run interactions from the steps block
153170
if (steps.length > 0) {
154-
await runInteractions(steps, rsc?.steps);
171+
try {
172+
await runInteractions(steps, rsc?.steps);
173+
} catch (error) {
174+
// A step that fails after an uncaught error is reported as that
175+
// error: after an uncaught render error, React unmounts the app.
176+
if (runtimeErrors.length === 0) {
177+
throw error;
178+
}
179+
}
180+
}
181+
if (expectsRuntimeError && runtimeErrors.length === 0) {
182+
throw new Error(
183+
`Fixture "${name}" starts with \`// expected-to-throw\`, but no ` +
184+
'uncaught error occurred. If the bug it pins is fixed, remove ' +
185+
'that line and update the snapshot.',
186+
);
155187
}
156188

157189
// Build snapshot string
@@ -165,6 +197,11 @@ for (const run of fixtureFiles.flatMap(getRuns)) {
165197
sections.push('```\n' + typeErrors.join('\n') + '\n```\n');
166198
}
167199

200+
if (runtimeErrors.length > 0) {
201+
sections.push('## Runtime Error\n');
202+
sections.push('```\n' + runtimeErrors.join('\n') + '\n```\n');
203+
}
204+
168205
if (logs.length > 0) {
169206
sections.push('## Console\n');
170207
sections.push('```\n' + logs.join('\n') + '\n```\n');
@@ -258,6 +295,11 @@ for (const run of fixtureFiles.flatMap(getRuns)) {
258295
}
259296
throw error;
260297
} finally {
298+
// jest-environment-jsdom counts `error` listeners, so remove only what
299+
// was added.
300+
if (expectsRuntimeError) {
301+
window.removeEventListener('error', onRuntimeError);
302+
}
261303
logSpy.mockRestore();
262304
warnSpy.mockRestore();
263305
errorSpy.mockRestore();

0 commit comments

Comments
 (0)