Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/runtime/cli/test_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2039,9 +2039,11 @@ impl TestCommand {
Ok(()) => {}
Err(scanner::ScanError::OutOfMemory) => bun::out_of_memory(),
// don't error if multiple are passed; one might fail
// but the others may not
// but the others may not. With --pass-with-no-tests, fall
// through to the shared "did not match any test files"
// path, which reports and exits 0.
Err(scanner::ScanError::DoesNotExist) => {
if file_or_dirnames.len() == 1 {
if file_or_dirnames.len() == 1 && !ctx.test_options.pass_with_no_tests {
if Output::is_ai_agent() {
pretty_errorln!(
"Test filter <b>{}<r> had no matches in --cwd={}",
Expand Down
38 changes: 38 additions & 0 deletions test/cli/test/pass-with-no-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,44 @@ test("--pass-with-no-tests exits with 0 when filters match no tests", async () =
expect(exitCode).toBe(0);
});

test.each([
["a path that does not exist", ["./does-not-exist"]],
["several paths that do not exist", ["./does-not-exist", "./also-missing"]],
["a directory with no test files", ["./empty"]],
])("--pass-with-no-tests exits with 0 when given %s", async (_label, paths) => {
using dir = tempDir("pass-with-no-tests-path", {
"some.test.ts": `import { test } from "bun:test"; test("example", () => {});`,
"empty/not-a-test.ts": `console.log("hello");`,
});

await using failing = Bun.spawn({
cmd: [bunExe(), "test", ...paths],
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
stdin: "ignore",
env: bunEnv,
});
const [failErr, failExitCode] = await Promise.all([failing.stderr.text(), failing.exited]);
expect(failErr).toContain(paths[0]);
expect(failErr).not.toContain("example");
expect(failExitCode).toBe(1);

await using passing = Bun.spawn({
cmd: [bunExe(), "test", ...paths, "--pass-with-no-tests"],
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
stdin: "ignore",
env: bunEnv,
});
const [passErr, passExitCode] = await Promise.all([passing.stderr.text(), passing.exited]);
expect(passErr).toContain("The following filters did not match any test files:");
expect(passErr).toContain(paths[0]);
expect(passErr).not.toContain("example");
expect(passExitCode).toBe(0);
});

test("without --pass-with-no-tests, exits with 1 when no test files found", async () => {
using dir = tempDir("fail-with-no-tests", {
"not-a-test.ts": `console.log("hello");`,
Expand Down
Loading