Skip to content
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

Implement bun test --timeout #3040

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const getAllocator = @import("../base.zig").getAllocator;
const JSPrivateDataPtr = @import("../base.zig").JSPrivateDataPtr;
const GetJSPrivateData = @import("../base.zig").GetJSPrivateData;

const default_timeout = std.time.ms_per_min * 5;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from std.time.ms_per_min to std.time.ms_per_s. This appeared to be a bug, since the default should be 5 seconds, not 5 minutes.


const ZigString = JSC.ZigString;
const JSInternalPromise = JSC.JSInternalPromise;
const JSPromise = JSC.JSPromise;
Expand Down Expand Up @@ -360,8 +358,6 @@ pub const TestRunner = struct {
only: bool = false,
last_file: u64 = 0,

timeout_seconds: f64 = 5.0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused.


allocator: std.mem.Allocator,
callback: *Callback = undefined,

Expand All @@ -376,6 +372,7 @@ pub const TestRunner = struct {

snapshots: Snapshots,

default_timeout_ms: u32 = 0,
test_timeout_timer: ?*bun.uws.Timer = null,
last_test_timeout_timer_duration: u32 = 0,
active_test_for_timeout: ?TestRunner.Test.ID = null,
Expand Down Expand Up @@ -3243,7 +3240,7 @@ pub const TestScope = struct {
skipped: bool = false,
is_todo: bool = false,
snapshot_count: usize = 0,
timeout_millis: u32 = default_timeout,
timeout_millis: u32 = 0,

pub const Class = NewClass(
void,
Expand Down Expand Up @@ -3382,7 +3379,7 @@ pub const TestScope = struct {
.label = label,
.callback = function.asObjectRef(),
.parent = DescribeScope.active,
.timeout_millis = if (arguments.len > 2) @intCast(u32, @max(args[2].coerce(i32, ctx), 0)) else default_timeout,
.timeout_millis = if (arguments.len > 2) @intCast(u32, @max(args[2].coerce(i32, ctx), 0)) else Jest.runner.?.default_timeout_ms,
}) catch unreachable;

if (test_elapsed_timer == null) create_tiemr: {
Expand Down
10 changes: 10 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub const Arguments = struct {

// TODO: update test completions
const test_only_params = [_]ParamType{
clap.parseParam("--timeout <NUMBER> Set the per-test timeout in milliseconds, default is 5000.") catch unreachable,
clap.parseParam("--update-snapshots Update snapshot files") catch unreachable,
clap.parseParam("--rerun-each <NUMBER> Re-run each test file <NUMBER> times, helps catch certain bugs") catch unreachable,
};
Expand Down Expand Up @@ -371,6 +372,14 @@ pub const Arguments = struct {
}

if (cmd == .TestCommand) {
if (args.option("--timeout")) |timeout_ms| {
if (timeout_ms.len > 0) {
ctx.test_options.default_timeout_ms = std.fmt.parseInt(u32, timeout_ms, 10) catch {
Output.prettyErrorln("<r><red>error<r>: Invalid timeout: \"{s}\"", .{timeout_ms});
Global.exit(1);
};
}
}
ctx.test_options.update_snapshots = args.flag("--update-snapshots");
if (args.option("--rerun-each")) |repeat_count| {
if (repeat_count.len > 0) {
Expand Down Expand Up @@ -904,6 +913,7 @@ pub const Command = struct {
};

pub const TestOptions = struct {
default_timeout_ms: u32 = 5 * std.time.ms_per_s,
update_snapshots: bool = false,
repeat_count: u32 = 0,
};
Expand Down
1 change: 1 addition & 0 deletions src/cli/test_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ pub const TestCommand = struct {
.allocator = ctx.allocator,
.log = ctx.log,
.callback = undefined,
.default_timeout_ms = ctx.test_options.default_timeout_ms,
.snapshots = Snapshots{
.allocator = ctx.allocator,
.update_snapshots = ctx.test_options.update_snapshots,
Expand Down
66 changes: 66 additions & 0 deletions test/cli/test/bun-test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { join } from "node:path";
import { tmpdir } from "node:os";
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
import { spawnSync } from "bun";
import { describe, test, expect } from "bun:test";
import { bunExe, bunEnv } from "harness";

describe("bun test", () => {
describe("--timeout", () => {
test("must provide a number timeout", () => {
const stderr = runTest({
args: ["--timeout", "foo"],
});
expect(stderr).toContain("Invalid timeout");
});
test("must provide non-negative timeout", () => {
const stderr = runTest({
args: ["--timeout", "-1"],
});
expect(stderr).toContain("Invalid timeout");
});
test("timeout can be set to 1ms", () => {
const stderr = runTest({
args: ["--timeout", "1"],
code: `
import { test, expect } from "bun:test";
import { sleep } from "bun";
test("timeout", async () => {
await sleep(2);
});
`,
});
expect(stderr).toContain("timed out after 1ms");
});
test("timeout should default to 5000ms", () => {
const stderr = runTest({
code: `
import { test, expect } from "bun:test";
import { sleep } from "bun";
test("timeout", async () => {
await sleep(5001);
});
`,
});
expect(stderr).toContain("timed out after 5000ms");
});
});
});

function runTest({ code = "", args = [] }: { code?: string; args?: string[] }): string {
const dir = mkdtempSync(join(tmpdir(), "bun-test-"));
const path = join(dir, `bun-test-${Date.now()}.test.ts`);
writeFileSync(path, code);
try {
const { stderr } = spawnSync({
cwd: dir,
cmd: [bunExe(), "test", path, ...args],
env: bunEnv,
stderr: "pipe",
stdout: "ignore",
});
return stderr.toString();
} finally {
rmSync(path);
}
}