aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-16 00:40:38 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-16 00:40:38 -0800
commit054d8739369a6f76a28e34f3cc3c1811677438cd (patch)
tree1849b1bd4354d849358ce2f05feea7f63b60c7c8
parent9f2f33e4e6ae2cf8abe9c1de825e2ab7aa31a190 (diff)
downloadbun-054d8739369a6f76a28e34f3cc3c1811677438cd.tar.gz
bun-054d8739369a6f76a28e34f3cc3c1811677438cd.tar.zst
bun-054d8739369a6f76a28e34f3cc3c1811677438cd.zip
[bun test] Implement `it.only`
-rw-r--r--src/javascript/jsc/test/jest.zig50
1 files changed, 45 insertions, 5 deletions
diff --git a/src/javascript/jsc/test/jest.zig b/src/javascript/jsc/test/jest.zig
index 80aedb6a6..11b2d7b8e 100644
--- a/src/javascript/jsc/test/jest.zig
+++ b/src/javascript/jsc/test/jest.zig
@@ -63,12 +63,23 @@ pub const TestRunner = struct {
log: *logger.Log,
files: File.List = .{},
index: File.Map = File.Map{},
+ only: bool = false,
timeout_seconds: f64 = 5.0,
allocator: std.mem.Allocator,
callback: *Callback = undefined,
+ pub fn setOnly(this: *TestRunner) void {
+ if (this.only) {
+ return;
+ }
+
+ this.only = true;
+ this.tests.shrinkRetainingCapacity(0);
+ this.callback.onUpdateCount(this.callback, 0, 0);
+ }
+
pub const Callback = struct {
pub const OnUpdateCount = fn (this: *Callback, delta: u32, total: u32) void;
pub const OnTestStart = fn (this: *Callback, test_id: Test.ID) void;
@@ -562,26 +573,48 @@ pub const TestScope = struct {
id: TestRunner.Test.ID = 0,
promise: ?*JSInternalPromise = null,
- pub const Class = NewClass(void, .{ .name = "test" }, .{ .call = call }, .{});
+ pub const Class = NewClass(void, .{ .name = "test" }, .{ .call = call, .only = only }, .{});
pub const Counter = struct {
expected: u32 = 0,
actual: u32 = 0,
};
- pub fn call(
+ pub fn only(
// the DescribeScope here is the top of the file, not the real one
_: void,
ctx: js.JSContextRef,
+ this: js.JSObjectRef,
_: js.JSObjectRef,
+ arguments: []const js.JSValueRef,
+ exception: js.ExceptionRef,
+ ) js.JSObjectRef {
+ return callMaybeOnly(this, ctx, arguments, exception, true);
+ }
+
+ pub fn call(
+ // the DescribeScope here is the top of the file, not the real one
+ _: void,
+ ctx: js.JSContextRef,
+ this: js.JSObjectRef,
_: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSObjectRef {
+ return callMaybeOnly(this, ctx, arguments, exception, false);
+ }
+
+ fn callMaybeOnly(
+ this: js.JSObjectRef,
+ ctx: js.JSContextRef,
+ arguments: []const js.JSValueRef,
+ exception: js.ExceptionRef,
+ is_only: bool,
+ ) js.JSObjectRef {
var args = arguments[0..@minimum(arguments.len, 2)];
var label: string = "";
if (args.len == 0) {
- return js.JSValueMakeUndefined(ctx);
+ return this;
}
if (js.JSValueIsString(ctx, args[0])) {
@@ -597,9 +630,16 @@ pub const TestScope = struct {
var function = args[0];
if (!js.JSValueIsObject(ctx, function) or !js.JSObjectIsFunction(ctx, function)) {
JSError(getAllocator(ctx), "test() expects a function", .{}, ctx, exception);
- return js.JSValueMakeUndefined(ctx);
+ return this;
+ }
+
+ if (is_only) {
+ Jest.runner.?.setOnly();
}
+ if (!is_only and Jest.runner.?.only)
+ return this;
+
js.JSValueProtect(ctx, function);
DescribeScope.active.tests.append(getAllocator(ctx), TestScope{
@@ -608,7 +648,7 @@ pub const TestScope = struct {
.parent = DescribeScope.active,
}) catch unreachable;
- return js.JSValueMakeUndefined(ctx);
+ return this;
}
pub const Result = union(TestRunner.Test.Status) {