Skip to content

Commit 490a120

Browse files
ci: count typed test declarations and flag unit tests renamed to browser tests
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f927a1c commit 490a120

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎.github/scripts/test-delta.cjs‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
const MARKER = "<!-- test-delta -->";
55
const NET_CASES_TO_REPORT = 20;
66
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/;
7-
const TEST_CASE = /^\s*(?:it|test)(?:\.\w+)*\s*\(/;
7+
// `<` admits type arguments, as in `it.each<Row>(...)`, which may span lines.
8+
const TEST_CASE = /^\s*(?:it|test)(?:\.\w+)*\s*[(<]/;
9+
const isBrowserTest = (path) => path.includes(".browser.test.");
810

911
function kindOf(path) {
10-
if (path.includes(".browser.test.")) return "browser";
12+
if (isBrowserTest(path)) return "browser";
1113
if (path.startsWith("apps/server/") || path.includes(".integration.")) return "server";
1214
return "unit";
1315
}
@@ -17,11 +19,20 @@ function countTestDelta(diff) {
1719
const kinds = { browser: 0, server: 0, unit: 0 };
1820
const newBrowserFiles = [];
1921
let path = null;
22+
// A browser test file is new when it is added, or renamed from a file that was not one.
23+
let file = null;
24+
const flush = () => {
25+
if (file?.isNew && isBrowserTest(file.target)) newBrowserFiles.push(file.target);
26+
};
2027
for (const line of diff.split("\n")) {
2128
if (line.startsWith("diff --git ")) {
29+
flush();
2230
path = null;
31+
file = { target: line.replace(/^diff --git a\/\S+ b\//, ""), isNew: false };
2332
continue;
2433
}
34+
if (line.startsWith("new file mode") && file) file.isNew = true;
35+
if (line.startsWith("rename from ") && file) file.isNew = !isBrowserTest(line.slice(12));
2536
if (line.startsWith("--- ")) {
2637
const source = line.slice(4).replace(/^a\//, "");
2738
path = TEST_FILE.test(source) ? source : null;
@@ -37,9 +48,7 @@ function countTestDelta(diff) {
3748
const sign = line[0] === "+" ? 1 : line[0] === "-" ? -1 : 0;
3849
if (sign !== 0 && TEST_CASE.test(line.slice(1))) kinds[kindOf(path)] += sign;
3950
}
40-
for (const match of diff.matchAll(/^diff --git a\/\S+ b\/(\S+)\nnew file mode/gm)) {
41-
if (match[1].includes(".browser.test.")) newBrowserFiles.push(match[1]);
42-
}
51+
flush();
4352
return { kinds, net: kinds.browser + kinds.server + kinds.unit, newBrowserFiles };
4453
}
4554

‎.github/scripts/test-delta.test.cjs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ new file mode 100644
99
+++ b/apps/web/src/Panel.browser.test.tsx
1010
@@ -0,0 +1,4 @@
1111
+it("opens", async () => {});
12-
+ it.each([1, 2])("renders %i", async () => {});
12+
+ it.each<{ n: number }>([{ n: 1 }])("renders $n", async () => {});
1313
+// it("is a comment, still counted only when it starts the line")
1414
diff --git a/apps/server/src/thing.test.ts b/apps/server/src/thing.test.ts
1515
--- a/apps/server/src/thing.test.ts
@@ -24,18 +24,26 @@ deleted file mode 100644
2424
+++ /dev/null
2525
@@ -1,1 +0,0 @@
2626
-it("goes", () => {});
27+
diff --git a/apps/web/src/Menu.test.tsx b/apps/web/src/Menu.browser.test.tsx
28+
similarity index 100%
29+
rename from apps/web/src/Menu.test.tsx
30+
rename to apps/web/src/Menu.browser.test.tsx
31+
diff --git a/apps/web/src/Old.browser.test.tsx b/apps/web/src/New.browser.test.tsx
32+
similarity index 100%
33+
rename from apps/web/src/Old.browser.test.tsx
34+
rename to apps/web/src/New.browser.test.tsx
2735
diff --git a/apps/web/src/Panel.tsx b/apps/web/src/Panel.tsx
2836
--- a/apps/web/src/Panel.tsx
2937
+++ b/apps/web/src/Panel.tsx
3038
@@ -1 +1 @@
3139
+it("is not a test file", () => {});
3240
`;
3341

34-
test("counts added and removed cases by kind, including deleted files", () => {
42+
test("counts cases by kind, including deleted files, and finds new or renamed browser files", () => {
3543
assert.deepEqual(countTestDelta(diff), {
3644
kinds: { browser: 2, server: -1, unit: -1 },
3745
net: 0,
38-
newBrowserFiles: ["apps/web/src/Panel.browser.test.tsx"],
46+
newBrowserFiles: ["apps/web/src/Panel.browser.test.tsx", "apps/web/src/Menu.browser.test.tsx"],
3947
});
4048
});
4149

0 commit comments

Comments
 (0)