Skip to content

Commit 5197cfc

Browse files
authored
Merge pull request #246 from taras/feat/testing-component
✨ Register <Testing> as an ordinary function component
2 parents 9282db0 + 1e0a37c commit 5197cfc

3 files changed

Lines changed: 82 additions & 38 deletions

File tree

‎packages/testing/src/components.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
import { Err } from "effection";
1818
import type { Operation } from "effection";
19-
import { Component, Execution } from "@executablemd/core";
19+
import { Component, Execution, registerComponents } from "@executablemd/core";
2020
import type { DocumentExecution } from "@executablemd/core";
2121
import { boundary, record, Test, TestFailureError } from "./test-api.ts";
2222
import type { BoundaryOutcome } from "./test-api.ts";
2323
import { readCompletedRun } from "./journal.ts";
2424
import { ASSERTIONS } from "./assertions.ts";
2525
import { createTestHandlers } from "./handlers.ts";
26+
import { Testing, TESTING_PROPS } from "./testing-component.ts";
2627
import type { TestHandlers } from "./handlers.ts";
2728

2829
const TEST_TIMEOUT_MS = 20_000;
@@ -42,11 +43,14 @@ export function* installHandlers(
4243
if (options?.verbose) {
4344
yield* Test.around({ verbose: () => true });
4445
}
46+
// `<Testing>` is a registered default: a repository component with that name
47+
// replaces it, as with any other. The rest still claim their names until
48+
// #202 PR 3b and PR 4 move them.
49+
yield* registerComponents([
50+
{ name: "Testing", origin: "@executablemd/testing", fn: Testing, props: TESTING_PROPS },
51+
]);
4552
yield* Component.around({
4653
*expand([element], next) {
47-
if (element.name === "Testing") {
48-
return { segments: yield* handlers.expandTesting(element) };
49-
}
5054
if (element.name === "Test") {
5155
return { segments: yield* handlers.expandTest(element) };
5256
}

‎packages/testing/src/handlers.ts‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class TeardownError extends Error {
6565
}
6666

6767
export interface TestHandlers {
68-
expandTesting(element: ComponentElement): Operation<Segment[]>;
6968
expandTest(element: ComponentElement): Operation<Segment[]>;
7069
expandAssertion(assertion: AssertionEntry, element: ComponentElement): Operation<Segment[]>;
7170
expandAssertThrows(element: ComponentElement): Operation<Segment[]>;
@@ -74,38 +73,6 @@ export interface TestHandlers {
7473
export function createTestHandlers(options: { timeoutMs: number }): TestHandlers {
7574
const { timeoutMs } = options;
7675

77-
function* expandTesting(element: ComponentElement): Operation<Segment[]> {
78-
return yield* scoped(function* () {
79-
const local: TestResult[] = [];
80-
yield* Test.around(
81-
{
82-
testing: () => true,
83-
// deno-lint-ignore require-yield
84-
*results() {
85-
return local;
86-
},
87-
*record([result], next) {
88-
local.push(result);
89-
yield* next(result);
90-
},
91-
},
92-
{ at: "min" },
93-
);
94-
const report = yield* expandSegments(element.children);
95-
// Journal the outcome before the root Close so a full replay can
96-
// restore it without re-expanding this boundary.
97-
const outcome = yield* persistBoundaryOutcome(
98-
{
99-
tests: local.length,
100-
failed: local.filter((result) => result.status === "fail").length,
101-
},
102-
formatLocation(element),
103-
);
104-
yield* boundary(outcome);
105-
return report;
106-
});
107-
}
108-
10976
function* expandTest(element: ComponentElement): Operation<Segment[]> {
11077
if (!(yield* testing)) {
11178
return [];
@@ -380,7 +347,7 @@ export function createTestHandlers(options: { timeoutMs: number }): TestHandlers
380347
];
381348
}
382349

383-
return { expandTesting, expandTest, expandAssertion, expandAssertThrows };
350+
return { expandTest, expandAssertion, expandAssertThrows };
384351
}
385352

386353
function* failAssertThrows(matcher: string | RegExp, actual: string | undefined): Operation<never> {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* `<Testing>` as an ordinary function component (spec §5.3).
3+
*
4+
* It marks a region as a testing boundary: tests inside it run, their results
5+
* collect here, and the boundary reports how many ran and how many failed.
6+
*
7+
* Its body is projected with `tryContent()` rather than `content()` because
8+
* `<Testing>` does not intercept raised segments — that is `<Test>`'s job — so
9+
* an error written beside a test settles where it was written and the boundary
10+
* still reports. `content()` would replace the whole boundary with that failure
11+
* and lose the report with it.
12+
*/
13+
14+
import { hasContent, invocation, tryContent } from "@executablemd/core";
15+
import type { ComponentInvocationMetadata, Json, PropsSchema } from "@executablemd/core";
16+
import type { Operation } from "effection";
17+
import { Test, boundary } from "./test-api.ts";
18+
import type { TestResult } from "./test-api.ts";
19+
import { persistBoundaryOutcome } from "./journal.ts";
20+
21+
export const TESTING_PROPS: PropsSchema = {
22+
type: "object",
23+
properties: {},
24+
additionalProperties: false,
25+
};
26+
27+
/** `path:line:column`, `line:column`, or `unknown` — the durable boundary key. */
28+
function formatLocation(metadata: ComponentInvocationMetadata): string {
29+
const position = metadata.position;
30+
if (!position) {
31+
return "unknown";
32+
}
33+
const at = `${position.line}:${position.column}`;
34+
return position.path ? `${position.path}:${at}` : at;
35+
}
36+
37+
export function* Testing(): Operation<Json> {
38+
const local: TestResult[] = [];
39+
yield* Test.around(
40+
{
41+
testing: () => true,
42+
// deno-lint-ignore require-yield
43+
*results() {
44+
return local;
45+
},
46+
*record([result], next) {
47+
local.push(result);
48+
yield* next(result);
49+
},
50+
},
51+
{ at: "min" },
52+
);
53+
54+
const projected = (yield* hasContent()) ? yield* tryContent() : { text: "" };
55+
56+
// Journal the outcome before the root Close so a full replay can restore it
57+
// without re-expanding this boundary.
58+
const outcome = yield* persistBoundaryOutcome(
59+
{
60+
tests: local.length,
61+
failed: local.filter((result) => result.status === "fail").length,
62+
},
63+
formatLocation(yield* invocation()),
64+
);
65+
yield* boundary(outcome);
66+
67+
// A body that stopped rather than collected is still the caller's failure to
68+
// settle: the report is what rendered, and the failure travels on.
69+
if (projected.failure !== undefined) {
70+
throw projected.failure;
71+
}
72+
return projected.text;
73+
}

0 commit comments

Comments
 (0)