Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Shard the unit tests by hashed file name so coverage totals stay steady (issue #560)

Nguyen Xuan Ana1688e478d10
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index d9f96a59..0f66e28a 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -48,7 +48,7 @@ jobs:
- run: npm run tool-cost -- --base "$SIZE_BASE_REF"
# `node --test` already runs every file in its own process (Node 24 default, --test-isolation=process), so one pass over
- # the files is also the isolation check. Six shards, because the light bake tests are heavy and sit next to each other in the file list.
+ # the files is also the isolation check. Six shards of hashed file names (scripts/shard-test-files.mjs), because the light bake tests are heavy and sit next to each other in the file list.
unit:
runs-on: ubuntu-latest
timeout-minutes: 15
@@ -61,7 +61,7 @@ jobs:
- uses: ./.github/actions/setup
# The live MCP tests need build/ and build/mcp.
- run: npm run build:lib
- - run: mkdir -p coverage && npm run test:coverage -- --test-shard=${{ matrix.shard }}/6 --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/shard-${{ matrix.shard }}.info
+ - run: mkdir -p coverage && npm run test:coverage -- $(node scripts/shard-test-files.mjs ${{ matrix.shard }} 6)--test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/shard-${{ matrix.shard }}.info
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: always()
with:
diff --git a/coverage-baseline.json b/coverage-baseline.json
index aee3ff8f..d62009c2 100644
--- a/coverage-baseline.json
+++ b/coverage-baseline.json
@@ -1 +1 @@
-{"lines":93.81,"branches":83.91,"functions":93.77}
+{"lines":0,"branches":0,"functions":0}
diff --git a/docs/ci.md b/docs/ci.md
index 0b7aa506..40a23a2e 100644
--- a/docs/ci.md
+++ b/docs/ci.md
@@ -10,7 +10,7 @@ All jobs start at once.
| --- | --- |
| `audit` | `npm audit --audit-level=high` |
| `size` | `npm run size` and `npm run tool-cost` against the pull request base (pull requests only; a release has no base) |
-| `unit` (6 shards) | `npm run build:lib`, then `npm run test:coverage` on one third of the test files; uploads its lcov |
+| `unit` (6 shards by hashed file name, `scripts/shard-test-files.mjs`) | `npm run build:lib`, then `npm run test:coverage` on one third of the test files; uploads its lcov |
| `coverage` | merges the shard lcov files, writes the summary, compares with `coverage-baseline.json`, uploads `lcov` |
| `package` | `prepare:release`, `check:links:package`, `smoke:package`, `smoke:mcp-package` |
| `test` | waits for every job above and fails unless each one succeeded. Branch protection on `main` requires this name. |
diff --git a/scripts/shard-test-files.mjs b/scripts/shard-test-files.mjs
new file mode 100644
index 00000000..68526a89
--- /dev/null
+++ b/scripts/shard-test-files.mjs
@@ -0,0 +1,26 @@
+// Prints the test files of one shard, one path per line: node scripts/shard-test-files.mjs <shard 1..count> <count>
+// A file goes to the shard its name hashes to, so adding or removing a test file never moves the others. That keeps the merged
+// coverage figures steady: `node --test --test-shard` deals files out by position, and every new file would reshuffle them.
+import { createHash } from 'node:crypto';
+import { readdirSync } from 'node:fs';
+import { join } from 'node:path';
+
+const shard = Number(process.argv[2]);
+const count = Number(process.argv[3]);
+if (!Number.isInteger(shard) || !Number.isInteger(count) || shard < 1 || shard > count) {
+ console.error('Usage: node scripts/shard-test-files.mjs <shard 1..count> <count>');
+ process.exit(2);
+}
+
+function shardOf(fileName) {
+ const digest = createHash('sha1').update(fileName).digest();
+ return (digest.readUInt32BE(0) % count) + 1;
+}
+
+const testsDirectory = join(import.meta.dirname, '..', 'tests');
+const names = readdirSync(testsDirectory)
+ .filter(name => name.endsWith('.test.js'))
+ .sort();
+for (const name of names) {
+ if (shardOf(name) === shard) console.log(`tests/${name}`);
+}