Repository navigation
Make empty web test discovery version-stable #8946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
499fbe1
36c99ec
35340bd
a79e402
f1e1f41
fc475d3
eff14c0
990bf08
362548d
98e1819
cb19b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| WEB_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| cd "$WEB_ROOT" | ||
|
|
||
| default_test_file_exists() { | ||
| while IFS= read -r -d '' file; do | ||
| case "$file" in | ||
| *.js | *.jsx | *.ts | *.tsx | *.mjs | *.cjs | *.mts | *.cts) ;; | ||
| *) continue ;; | ||
| esac | ||
| case "${file##*/}" in | ||
| *".test"* | *"_test_"* | *".spec"* | *"_spec_"*) return 0 ;; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The basename substrings do not match Bun's default filename rules: Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| esac | ||
| done < <( | ||
| find . \ | ||
| \( -path "./node_modules" -o -path "./.next" \) -prune \ | ||
| -o -type f -print0 | ||
| ) | ||
| return 1 | ||
| } | ||
|
|
||
| if (( $# == 0 )) && ! default_test_file_exists; then | ||
| echo "cmux web test runner found no test files" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| exec bun test --isolate "$@" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { | ||
| copyFileSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| rmSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const webRoot = fileURLToPath(new URL("..", import.meta.url)); | ||
| const runnerPath = fileURLToPath( | ||
| new URL("../scripts/run-tests.sh", import.meta.url), | ||
| ); | ||
| const fixtureTestSource = ` | ||
| import { test } from "bun:test"; | ||
| test("fixture runs", () => {}); | ||
| `; | ||
|
|
||
| test("shared web test runner isolates module mocks across files", () => { | ||
| const result = runChild( | ||
| process.execPath, | ||
| [ | ||
| "run", | ||
| "test", | ||
| "--", | ||
| "--randomize", | ||
| "--seed=1", | ||
| "tests/feedback-route.test.ts", | ||
| "tests/founders-welcome-route.test.ts", | ||
| ], | ||
| webRoot, | ||
| ); | ||
|
|
||
| if (result.status !== 0) { | ||
| throw new Error( | ||
| `shared web test runner leaked module state:\n${result.output}`, | ||
| ); | ||
| } | ||
| expect(result.output).toContain("tests/feedback-route.test.ts:"); | ||
| expect(result.output).toContain("tests/founders-welcome-route.test.ts:"); | ||
| expect(result.output).toContain("0 fail"); | ||
| }); | ||
|
|
||
| test("shared web test runner preserves recursive default discovery", () => { | ||
| const fixtureRoot = createRunnerFixture(); | ||
| try { | ||
| mkdirSync(join(fixtureRoot, "tests", "nested"), { recursive: true }); | ||
| writeFileSync( | ||
| join(fixtureRoot, "tests", "top-level.test.ts"), | ||
| fixtureTestSource, | ||
| ); | ||
| writeFileSync( | ||
| join(fixtureRoot, "tests", "nested", "nested.test.ts"), | ||
| fixtureTestSource, | ||
| ); | ||
|
|
||
| const result = runChild( | ||
| "/bin/bash", | ||
| ["scripts/run-tests.sh"], | ||
| fixtureRoot, | ||
| ); | ||
| if (result.status !== 0) { | ||
| throw new Error( | ||
| `shared web test runner skipped recursive discovery:\n${result.output}`, | ||
| ); | ||
| } | ||
| expect(result.output).toContain("tests/top-level.test.ts:"); | ||
| expect(result.output).toContain("tests/nested/nested.test.ts:"); | ||
| } finally { | ||
| rmSync(fixtureRoot, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| test("shared web test runner fails when default discovery finds no tests", () => { | ||
| const fixtureRoot = createRunnerFixture(); | ||
| try { | ||
| mkdirSync(join(fixtureRoot, "tests")); | ||
| const result = runChild( | ||
| "/bin/bash", | ||
| ["scripts/run-tests.sh"], | ||
| fixtureRoot, | ||
| ); | ||
| expect(result.status).not.toBe(0); | ||
| expect(result.output).toContain( | ||
| "cmux web test runner found no test files", | ||
| ); | ||
| } finally { | ||
| rmSync(fixtureRoot, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| function createRunnerFixture(): string { | ||
| const fixtureRoot = mkdtempSync(join(tmpdir(), "cmux-web-test-runner-")); | ||
| mkdirSync(join(fixtureRoot, "scripts")); | ||
| copyFileSync(runnerPath, join(fixtureRoot, "scripts", "run-tests.sh")); | ||
| return fixtureRoot; | ||
| } | ||
|
|
||
| function runChild( | ||
| command: string, | ||
| args: string[], | ||
| cwd: string, | ||
| ): { status: number | null; output: string } { | ||
| const result = spawnSync(command, args, { | ||
| cwd, | ||
| encoding: "utf8", | ||
| timeout: 30_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| const output = [result.stdout, result.stderr].join("\n"); | ||
|
|
||
| if (result.error) { | ||
| throw new Error( | ||
| `shared web test runner did not finish: ${result.error.message}\n${output}`, | ||
| ); | ||
| } | ||
| return { status: result.status, output }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This invokes the new runner with no arguments, but the fixture contains only
web/scripts/run-tests.shand the fakebunexecutable. Consequently,default_test_file_existsexits before the fake Bun is called; runningbash tests/test_ci_self_hosted_guard.shproducescmux web test runner found no test filesfollowed byFAIL: shared web test runner default discovery should execute. Since.github/workflows/ci.ymlruns this script inworkflow-guard-tests, that required job now fails on every run; create a correctly named fixture test before invoking the runner.Useful? React with 👍 / 👎.