|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { bunEnv, bunExe, tempDir } from "harness"; |
| 3 | + |
| 4 | +// `bun test --shard=M/N` splits discovered test files across N shards. |
| 5 | +// Files are sorted by path first for determinism, then distributed |
| 6 | +// round-robin: file i goes to shard (i % N) + 1. |
| 7 | + |
| 8 | +function makeFixture(name: string, fileCount: number) { |
| 9 | + const files: Record<string, string> = {}; |
| 10 | + for (let i = 0; i < fileCount; i++) { |
| 11 | + const id = String(i).padStart(2, "0"); |
| 12 | + files[`f${id}.test.ts`] = `import { test } from "bun:test"; test("t", () => { console.log("RAN f${id}"); });`; |
| 13 | + } |
| 14 | + return tempDir(name, files); |
| 15 | +} |
| 16 | + |
| 17 | +async function runShard(cwd: string, shard: string, extra: string[] = []) { |
| 18 | + await using proc = Bun.spawn({ |
| 19 | + cmd: [bunExe(), "test", `--shard=${shard}`, ...extra], |
| 20 | + env: bunEnv, |
| 21 | + cwd, |
| 22 | + stdout: "pipe", |
| 23 | + stderr: "pipe", |
| 24 | + }); |
| 25 | + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); |
| 26 | + const ran = stdout |
| 27 | + .split("\n") |
| 28 | + .filter(l => l.startsWith("RAN ")) |
| 29 | + .map(l => l.slice(4)) |
| 30 | + .sort(); |
| 31 | + return { ran, stderr, exitCode }; |
| 32 | +} |
| 33 | + |
| 34 | +describe.concurrent("--shard", () => { |
| 35 | + test("partitions test files across shards with no overlap or gaps", async () => { |
| 36 | + using dir = makeFixture("shard-partition", 10); |
| 37 | + const cwd = String(dir); |
| 38 | + |
| 39 | + const results = await Promise.all(["1/3", "2/3", "3/3"].map(s => runShard(cwd, s))); |
| 40 | + for (const r of results) { |
| 41 | + expect(r.stderr).toContain("--shard="); |
| 42 | + expect(r.exitCode).toBe(0); |
| 43 | + } |
| 44 | + |
| 45 | + const all = results.flatMap(r => r.ran).sort(); |
| 46 | + // Every file ran exactly once across all shards. |
| 47 | + expect(all).toEqual(["f00", "f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09"]); |
| 48 | + |
| 49 | + // No overlap between shards. |
| 50 | + const seen = new Set<string>(); |
| 51 | + for (const r of results) { |
| 52 | + for (const f of r.ran) { |
| 53 | + expect(seen.has(f)).toBe(false); |
| 54 | + seen.add(f); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Round-robin over the sorted list: shard M gets indices M-1, M-1+N, ... |
| 59 | + expect(results[0].ran).toEqual(["f00", "f03", "f06", "f09"]); |
| 60 | + expect(results[1].ran).toEqual(["f01", "f04", "f07"]); |
| 61 | + expect(results[2].ran).toEqual(["f02", "f05", "f08"]); |
| 62 | + }); |
| 63 | + |
| 64 | + test("is deterministic across repeated runs", async () => { |
| 65 | + using dir = makeFixture("shard-determinism", 12); |
| 66 | + const cwd = String(dir); |
| 67 | + |
| 68 | + const [a, b] = await Promise.all([runShard(cwd, "2/4"), runShard(cwd, "2/4")]); |
| 69 | + expect(a.ran).toEqual(["f01", "f05", "f09"]); |
| 70 | + expect(a.ran).toEqual(b.ran); |
| 71 | + expect(a.exitCode).toBe(0); |
| 72 | + expect(b.exitCode).toBe(0); |
| 73 | + }); |
| 74 | + |
| 75 | + test("composes with --randomize: shard selection is independent of the seed", async () => { |
| 76 | + // Shard selection sorts, picks, then --randomize shuffles only the |
| 77 | + // selected subset. This test verifies the SET of files in a shard |
| 78 | + // is unaffected by randomization — every seed (and no seed) yields |
| 79 | + // the same shard membership. Shuffle-order determinism under a |
| 80 | + // fixed seed is covered by test-randomize.test.ts. |
| 81 | + using dir = makeFixture("shard-randomize", 12); |
| 82 | + const cwd = String(dir); |
| 83 | + |
| 84 | + const [plain, seeded1, seeded2, otherSeed] = await Promise.all([ |
| 85 | + runShard(cwd, "2/4"), |
| 86 | + runShard(cwd, "2/4", ["--seed=123"]), |
| 87 | + runShard(cwd, "2/4", ["--seed=123"]), |
| 88 | + runShard(cwd, "2/4", ["--seed=999999"]), |
| 89 | + ]); |
| 90 | + |
| 91 | + expect(plain.ran).toEqual(["f01", "f05", "f09"]); |
| 92 | + expect(seeded1.ran).toEqual(plain.ran); |
| 93 | + expect(seeded2.ran).toEqual(plain.ran); |
| 94 | + expect(otherSeed.ran).toEqual(plain.ran); |
| 95 | + |
| 96 | + expect(seeded1.stderr).toContain("--shard=2/4:"); |
| 97 | + for (const r of [plain, seeded1, seeded2, otherSeed]) expect(r.exitCode).toBe(0); |
| 98 | + }); |
| 99 | + |
| 100 | + test("--shard=1/1 runs every test file", async () => { |
| 101 | + using dir = makeFixture("shard-one", 5); |
| 102 | + const cwd = String(dir); |
| 103 | + |
| 104 | + const { ran, stderr, exitCode } = await runShard(cwd, "1/1"); |
| 105 | + expect(stderr).toContain("--shard=1/1:"); |
| 106 | + expect(ran).toEqual(["f00", "f01", "f02", "f03", "f04"]); |
| 107 | + expect(exitCode).toBe(0); |
| 108 | + }); |
| 109 | + |
| 110 | + test("prints the shard summary line", async () => { |
| 111 | + using dir = makeFixture("shard-summary", 7); |
| 112 | + const cwd = String(dir); |
| 113 | + |
| 114 | + const { stderr, exitCode } = await runShard(cwd, "2/3"); |
| 115 | + expect(stderr).toMatch(/--shard=2\/3: running \d+\/7 test files/); |
| 116 | + expect(exitCode).toBe(0); |
| 117 | + }); |
| 118 | + |
| 119 | + test("does not print shard summary when there are no test files", async () => { |
| 120 | + // With no test files at all, --shard should stay out of the way and |
| 121 | + // let the normal "No tests found!" error path handle it. |
| 122 | + using dir = tempDir("shard-no-files", { |
| 123 | + "not-a-test.ts": "export const x = 1;", |
| 124 | + }); |
| 125 | + await using proc = Bun.spawn({ |
| 126 | + cmd: [bunExe(), "test", "--shard=1/3"], |
| 127 | + env: bunEnv, |
| 128 | + cwd: String(dir), |
| 129 | + stdout: "pipe", |
| 130 | + stderr: "pipe", |
| 131 | + }); |
| 132 | + const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); |
| 133 | + expect(stderr).not.toContain("--shard="); |
| 134 | + expect(stderr).not.toContain("0/0"); |
| 135 | + expect(stderr.toLowerCase()).toContain("no tests found"); |
| 136 | + expect(exitCode).not.toBe(0); |
| 137 | + }); |
| 138 | + |
| 139 | + test("empty shard exits 0 without 'No tests found'", async () => { |
| 140 | + // 2 files, 5 shards → shards 3, 4, 5 get nothing. |
| 141 | + using dir = makeFixture("shard-empty", 2); |
| 142 | + const cwd = String(dir); |
| 143 | + |
| 144 | + const { ran, stderr, exitCode } = await runShard(cwd, "5/5"); |
| 145 | + expect(ran).toEqual([]); |
| 146 | + expect(stderr).toContain("--shard=5/5:"); |
| 147 | + expect(stderr).toContain("running 0/2 test files"); |
| 148 | + expect(stderr).not.toContain("No tests found"); |
| 149 | + expect(stderr).not.toContain("did not match any test files"); |
| 150 | + expect(exitCode).toBe(0); |
| 151 | + }); |
| 152 | + |
| 153 | + test.each([ |
| 154 | + ["0/3", "index must be between 1 and 3"], |
| 155 | + ["4/3", "index must be between 1 and 3"], |
| 156 | + ["1/0", "count must be greater than 0"], |
| 157 | + ["abc", "expects"], |
| 158 | + ["1/", "count must be a positive integer"], |
| 159 | + ["/3", "index must be a positive integer"], |
| 160 | + ["a/3", "index must be a positive integer"], |
| 161 | + ["1/b", "count must be a positive integer"], |
| 162 | + ])("rejects invalid --shard=%s", async (arg, needle) => { |
| 163 | + using dir = makeFixture("shard-invalid", 1); |
| 164 | + await using proc = Bun.spawn({ |
| 165 | + cmd: [bunExe(), "test", `--shard=${arg}`], |
| 166 | + env: bunEnv, |
| 167 | + cwd: String(dir), |
| 168 | + stdout: "pipe", |
| 169 | + stderr: "pipe", |
| 170 | + }); |
| 171 | + const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); |
| 172 | + expect(stderr).toContain(needle); |
| 173 | + expect(exitCode).not.toBe(0); |
| 174 | + }); |
| 175 | + |
| 176 | + test("composes with --parallel: shard filters first, then workers run the subset", async () => { |
| 177 | + // 4 files; --shard=1/2 keeps f00 and f02. --parallel runs that subset. |
| 178 | + // This proves the file filter happens before the coordinator distributes; |
| 179 | + // worker distribution itself is covered by the JEST_WORKER_ID test in |
| 180 | + // parallel.test.ts. |
| 181 | + const files: Record<string, string> = {}; |
| 182 | + for (let i = 0; i < 4; i++) { |
| 183 | + const id = String(i).padStart(2, "0"); |
| 184 | + files[`f${id}.test.ts`] = |
| 185 | + `import {test} from "bun:test"; test("t", () => console.log("RAN f${id} WID="+process.env.JEST_WORKER_ID));`; |
| 186 | + } |
| 187 | + using dir = tempDir("shard-parallel", files); |
| 188 | + await using proc = Bun.spawn({ |
| 189 | + cmd: [bunExe(), "test", "--shard=1/2", "--parallel=2"], |
| 190 | + env: { ...bunEnv, BUN_TEST_PARALLEL_SCALE_MS: "0" }, |
| 191 | + cwd: String(dir), |
| 192 | + stdout: "pipe", |
| 193 | + stderr: "pipe", |
| 194 | + }); |
| 195 | + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); |
| 196 | + const out = stdout + stderr; |
| 197 | + expect(stdout).toContain("PARALLEL"); |
| 198 | + expect(stderr).toContain("--shard=1/2:"); |
| 199 | + expect(stderr).toContain("running 2/4 test files"); |
| 200 | + const ran = [...out.matchAll(/RAN (f\d\d) WID=(\S+)/g)].map(m => ({ file: m[1], wid: m[2] })); |
| 201 | + // Only shard-1 files ran, in any worker: |
| 202 | + expect(ran.map(r => r.file).sort()).toEqual(["f00", "f02"]); |
| 203 | + // JEST_WORKER_ID is the local worker (1..K), never undefined and never the |
| 204 | + // shard index. With 2 sharded files and lazy spawn, K may collapse to 1. |
| 205 | + for (const r of ran) { |
| 206 | + expect(["1", "2"]).toContain(r.wid); |
| 207 | + } |
| 208 | + expect(exitCode).toBe(0); |
| 209 | + }); |
| 210 | +}); |
0 commit comments