diff --git a/packages/bun-types/test.d.ts b/packages/bun-types/test.d.ts index 6dfb6709b43812..8edc263f33a944 100644 --- a/packages/bun-types/test.d.ts +++ b/packages/bun-types/test.d.ts @@ -490,7 +490,12 @@ declare module "bun:test" { export interface Expect extends AsymmetricMatchers { // the `expect()` callable signature - (actual?: T): Matchers; + /** + * @param actual the actual value + * @param customFailMessage an optional custom message to display if the test fails. + * */ + + (actual?: T, customFailMessage?: string): Matchers; /** * Access to negated asymmetric matchers. diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 0c1bee9933a4fc..803390161cc61d 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -4786,23 +4786,15 @@ static JSC::Identifier builtinNameMap(JSC::JSGlobalObject* globalObject, unsigne JSC__JSValue JSC__JSValue__fastGetDirect_(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject, unsigned char arg2) { JSC::JSValue value = JSC::JSValue::decode(JSValue0); - if (!value.isCell()) { - return JSValue::encode({}); - } - - return JSValue::encode( - value.getObject()->getDirect(globalObject->vm(), PropertyName(builtinNameMap(globalObject, arg2)))); + ASSERT(value.isCell()); + return JSValue::encode(value.getObject()->getDirect(globalObject->vm(), PropertyName(builtinNameMap(globalObject, arg2)))); } JSC__JSValue JSC__JSValue__fastGet_(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject, unsigned char arg2) { JSC::JSValue value = JSC::JSValue::decode(JSValue0); - if (!value.isCell()) { - return JSC::JSValue::encode(JSC::jsUndefined()); - } - - return JSValue::encode( - value.getObject()->getIfPropertyExists(globalObject, builtinNameMap(globalObject, arg2))); + ASSERT(value.isCell()); + return JSValue::encode( value.getObject()->getIfPropertyExists(globalObject, builtinNameMap(globalObject, arg2))); } bool JSC__JSValue__toBooleanSlow(JSC__JSValue JSValue0, JSC__JSGlobalObject* globalObject) diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index cc6e60d8f79474..648789dd92f032 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -4776,8 +4776,11 @@ pub const JSValue = enum(JSValueReprInt) { }; } - // intended to be more lightweight than ZigString + // `this` must be known to be an object + // intended to be more lightweight than ZigString. pub fn fastGet(this: JSValue, global: *JSGlobalObject, builtin_name: BuiltinName) ?JSValue { + if (bun.Environment.allow_assert) + bun.assert(this.isObject()); const result = fastGet_(this, global, @intFromEnum(builtin_name)); if (result == .zero or // JS APIs treat {}.a as mostly the same as though it was not defined @@ -4871,9 +4874,12 @@ pub const JSValue = enum(JSValueReprInt) { return if (@intFromEnum(value) != 0) value else return null; } + /// safe to use on any JSValue pub fn implementsToString(this: JSValue, global: *JSGlobalObject) bool { - bun.assert(this.isCell()); - const function = this.fastGet(global, BuiltinName.toString) orelse return false; + if (!this.isObject()) + return false; + const function = this.fastGet(global, BuiltinName.toString) orelse + return false; return function.isCell() and function.isCallable(global.vm()); } diff --git a/src/bun.js/test/diff_format.zig b/src/bun.js/test/diff_format.zig index 27e4bf78a04df3..5d28d2532cd9ef 100644 --- a/src/bun.js/test/diff_format.zig +++ b/src/bun.js/test/diff_format.zig @@ -15,7 +15,7 @@ pub const DiffFormatter = struct { expected_string: ?string = null, received: ?JSValue = null, expected: ?JSValue = null, - globalObject: *JSGlobalObject, + globalThis: *JSGlobalObject, not: bool = false, pub fn format(this: DiffFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { @@ -104,7 +104,7 @@ pub const DiffFormatter = struct { }; ConsoleObject.format2( .Debug, - this.globalObject, + this.globalThis, @as([*]const JSValue, @ptrCast(&received)), 1, Writer, @@ -118,7 +118,7 @@ pub const DiffFormatter = struct { ConsoleObject.format2( .Debug, - this.globalObject, + this.globalThis, @as([*]const JSValue, @ptrCast(&this.expected)), 1, Writer, @@ -142,21 +142,21 @@ pub const DiffFormatter = struct { return; } - switch (received.determineDiffMethod(expected, this.globalObject)) { + switch (received.determineDiffMethod(expected, this.globalThis)) { .none => { const fmt = "Expected: {any}\nReceived: {any}"; - var formatter = ConsoleObject.Formatter{ .globalThis = this.globalObject, .quote_strings = true }; + var formatter = ConsoleObject.Formatter{ .globalThis = this.globalThis, .quote_strings = true }; if (Output.enable_ansi_colors) { try writer.print(Output.prettyFmt(fmt, true), .{ - expected.toFmt(this.globalObject, &formatter), - received.toFmt(this.globalObject, &formatter), + expected.toFmt(this.globalThis, &formatter), + received.toFmt(this.globalThis, &formatter), }); return; } try writer.print(Output.prettyFmt(fmt, true), .{ - expected.toFmt(this.globalObject, &formatter), - received.toFmt(this.globalObject, &formatter), + expected.toFmt(this.globalThis, &formatter), + received.toFmt(this.globalThis, &formatter), }); return; }, diff --git a/src/bun.js/test/expect.zig b/src/bun.js/test/expect.zig index bff3f91ebe71e9..46ca6ee00f714e 100644 --- a/src/bun.js/test/expect.zig +++ b/src/bun.js/test/expect.zig @@ -62,6 +62,7 @@ pub const Expect = struct { pub usingnamespace JSC.Codegen.JSExpect; flags: Flags = .{}, parent: ParentScope = .{ .global = {} }, + custom_label: bun.String = bun.String.empty, pub const TestScope = struct { test_id: TestRunner.Test.ID, @@ -116,7 +117,7 @@ pub const Expect = struct { return received ++ matcher_name ++ "(" ++ args ++ ")"; } - pub fn throwPrettyMatcherError(globalThis: *JSGlobalObject, matcher_name: anytype, matcher_params: anytype, flags: Flags, comptime message_fmt: string, message_args: anytype) void { + pub fn throwPrettyMatcherError(globalThis: *JSGlobalObject, custom_label: bun.String, matcher_name: anytype, matcher_params: anytype, flags: Flags, comptime message_fmt: string, message_args: anytype) void { switch (Output.enable_ansi_colors) { inline else => |colors| { const chain = switch (flags.promise) { @@ -124,12 +125,23 @@ pub const Expect = struct { .rejects => if (flags.not) Output.prettyFmt("rejects.not.", colors) else Output.prettyFmt("rejects.", colors), .none => if (flags.not) Output.prettyFmt("not.", colors) else "", }; - const fmt = comptime Output.prettyFmt("expect(received).{s}{s}({s})\n\n" ++ message_fmt, colors); - globalThis.throwPretty(fmt, .{ - chain, - matcher_name, - matcher_params, - } ++ message_args); + switch (!custom_label.isEmpty()) { + inline else => |use_default_label| { + if (use_default_label) { + const fmt = comptime Output.prettyFmt("expect(received).{s}{s}({s})\n\n" ++ message_fmt, colors); + globalThis.throwPretty(fmt, .{ + chain, + matcher_name, + matcher_params, + } ++ message_args); + } else { + const fmt = comptime Output.prettyFmt("{}\n\n" ++ message_fmt, colors); + globalThis.throwPretty(fmt, .{ + custom_label, + } ++ message_args); + } + }, + } }, } } @@ -173,13 +185,13 @@ pub const Expect = struct { const matcher_params = switch (Output.enable_ansi_colors) { inline else => |colors| comptime Output.prettyFmt(matcher_params_fmt, colors), }; - return processPromise(this.flags, globalThis, value, matcher_name, matcher_params, false); + return processPromise(this.custom_label, this.flags, globalThis, value, matcher_name, matcher_params, false); } /// Processes the async flags (resolves/rejects), waiting for the async value if needed. /// If no flags, returns the original value /// If either flag is set, waits for the result, and returns either it as a JSValue, or null if the expectation failed (in which case if silent is false, also throws a js exception) - pub fn processPromise(flags: Expect.Flags, globalThis: *JSGlobalObject, value: JSValue, matcher_name: anytype, matcher_params: anytype, comptime silent: bool) ?JSValue { + pub fn processPromise(custom_label: bun.String, flags: Expect.Flags, globalThis: *JSGlobalObject, value: JSValue, matcher_name: anytype, matcher_params: anytype, comptime silent: bool) ?JSValue { switch (flags.promise) { inline .resolves, .rejects => |resolution| { if (value.asAnyPromise()) |promise| { @@ -196,7 +208,7 @@ pub const Expect = struct { if (!silent) { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const message = "Expected promise that rejects\nReceived promise that resolved: {any}\n"; - throwPrettyMatcherError(globalThis, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); + throwPrettyMatcherError(globalThis, custom_label, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); } return null; }, @@ -208,7 +220,7 @@ pub const Expect = struct { if (!silent) { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const message = "Expected promise that resolves\nReceived promise that rejected: {any}\n"; - throwPrettyMatcherError(globalThis, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); + throwPrettyMatcherError(globalThis, custom_label, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); } return null; }, @@ -223,7 +235,7 @@ pub const Expect = struct { if (!silent) { var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const message = "Expected promise\nReceived: {any}\n"; - throwPrettyMatcherError(globalThis, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); + throwPrettyMatcherError(globalThis, custom_label, matcher_name, matcher_params, flags, message, .{value.toFmt(globalThis, &formatter)}); } return null; } @@ -261,7 +273,7 @@ pub const Expect = struct { outFlags.* = flags.encode(); // (note that matcher_name/matcher_args are not used because silent=true) - if (processPromise(flags, globalThis, value.*, "", "", true)) |result| { + if (processPromise(bun.String.empty, flags, globalThis, value.*, "", "", true)) |result| { value.* = result; return true; } @@ -316,19 +328,31 @@ pub const Expect = struct { pub fn finalize( this: *Expect, ) callconv(.C) void { + this.custom_label.deref(); VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { - const arguments = callframe.arguments(1).slice(); + pub fn call(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + const arguments = callframe.arguments(2).slice(); const value = if (arguments.len < 1) JSC.JSValue.jsUndefined() else arguments[0]; - var expect = globalObject.bunVM().allocator.create(Expect) catch { - globalObject.throwOutOfMemory(); + var custom_label = bun.String.empty; + if (arguments.len > 1) { + if (arguments[1].isString() or arguments[1].implementsToString(globalThis)) { + const label = arguments[1].toBunString(globalThis); + if (globalThis.hasException()) return .zero; + custom_label = label; + } + } + + var expect = globalThis.bunVM().allocator.create(Expect) catch { + custom_label.deref(); + globalThis.throwOutOfMemory(); return .zero; }; expect.* = .{ + .custom_label = custom_label, .parent = if (Jest.runner) |runner| if (runner.pending_test) |pending| Expect.ParentScope{ .TestScope = Expect.TestScope{ @@ -340,29 +364,38 @@ pub const Expect = struct { else Expect.ParentScope{ .global = {} }, }; - const expect_js_value = expect.toJS(globalObject); + const expect_js_value = expect.toJS(globalThis); expect_js_value.ensureStillAlive(); - Expect.capturedValueSetCached(expect_js_value, globalObject, value); + Expect.capturedValueSetCached(expect_js_value, globalThis, value); expect_js_value.ensureStillAlive(); - expect.postMatch(globalObject); + + expect.postMatch(globalThis); return expect_js_value; } + pub fn throw(this: *Expect, globalThis: *JSC.JSGlobalObject, comptime signature: string, comptime fmt: string, args: anytype) void { + if (this.custom_label.isEmpty()) { + globalThis.throwPretty(comptime signature ++ fmt, args); + } else { + globalThis.throwPretty(comptime "{}" ++ fmt, .{this.custom_label} ++ args); + } + } + pub fn constructor( - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame, ) callconv(.C) ?*Expect { - globalObject.throw("expect() cannot be called with new", .{}); + globalThis.throw("expect() cannot be called with new", .{}); return null; } // pass here has a leading underscore to avoid name collision with the pass variable in other functions pub fn _pass( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; @@ -374,11 +407,11 @@ pub const Expect = struct { value.ensureStillAlive(); if (!value.isString()) { - globalObject.throwInvalidArgumentType("pass", "message", "string"); + globalThis.throwInvalidArgumentType("pass", "message", "string"); return .zero; } - value.toZigString(&_msg, globalObject); + value.toZigString(&_msg, globalThis); } else { _msg = ZigString.fromBytes("passes by .pass() assertion"); } @@ -396,8 +429,7 @@ pub const Expect = struct { if (not) { const signature = comptime getSignature("pass", "", true); - const fmt = signature ++ "\n\n{s}\n"; - globalObject.throwPretty(fmt, .{msg.slice()}); + this.throw(globalThis, signature, "\n\n{s}\n", .{msg.slice()}); return .zero; } @@ -407,10 +439,10 @@ pub const Expect = struct { pub fn fail( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; @@ -422,11 +454,11 @@ pub const Expect = struct { value.ensureStillAlive(); if (!value.isString()) { - globalObject.throwInvalidArgumentType("fail", "message", "string"); + globalThis.throwInvalidArgumentType("fail", "message", "string"); return .zero; } - value.toZigString(&_msg, globalObject); + value.toZigString(&_msg, globalThis); } else { _msg = ZigString.fromBytes("fails by .fail() assertion"); } @@ -443,127 +475,128 @@ pub const Expect = struct { defer msg.deinit(); const signature = comptime getSignature("fail", "", true); - const fmt = signature ++ "\n\n{s}\n"; - globalObject.throwPretty(fmt, .{msg.slice()}); + this.throw(globalThis, signature, "\n\n{s}\n", .{msg.slice()}); return .zero; } /// Object.is() pub fn toBe( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callframe.this(); - const arguments_ = callframe.arguments(1); + const arguments_ = callframe.arguments(2); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBe() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toBe() takes 1 argument", .{}); return .zero; } incrementExpectCallCounter(); const right = arguments[0]; right.ensureStillAlive(); - const left = this.getValue(globalObject, thisValue, "toBe", "expected") orelse return .zero; + const left = this.getValue(globalThis, thisValue, "toBe", "expected") orelse return .zero; const not = this.flags.not; - var pass = right.isSameValue(left, globalObject); + var pass = right.isSameValue(left, globalThis); if (not) pass = !pass; if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - if (not) { - const signature = comptime getSignature("toBe", "expected", true); - const fmt = signature ++ "\n\nExpected: not {any}\n"; - globalObject.throwPretty(fmt, .{right.toFmt(globalObject, &formatter)}); - return .zero; - } + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; - const signature = comptime getSignature("toBe", "expected", false); - if (left.deepEquals(right, globalObject) or left.strictDeepEquals(right, globalObject)) { - const fmt = signature ++ - "\n\nIf this test should pass, replace \"toBe\" with \"toEqual\" or \"toStrictEqual\"" ++ - "\n\nExpected: {any}\n" ++ - "Received: serializes to the same string\n"; - globalObject.throwPretty(fmt, .{right.toFmt(globalObject, &formatter)}); - return .zero; - } + switch (this.custom_label.isEmpty()) { + inline else => |has_custom_label| { + if (not) { + const signature = comptime getSignature("toBe", "expected", true); + this.throw(globalThis, signature, "\n\nExpected: not {any}\n", .{right.toFmt(globalThis, &formatter)}); + return .zero; + } - if (right.isString() and left.isString()) { - const diff_format = DiffFormatter{ - .expected = right, - .received = left, - .globalObject = globalObject, - .not = not, - }; - const fmt = comptime signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_format}); - return .zero; - } + const signature = comptime getSignature("toBe", "expected", false); + if (left.deepEquals(right, globalThis) or left.strictDeepEquals(right, globalThis)) { + const fmt = signature ++ + (if (!has_custom_label) "\n\nIf this test should pass, replace \"toBe\" with \"toEqual\" or \"toStrictEqual\"" else "") ++ + "\n\nExpected: {any}\n" ++ + "Received: serializes to the same string\n"; + this.throw(globalThis, signature, fmt, .{right.toFmt(globalThis, &formatter)}); + return .zero; + } - const fmt = signature ++ "\n\nExpected: {any}\nReceived: {any}\n"; - globalObject.throwPretty(fmt, .{ - right.toFmt(globalObject, &formatter), - left.toFmt(globalObject, &formatter), - }); - return .zero; + if (right.isString() and left.isString()) { + const diff_format = DiffFormatter{ + .expected = right, + .received = left, + .globalThis = globalThis, + .not = not, + }; + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_format}); + return .zero; + } + + this.throw(globalThis, signature, "\n\nExpected: {any}\nReceived: {any}\n", .{ + right.toFmt(globalThis, &formatter), + left.toFmt(globalThis, &formatter), + }); + return .zero; + }, + } } pub fn toHaveLength( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callframe.this(); const arguments_ = callframe.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toHaveLength() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toHaveLength() takes 1 argument", .{}); return .zero; } incrementExpectCallCounter(); const expected: JSValue = arguments[0]; - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveLength", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveLength", "expected") orelse return .zero; if (!value.isObject() and !value.isString()) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Received value does not have a length property: {any}", .{value.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Received value does not have a length property: {any}", .{value.toFmt(globalThis, &fmt)}); return .zero; } if (!expected.isNumber()) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalThis, &fmt)}); return .zero; } const expected_length: f64 = expected.asNumber(); if (@round(expected_length) != expected_length or std.math.isInf(expected_length) or std.math.isNan(expected_length) or expected_length < 0) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalThis, &fmt)}); return .zero; } const not = this.flags.not; var pass = false; - const actual_length = value.getLengthIfPropertyExistsInternal(globalObject); + const actual_length = value.getLengthIfPropertyExistsInternal(globalThis); if (actual_length == std.math.inf(f64)) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Received value does not have a length property: {any}", .{value.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Received value does not have a length property: {any}", .{value.toFmt(globalThis, &fmt)}); return .zero; } else if (std.math.isNan(actual_length)) { - globalObject.throw("Received value has non-number length property: {}", .{actual_length}); + globalThis.throw("Received value has non-number length property: {}", .{actual_length}); return .zero; } @@ -577,65 +610,63 @@ pub const Expect = struct { // handle failure if (not) { const expected_line = "Expected length: not {d}\n"; - const fmt = comptime getSignature("toHaveLength", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{expected_length}); + const signature = comptime getSignature("toHaveLength", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{expected_length}); return .zero; } const expected_line = "Expected length: {d}\n"; const received_line = "Received length: {d}\n"; - const fmt = comptime getSignature("toHaveLength", "expected", false) ++ "\n\n" ++ - expected_line ++ received_line; - - globalObject.throwPretty(fmt, .{ expected_length, actual_length }); + const signature = comptime getSignature("toHaveLength", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_length, actual_length }); return .zero; } pub fn toBeOneOf( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeOneOf() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toBeOneOf() takes 1 argument", .{}); return .zero; } incrementExpectCallCounter(); - const expected = this.getValue(globalObject, thisValue, "toBeOneOf", "expected") orelse return .zero; + const expected = this.getValue(globalThis, thisValue, "toBeOneOf", "expected") orelse return .zero; const list_value: JSValue = arguments[0]; const not = this.flags.not; var pass = false; const ExpectedEntry = struct { - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, expected: JSValue, pass: *bool, }; if (list_value.jsTypeLoose().isArrayLike()) { - var itr = list_value.arrayIterator(globalObject); + var itr = list_value.arrayIterator(globalThis); while (itr.next()) |item| { // Confusingly, jest-extended uses `deepEqual`, instead of `toBe` - if (item.jestDeepEquals(expected, globalObject)) { + if (item.jestDeepEquals(expected, globalThis)) { pass = true; break; } } - } else if (list_value.isIterable(globalObject)) { + } else if (list_value.isIterable(globalThis)) { var expected_entry = ExpectedEntry{ - .globalObject = globalObject, + .globalThis = globalThis, .expected = expected, .pass = &pass, }; - list_value.forEach(globalObject, &expected_entry, struct { + list_value.forEach(globalThis, &expected_entry, struct { pub fn sameValueIterator( _: *JSC.VM, _: *JSGlobalObject, @@ -644,14 +675,14 @@ pub const Expect = struct { ) callconv(.C) void { const entry = bun.cast(*ExpectedEntry, entry_.?); // Confusingly, jest-extended uses `deepEqual`, instead of `toBe` - if (item.jestDeepEquals(entry.expected, entry.globalObject)) { + if (item.jestDeepEquals(entry.expected, entry.globalThis)) { entry.pass.* = true; // TODO(perf): break out of the `forEach` when a match is found } } }.sameValueIterator); } else { - globalObject.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); + globalThis.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); return .zero; } @@ -659,36 +690,36 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = list_value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = list_value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { - const received_fmt = list_value.toFmt(globalObject, &formatter); + const received_fmt = list_value.toFmt(globalThis, &formatter); const expected_line = "Expected to not be one of: {any}\nReceived: {any}\n"; - const fmt = comptime getSignature("toBeOneOf", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{ received_fmt, expected_fmt }); + const signature = comptime getSignature("toBeOneOf", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{ received_fmt, expected_fmt }); return .zero; } const expected_line = "Expected to be one of: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeOneOf", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ value_fmt, expected_fmt }); + const signature = comptime getSignature("toBeOneOf", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ value_fmt, expected_fmt }); return .zero; } pub fn toContain( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toContain() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toContain() takes 1 argument", .{}); return .zero; } @@ -696,29 +727,29 @@ pub const Expect = struct { const expected = arguments[0]; expected.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toContain", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toContain", "expected") orelse return .zero; const not = this.flags.not; var pass = false; const ExpectedEntry = struct { - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, expected: JSValue, pass: *bool, }; if (value.jsTypeLoose().isArrayLike()) { - var itr = value.arrayIterator(globalObject); + var itr = value.arrayIterator(globalThis); while (itr.next()) |item| { - if (item.isSameValue(expected, globalObject)) { + if (item.isSameValue(expected, globalThis)) { pass = true; break; } } } else if (value.isStringLiteral() and expected.isStringLiteral()) { - const value_string = value.toSlice(globalObject, default_allocator); + const value_string = value.toSlice(globalThis, default_allocator); defer value_string.deinit(); - const expected_string = expected.toSlice(globalObject, default_allocator); + const expected_string = expected.toSlice(globalThis, default_allocator); defer expected_string.deinit(); if (expected_string.len == 0) { // edge case empty string is always contained @@ -728,13 +759,13 @@ pub const Expect = struct { } else if (value_string.len == 0 and expected_string.len == 0) { // edge case two empty strings are true pass = true; } - } else if (value.isIterable(globalObject)) { + } else if (value.isIterable(globalThis)) { var expected_entry = ExpectedEntry{ - .globalObject = globalObject, + .globalThis = globalThis, .expected = expected, .pass = &pass, }; - value.forEach(globalObject, &expected_entry, struct { + value.forEach(globalThis, &expected_entry, struct { pub fn sameValueIterator( _: *JSC.VM, _: *JSGlobalObject, @@ -742,14 +773,14 @@ pub const Expect = struct { item: JSValue, ) callconv(.C) void { const entry = bun.cast(*ExpectedEntry, entry_.?); - if (item.isSameValue(entry.expected, entry.globalObject)) { + if (item.isSameValue(entry.expected, entry.globalThis)) { entry.pass.* = true; // TODO(perf): break out of the `forEach` when a match is found } } }.sameValueIterator); } else { - globalObject.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); + globalThis.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); return .zero; } @@ -757,36 +788,36 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { - const received_fmt = value.toFmt(globalObject, &formatter); + const received_fmt = value.toFmt(globalThis, &formatter); const expected_line = "Expected to not contain: {any}\nReceived: {any}\n"; - const fmt = comptime getSignature("toContain", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const signature = comptime getSignature("toContain", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{ expected_fmt, received_fmt }); return .zero; } const expected_line = "Expected to contain: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toContain", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toContain", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } pub fn toContainKey( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toContainKey() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toContainKey() takes 1 argument", .{}); return .zero; } @@ -794,45 +825,45 @@ pub const Expect = struct { const expected = arguments[0]; expected.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toContainKey", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toContainKey", "expected") orelse return .zero; const not = this.flags.not; - var pass = value.hasOwnPropertyValue(globalObject, expected); + var pass = value.hasOwnPropertyValue(globalThis, expected); if (not) pass = !pass; if (pass) return thisValue; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { - const received_fmt = value.toFmt(globalObject, &formatter); + const received_fmt = value.toFmt(globalThis, &formatter); const expected_line = "Expected to not contain: {any}\nReceived: {any}\n"; - const fmt = comptime getSignature("toContainKey", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const signature = comptime getSignature("toContainKey", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{ expected_fmt, received_fmt }); return .zero; } const expected_line = "Expected to contain: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toContainKey", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toContainKey", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } pub fn toContainKeys( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toContainKeys() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toContainKeys() takes 1 argument", .{}); return .zero; } @@ -840,27 +871,27 @@ pub const Expect = struct { const expected = arguments[0]; expected.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toContainKeys", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toContainKeys", "expected") orelse return .zero; if (!expected.jsType().isArray()) { - globalObject.throwInvalidArgumentType("toContainKeys", "expected", "array"); + globalThis.throwInvalidArgumentType("toContainKeys", "expected", "array"); return .zero; } const not = this.flags.not; var pass = brk: { - const count = expected.getLength(globalObject); + const count = expected.getLength(globalThis); // jest-extended checks for truthiness before calling hasOwnProperty // https://github.com/jest-community/jest-extended/blob/711fdcc54d68c2b2c1992c7cfbdf0d0bd6be0f4d/src/matchers/toContainKeys.js#L1-L6 - if (!value.coerce(bool, globalObject)) break :brk count == 0; + if (!value.coerce(bool, globalThis)) break :brk count == 0; var i: u32 = 0; while (i < count) : (i += 1) { - const key = expected.getIndex(globalObject, i); + const key = expected.getIndex(globalThis, i); - if (!value.hasOwnPropertyValue(globalObject, key)) { + if (!value.hasOwnPropertyValue(globalThis, key)) { break :brk false; } } @@ -872,36 +903,36 @@ pub const Expect = struct { if (pass) return thisValue; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { - const received_fmt = value.toFmt(globalObject, &formatter); + const received_fmt = value.toFmt(globalThis, &formatter); const expected_line = "Expected to not contain: {any}\nReceived: {any}\n"; - const fmt = comptime getSignature("toContainKeys", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const signature = comptime getSignature("toContainKeys", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{ expected_fmt, received_fmt }); return .zero; } const expected_line = "Expected to contain: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toContainKeys", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toContainKeys", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } pub fn toContainAnyKeys( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toContainAnyKeys() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toContainAnyKeys() takes 1 argument", .{}); return .zero; } @@ -909,24 +940,24 @@ pub const Expect = struct { const expected = arguments[0]; expected.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toContainAnyKeys", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toContainAnyKeys", "expected") orelse return .zero; if (!expected.jsType().isArray()) { - globalObject.throwInvalidArgumentType("toContainAnyKeys", "expected", "array"); + globalThis.throwInvalidArgumentType("toContainAnyKeys", "expected", "array"); return .zero; } const not = this.flags.not; var pass = false; - const count = expected.getLength(globalObject); + const count = expected.getLength(globalThis); var i: u32 = 0; while (i < count) : (i += 1) { - const key = expected.getIndex(globalObject, i); + const key = expected.getIndex(globalThis, i); - if (value.hasOwnPropertyValue(globalObject, key)) { + if (value.hasOwnPropertyValue(globalThis, key)) { pass = true; break; } @@ -936,36 +967,36 @@ pub const Expect = struct { if (pass) return thisValue; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { - const received_fmt = value.toFmt(globalObject, &formatter); + const received_fmt = value.toFmt(globalThis, &formatter); const expected_line = "Expected to not contain: {any}\nReceived: {any}\n"; - const fmt = comptime getSignature("toContainAnyKeys", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const signature = comptime getSignature("toContainAnyKeys", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{ expected_fmt, received_fmt }); return .zero; } const expected_line = "Expected to contain: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toContainAnyKeys", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toContainAnyKeys", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } pub fn toContainEqual( this: *Expect, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toContainEqual() takes 1 argument", .{}); + globalThis.throwInvalidArguments("toContainEqual() takes 1 argument", .{}); return .zero; } @@ -973,13 +1004,13 @@ pub const Expect = struct { const expected = arguments[0]; expected.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toContainEqual", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toContainEqual", "expected") orelse return .zero; const not = this.flags.not; var pass = false; const ExpectedEntry = struct { - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, expected: JSValue, pass: *bool, }; @@ -988,18 +1019,18 @@ pub const Expect = struct { const expected_type = expected.jsType(); if (value_type.isArrayLike()) { - var itr = value.arrayIterator(globalObject); + var itr = value.arrayIterator(globalThis); while (itr.next()) |item| { - if (item.jestDeepEquals(expected, globalObject)) { + if (item.jestDeepEquals(expected, globalThis)) { pass = true; break; } } } else if (value_type.isStringLike() and expected_type.isStringLike()) { if (expected_type.isStringObjectLike() and value_type.isString()) pass = false else { - const value_string = value.toSliceOrNull(globalObject) orelse return .zero; + const value_string = value.toSliceOrNull(globalThis) orelse return .zero; defer value_string.deinit(); - const expected_string = expected.toSliceOrNull(globalObject) orelse return .zero; + const expected_string = expected.toSliceOrNull(globalThis) orelse return .zero; defer expected_string.deinit(); // jest does not have a `typeof === "string"` check for `toContainEqual`. @@ -1014,13 +1045,13 @@ pub const Expect = struct { else strings.indexOf(value_string.slice(), expected_string.slice()) != null; } - } else if (value.isIterable(globalObject)) { + } else if (value.isIterable(globalThis)) { var expected_entry = ExpectedEntry{ - .globalObject = globalObject, + .globalThis = globalThis, .expected = expected, .pass = &pass, }; - value.forEach(globalObject, &expected_entry, struct { + value.forEach(globalThis, &expected_entry, struct { pub fn deepEqualsIterator( _: *JSC.VM, _: *JSGlobalObject, @@ -1028,14 +1059,14 @@ pub const Expect = struct { item: JSValue, ) callconv(.C) void { const entry = bun.cast(*ExpectedEntry, entry_.?); - if (item.jestDeepEquals(entry.expected, entry.globalObject)) { + if (item.jestDeepEquals(entry.expected, entry.globalThis)) { entry.pass.* = true; // TODO(perf): break out of the `forEach` when a match is found } } }.deepEqualsIterator); } else { - globalObject.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); + globalThis.throw("Received value must be an array type, or both received and expected values must be strings.", .{}); return .zero; } @@ -1043,59 +1074,59 @@ pub const Expect = struct { if (pass) return thisValue; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = expected.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = expected.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected to not contain: {any}\n"; - const fmt = comptime getSignature("toContainEqual", "expected", true) ++ "\n\n" ++ expected_line; - globalObject.throwPretty(fmt, .{expected_fmt}); + const signature = comptime getSignature("toContainEqual", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line, .{expected_fmt}); return .zero; } const expected_line = "Expected to contain: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toContainEqual", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toContainEqual", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeTruthy(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeTruthy(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeTruthy", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeTruthy", "") orelse return .zero; incrementExpectCallCounter(); const not = this.flags.not; var pass = false; - const truthy = value.toBooleanSlow(globalObject); + const truthy = value.toBooleanSlow(globalThis); if (truthy) pass = true; if (not) pass = !pass; if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeTruthy", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeTruthy", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeTruthy", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeTruthy", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeUndefined(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeUndefined(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeUndefined", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeUndefined", "") orelse return .zero; incrementExpectCallCounter(); @@ -1107,26 +1138,26 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeUndefined", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeUndefined", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeUndefined", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeUndefined", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeNaN(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeNaN(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeNaN", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeNaN", "") orelse return .zero; incrementExpectCallCounter(); @@ -1141,26 +1172,26 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeNaN", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeNaN", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeNaN", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeNaN", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeNull(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeNull(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeNull", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeNull", "") orelse return .zero; incrementExpectCallCounter(); @@ -1170,26 +1201,26 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeNull", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeNull", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeNull", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeNull", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeDefined(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeDefined(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeDefined", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeDefined", "") orelse return .zero; incrementExpectCallCounter(); @@ -1199,74 +1230,74 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeDefined", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeDefined", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeDefined", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeDefined", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeFalsy(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeFalsy(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeFalsy", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeFalsy", "") orelse return .zero; incrementExpectCallCounter(); const not = this.flags.not; var pass = false; - const truthy = value.toBooleanSlow(globalObject); + const truthy = value.toBooleanSlow(globalThis); if (!truthy) pass = true; if (not) pass = !pass; if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeFalsy", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeFalsy", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeFalsy", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeFalsy", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toEqual(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toEqual(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toEqual() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toEqual() requires 1 argument", .{}); return .zero; } incrementExpectCallCounter(); const expected = arguments[0]; - const value: JSValue = this.getValue(globalObject, thisValue, "toEqual", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toEqual", "expected") orelse return .zero; const not = this.flags.not; - var pass = value.jestDeepEquals(expected, globalObject); + var pass = value.jestDeepEquals(expected, globalThis); if (not) pass = !pass; if (pass) return .undefined; @@ -1275,71 +1306,67 @@ pub const Expect = struct { const diff_formatter = DiffFormatter{ .received = value, .expected = expected, - .globalObject = globalObject, + .globalThis = globalThis, .not = not, }; if (not) { const signature = comptime getSignature("toEqual", "expected", true); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } const signature = comptime getSignature("toEqual", "expected", false); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } - pub fn toStrictEqual(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toStrictEqual(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toStrictEqual() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toStrictEqual() requires 1 argument", .{}); return .zero; } incrementExpectCallCounter(); const expected = arguments[0]; - const value: JSValue = this.getValue(globalObject, thisValue, "toStrictEqual", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toStrictEqual", "expected") orelse return .zero; const not = this.flags.not; - var pass = value.jestStrictDeepEquals(expected, globalObject); + var pass = value.jestStrictDeepEquals(expected, globalThis); if (not) pass = !pass; if (pass) return .undefined; // handle failure - const diff_formatter = DiffFormatter{ .received = value, .expected = expected, .globalObject = globalObject, .not = not }; + const diff_formatter = DiffFormatter{ .received = value, .expected = expected, .globalThis = globalThis, .not = not }; if (not) { const signature = comptime getSignature("toStrictEqual", "expected", true); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } const signature = comptime getSignature("toStrictEqual", "expected", false); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } - pub fn toHaveProperty(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toHaveProperty(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(2); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toHaveProperty() requires at least 1 argument", .{}); + globalThis.throwInvalidArguments("toHaveProperty() requires at least 1 argument", .{}); return .zero; } @@ -1350,52 +1377,50 @@ pub const Expect = struct { const expected_property: ?JSValue = if (arguments.len > 1) arguments[1] else null; if (expected_property) |ev| ev.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveProperty", "path, value") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveProperty", "path, value") orelse return .zero; - if (!expected_property_path.isString() and !expected_property_path.isIterable(globalObject)) { - globalObject.throw("Expected path must be a string or an array", .{}); + if (!expected_property_path.isString() and !expected_property_path.isIterable(globalThis)) { + globalThis.throw("Expected path must be a string or an array", .{}); return .zero; } const not = this.flags.not; var path_string = ZigString.Empty; - expected_property_path.toZigString(&path_string, globalObject); + expected_property_path.toZigString(&path_string, globalThis); var pass = !value.isUndefinedOrNull(); var received_property: JSValue = .zero; if (pass) { - received_property = value.getIfPropertyExistsFromPath(globalObject, expected_property_path); + received_property = value.getIfPropertyExistsFromPath(globalThis, expected_property_path); pass = !received_property.isEmpty(); } if (pass and expected_property != null) { - pass = received_property.jestDeepEquals(expected_property.?, globalObject); + pass = received_property.jestDeepEquals(expected_property.?, globalThis); } if (not) pass = !pass; if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; if (not) { if (expected_property != null) { const signature = comptime getSignature("toHaveProperty", "path, value", true); if (!received_property.isEmpty()) { - const fmt = signature ++ "\n\nExpected path: {any}\n\nExpected value: not {any}\n"; - globalObject.throwPretty(fmt, .{ - expected_property_path.toFmt(globalObject, &formatter), - expected_property.?.toFmt(globalObject, &formatter), + this.throw(globalThis, signature, "\n\nExpected path: {any}\n\nExpected value: not {any}\n", .{ + expected_property_path.toFmt(globalThis, &formatter), + expected_property.?.toFmt(globalThis, &formatter), }); return .zero; } } const signature = comptime getSignature("toHaveProperty", "path", true); - const fmt = signature ++ "\n\nExpected path: not {any}\n\nReceived value: {any}\n"; - globalObject.throwPretty(fmt, .{ - expected_property_path.toFmt(globalObject, &formatter), - received_property.toFmt(globalObject, &formatter), + this.throw(globalThis, signature, "\n\nExpected path: not {any}\n\nReceived value: {any}\n", .{ + expected_property_path.toFmt(globalThis, &formatter), + received_property.toFmt(globalThis, &formatter), }); return .zero; } @@ -1404,38 +1429,36 @@ pub const Expect = struct { const signature = comptime getSignature("toHaveProperty", "path, value", false); if (!received_property.isEmpty()) { // deep equal case - const fmt = signature ++ "\n\n{any}\n"; const diff_format = DiffFormatter{ .received = received_property, .expected = expected_property.?, - .globalObject = globalObject, + .globalThis = globalThis, }; - globalObject.throwPretty(fmt, .{diff_format}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_format}); return .zero; } - const fmt = signature ++ "\n\nExpected path: {any}\n\nExpected value: {any}\n\n" ++ + const fmt = "\n\nExpected path: {any}\n\nExpected value: {any}\n\n" ++ "Unable to find property\n"; - globalObject.throwPretty(fmt, .{ - expected_property_path.toFmt(globalObject, &formatter), - expected_property.?.toFmt(globalObject, &formatter), + this.throw(globalThis, signature, fmt, .{ + expected_property_path.toFmt(globalThis, &formatter), + expected_property.?.toFmt(globalThis, &formatter), }); return .zero; } const signature = comptime getSignature("toHaveProperty", "path", false); - const fmt = signature ++ "\n\nExpected path: {any}\n\nUnable to find property\n"; - globalObject.throwPretty(fmt, .{expected_property_path.toFmt(globalObject, &formatter)}); + this.throw(globalThis, signature, "\n\nExpected path: {any}\n\nUnable to find property\n", .{expected_property_path.toFmt(globalThis, &formatter)}); return .zero; } - pub fn toBeEven(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeEven(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeEven", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeEven", "") orelse return .zero; incrementExpectCallCounter(); @@ -1469,30 +1492,30 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeEven", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeEven", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeEven", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeEven", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toBeGreaterThan(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeGreaterThan(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeGreaterThan() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toBeGreaterThan() requires 1 argument", .{}); return .zero; } @@ -1501,10 +1524,10 @@ pub const Expect = struct { const other_value = arguments[0]; other_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeGreaterThan", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeGreaterThan", "expected") orelse return .zero; if ((!value.isNumber() and !value.isBigInt()) or (!other_value.isNumber() and !other_value.isBigInt())) { - globalObject.throw("Expected and actual values must be numbers or bigints", .{}); + globalThis.throw("Expected and actual values must be numbers or bigints", .{}); return .zero; } @@ -1514,12 +1537,12 @@ pub const Expect = struct { if (!value.isBigInt() and !other_value.isBigInt()) { pass = value.asNumber() > other_value.asNumber(); } else if (value.isBigInt()) { - pass = switch (value.asBigIntCompare(globalObject, other_value)) { + pass = switch (value.asBigIntCompare(globalThis, other_value)) { .greater_than => true, else => pass, }; } else { - pass = switch (other_value.asBigIntCompare(globalObject, value)) { + pass = switch (other_value.asBigIntCompare(globalThis, value)) { .less_than => true, else => pass, }; @@ -1529,36 +1552,33 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = other_value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = other_value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected: not \\> {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeGreaterThan", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeGreaterThan", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected: \\> {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeGreaterThan", "expected", false) ++ "\n\n" ++ - expected_line ++ received_line; - - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeGreaterThan", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeGreaterThanOrEqual(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeGreaterThanOrEqual(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeGreaterThanOrEqual() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toBeGreaterThanOrEqual() requires 1 argument", .{}); return .zero; } @@ -1567,10 +1587,10 @@ pub const Expect = struct { const other_value = arguments[0]; other_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeGreaterThanOrEqual", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeGreaterThanOrEqual", "expected") orelse return .zero; if ((!value.isNumber() and !value.isBigInt()) or (!other_value.isNumber() and !other_value.isBigInt())) { - globalObject.throw("Expected and actual values must be numbers or bigints", .{}); + globalThis.throw("Expected and actual values must be numbers or bigints", .{}); return .zero; } @@ -1580,12 +1600,12 @@ pub const Expect = struct { if (!value.isBigInt() and !other_value.isBigInt()) { pass = value.asNumber() >= other_value.asNumber(); } else if (value.isBigInt()) { - pass = switch (value.asBigIntCompare(globalObject, other_value)) { + pass = switch (value.asBigIntCompare(globalThis, other_value)) { .greater_than, .equal => true, else => pass, }; } else { - pass = switch (other_value.asBigIntCompare(globalObject, value)) { + pass = switch (other_value.asBigIntCompare(globalThis, value)) { .less_than, .equal => true, else => pass, }; @@ -1595,33 +1615,33 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = other_value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = other_value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected: not \\>= {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeGreaterThanOrEqual", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeGreaterThanOrEqual", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected: \\>= {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeGreaterThanOrEqual", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeGreaterThanOrEqual", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeLessThan(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeLessThan(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeLessThan() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toBeLessThan() requires 1 argument", .{}); return .zero; } @@ -1630,10 +1650,10 @@ pub const Expect = struct { const other_value = arguments[0]; other_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeLessThan", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeLessThan", "expected") orelse return .zero; if ((!value.isNumber() and !value.isBigInt()) or (!other_value.isNumber() and !other_value.isBigInt())) { - globalObject.throw("Expected and actual values must be numbers or bigints", .{}); + globalThis.throw("Expected and actual values must be numbers or bigints", .{}); return .zero; } @@ -1643,12 +1663,12 @@ pub const Expect = struct { if (!value.isBigInt() and !other_value.isBigInt()) { pass = value.asNumber() < other_value.asNumber(); } else if (value.isBigInt()) { - pass = switch (value.asBigIntCompare(globalObject, other_value)) { + pass = switch (value.asBigIntCompare(globalThis, other_value)) { .less_than => true, else => pass, }; } else { - pass = switch (other_value.asBigIntCompare(globalObject, value)) { + pass = switch (other_value.asBigIntCompare(globalThis, value)) { .greater_than => true, else => pass, }; @@ -1658,33 +1678,33 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = other_value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = other_value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected: not \\< {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeLessThan", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeLessThan", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected: \\< {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeLessThan", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeLessThan", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeLessThanOrEqual(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeLessThanOrEqual(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeLessThanOrEqual() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toBeLessThanOrEqual() requires 1 argument", .{}); return .zero; } @@ -1693,10 +1713,10 @@ pub const Expect = struct { const other_value = arguments[0]; other_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeLessThanOrEqual", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeLessThanOrEqual", "expected") orelse return .zero; if ((!value.isNumber() and !value.isBigInt()) or (!other_value.isNumber() and !other_value.isBigInt())) { - globalObject.throw("Expected and actual values must be numbers or bigints", .{}); + globalThis.throw("Expected and actual values must be numbers or bigints", .{}); return .zero; } @@ -1706,12 +1726,12 @@ pub const Expect = struct { if (!value.isBigInt() and !other_value.isBigInt()) { pass = value.asNumber() <= other_value.asNumber(); } else if (value.isBigInt()) { - pass = switch (value.asBigIntCompare(globalObject, other_value)) { + pass = switch (value.asBigIntCompare(globalThis, other_value)) { .less_than, .equal => true, else => pass, }; } else { - pass = switch (other_value.asBigIntCompare(globalObject, value)) { + pass = switch (other_value.asBigIntCompare(globalThis, value)) { .greater_than, .equal => true, else => pass, }; @@ -1721,39 +1741,39 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); - const expected_fmt = other_value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); + const expected_fmt = other_value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected: not \\<= {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeLessThanOrEqual", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeLessThanOrEqual", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected: \\<= {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeLessThanOrEqual", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeLessThanOrEqual", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeCloseTo(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeCloseTo(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const thisArguments = callFrame.arguments(2); const arguments = thisArguments.ptr[0..thisArguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeCloseTo() requires at least 1 argument. Expected value must be a number", .{}); + globalThis.throwInvalidArguments("toBeCloseTo() requires at least 1 argument. Expected value must be a number", .{}); return .zero; } const expected_ = arguments[0]; if (!expected_.isNumber()) { - globalObject.throwInvalidArgumentType("toBeCloseTo", "expected", "number"); + globalThis.throwInvalidArgumentType("toBeCloseTo", "expected", "number"); return .zero; } @@ -1761,16 +1781,16 @@ pub const Expect = struct { if (arguments.len > 1) { const precision_ = arguments[1]; if (!precision_.isNumber()) { - globalObject.throwInvalidArgumentType("toBeCloseTo", "precision", "number"); + globalThis.throwInvalidArgumentType("toBeCloseTo", "precision", "number"); return .zero; } precision = precision_.asNumber(); } - const received_: JSValue = this.getValue(globalObject, thisValue, "toBeCloseTo", "expected, precision") orelse return .zero; + const received_: JSValue = this.getValue(globalThis, thisValue, "toBeCloseTo", "expected, precision") orelse return .zero; if (!received_.isNumber()) { - globalObject.throwInvalidArgumentType("expect", "received", "number"); + globalThis.throwInvalidArgumentType("expect", "received", "number"); return .zero; } @@ -1798,10 +1818,10 @@ pub const Expect = struct { if (pass) return .undefined; - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; - const expected_fmt = expected_.toFmt(globalObject, &formatter); - const received_fmt = received_.toFmt(globalObject, &formatter); + const expected_fmt = expected_.toFmt(globalThis, &formatter); + const received_fmt = received_.toFmt(globalThis, &formatter); const expected_line = "Expected: {any}\n"; const received_line = "Received: {any}\n"; @@ -1812,23 +1832,22 @@ pub const Expect = struct { const suffix_fmt = "\n\n" ++ expected_line ++ received_line ++ "\n" ++ expected_precision ++ expected_difference ++ received_difference; if (not) { - const fmt = comptime getSignature("toBeCloseTo", "expected, precision", true) ++ suffix_fmt; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt, precision, expected_diff, actual_diff }); + const signature = comptime getSignature("toBeCloseTo", "expected, precision", true); + this.throw(globalThis, signature, suffix_fmt, .{ expected_fmt, received_fmt, precision, expected_diff, actual_diff }); return .zero; } - const fmt = comptime getSignature("toBeCloseTo", "expected, precision", false) ++ suffix_fmt; - - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt, precision, expected_diff, actual_diff }); + const signature = comptime getSignature("toBeCloseTo", "expected, precision", false); + this.throw(globalThis, signature, suffix_fmt, .{ expected_fmt, received_fmt, precision, expected_diff, actual_diff }); return .zero; } - pub fn toBeOdd(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeOdd(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeOdd", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeOdd", "") orelse return .zero; incrementExpectCallCounter(); @@ -1860,23 +1879,23 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const value_fmt = value.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeOdd", "", true) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeOdd", "", true); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeOdd", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{value_fmt}); + const signature = comptime getSignature("toBeOdd", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{value_fmt}); return .zero; } - pub fn toThrow(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toThrow(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); @@ -1887,15 +1906,15 @@ pub const Expect = struct { const expected_value: JSValue = if (arguments.len > 0) brk: { const value = arguments[0]; if (value.isEmptyOrUndefinedOrNull() or !value.isObject() and !value.isString()) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Expected value must be string or Error: {any}", .{value.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Expected value must be string or Error: {any}", .{value.toFmt(globalThis, &fmt)}); return .zero; } break :brk value; } else .zero; expected_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toThrow", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toThrow", "expected") orelse return .zero; const not = this.flags.not; @@ -1905,11 +1924,11 @@ pub const Expect = struct { break :brk value; } - globalObject.throw("Expected value must be a function", .{}); + globalThis.throw("Expected value must be a function", .{}); return .zero; } - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); var return_value: JSValue = .zero; // Drain existing unhandled rejections @@ -1919,7 +1938,7 @@ pub const Expect = struct { const prev_unhandled_pending_rejection_to_capture = vm.unhandled_pending_rejection_to_capture; vm.unhandled_pending_rejection_to_capture = &return_value; vm.onUnhandledRejection = &VirtualMachine.onQuietUnhandledRejectionHandlerCaptureValue; - const return_value_from_fucntion: JSValue = value.call(globalObject, &.{}); + const return_value_from_fucntion: JSValue = value.call(globalThis, &.{}); vm.unhandled_pending_rejection_to_capture = prev_unhandled_pending_rejection_to_capture; vm.global.handleRejectedPromises(); @@ -1931,14 +1950,14 @@ pub const Expect = struct { if (return_value.asAnyPromise()) |promise| { vm.waitForPromise(promise); scope.apply(vm); - const promise_result = promise.result(globalObject.vm()); + const promise_result = promise.result(globalThis.vm()); - switch (promise.status(globalObject.vm())) { + switch (promise.status(globalThis.vm())) { .Fulfilled => { break :brk null; }, .Rejected => { - promise.setHandled(globalObject.vm()); + promise.setHandled(globalThis.vm()); // since we know for sure it rejected, we should always return the error break :brk promise_result.toError() orelse promise_result; @@ -1949,7 +1968,7 @@ pub const Expect = struct { if (return_value != return_value_from_fucntion) { if (return_value_from_fucntion.asAnyPromise()) |existing| { - existing.setHandled(globalObject.vm()); + existing.setHandled(globalThis.vm()); } } @@ -1966,86 +1985,81 @@ pub const Expect = struct { if (!did_throw) return .undefined; const result: JSValue = result_.?; - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; if (expected_value.isEmpty() or expected_value.isUndefined()) { const signature_no_args = comptime getSignature("toThrow", "", true); if (result.toError()) |err| { - const name = err.get(globalObject, "name") orelse JSValue.undefined; - const message = err.get(globalObject, "message") orelse JSValue.undefined; + const name = err.get(globalThis, "name") orelse JSValue.undefined; + const message = err.get(globalThis, "message") orelse JSValue.undefined; const fmt = signature_no_args ++ "\n\nError name: {any}\nError message: {any}\n"; - globalObject.throwPretty(fmt, .{ - name.toFmt(globalObject, &formatter), - message.toFmt(globalObject, &formatter), + globalThis.throwPretty(fmt, .{ + name.toFmt(globalThis, &formatter), + message.toFmt(globalThis, &formatter), }); return .zero; } // non error thrown const fmt = signature_no_args ++ "\n\nThrown value: {any}\n"; - globalObject.throwPretty(fmt, .{result.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{result.toFmt(globalThis, &formatter)}); return .zero; } if (expected_value.isString()) { - const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7); + const received_message = result.getIfPropertyExistsImpl(globalThis, "message", 7); // TODO: remove this allocation // partial match { - const expected_slice = expected_value.toSliceOrNull(globalObject) orelse return .zero; + const expected_slice = expected_value.toSliceOrNull(globalThis) orelse return .zero; defer expected_slice.deinit(); - const received_slice = received_message.toSliceOrNull(globalObject) orelse return .zero; + const received_slice = received_message.toSliceOrNull(globalThis) orelse return .zero; defer received_slice.deinit(); if (!strings.contains(received_slice.slice(), expected_slice.slice())) return .undefined; } - const fmt = signature ++ "\n\nExpected substring: not {any}\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ - expected_value.toFmt(globalObject, &formatter), - received_message.toFmt(globalObject, &formatter), + this.throw(globalThis, signature, "\n\nExpected substring: not {any}\nReceived message: {any}\n", .{ + expected_value.toFmt(globalThis, &formatter), + received_message.toFmt(globalThis, &formatter), }); return .zero; } if (expected_value.isRegExp()) { - const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7); + const received_message = result.getIfPropertyExistsImpl(globalThis, "message", 7); // TODO: REMOVE THIS GETTER! Expose a binding to call .test on the RegExp object directly. - if (expected_value.get(globalObject, "test")) |test_fn| { - const matches = test_fn.callWithThis(globalObject, expected_value, &.{received_message}); - if (!matches.toBooleanSlow(globalObject)) return .undefined; + if (expected_value.get(globalThis, "test")) |test_fn| { + const matches = test_fn.callWithThis(globalThis, expected_value, &.{received_message}); + if (!matches.toBooleanSlow(globalThis)) return .undefined; } - const fmt = signature ++ "\n\nExpected pattern: not {any}\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ - expected_value.toFmt(globalObject, &formatter), - received_message.toFmt(globalObject, &formatter), + this.throw(globalThis, signature, "\n\nExpected pattern: not {any}\nReceived message: {any}\n", .{ + expected_value.toFmt(globalThis, &formatter), + received_message.toFmt(globalThis, &formatter), }); return .zero; } - if (expected_value.get(globalObject, "message")) |expected_message| { - const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7); + if (expected_value.get(globalThis, "message")) |expected_message| { + const received_message = result.getIfPropertyExistsImpl(globalThis, "message", 7); // no partial match for this case - if (!expected_message.isSameValue(received_message, globalObject)) return .undefined; + if (!expected_message.isSameValue(received_message, globalThis)) return .undefined; - const fmt = signature ++ "\n\nExpected message: not {any}\n"; - globalObject.throwPretty(fmt, .{expected_message.toFmt(globalObject, &formatter)}); + this.throw(globalThis, signature, "\n\nExpected message: not {any}\n", .{expected_message.toFmt(globalThis, &formatter)}); return .zero; } - if (!result.isInstanceOf(globalObject, expected_value)) return .undefined; + if (!result.isInstanceOf(globalThis, expected_value)) return .undefined; var expected_class = ZigString.Empty; - expected_value.getClassName(globalObject, &expected_class); - const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7); - const fmt = signature ++ "\n\nExpected constructor: not {s}\n\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ expected_class, received_message.toFmt(globalObject, &formatter) }); + expected_value.getClassName(globalThis, &expected_class); + const received_message = result.getIfPropertyExistsImpl(globalThis, "message", 7); + this.throw(globalThis, signature, "\n\nExpected constructor: not {s}\n\nReceived message: {any}\n", .{ expected_class, received_message.toFmt(globalThis, &formatter) }); return .zero; } - const signature = comptime getSignature("toThrow", "expected", false); if (did_throw) { if (expected_value.isEmpty() or expected_value.isUndefined()) return .undefined; @@ -2055,8 +2069,8 @@ pub const Expect = struct { result_.?; const _received_message: ?JSValue = if (result.isObject()) - result.get(globalObject, "message") - else if (result.toStringOrNull(globalObject)) |js_str| + result.get(globalThis, "message") + else if (result.toStringOrNull(globalThis)) |js_str| JSC.JSValue.fromCell(js_str) else null; @@ -2065,28 +2079,28 @@ pub const Expect = struct { if (_received_message) |received_message| { // TODO: remove this allocation // partial match - const expected_slice = expected_value.toSliceOrNull(globalObject) orelse return .zero; + const expected_slice = expected_value.toSliceOrNull(globalThis) orelse return .zero; defer expected_slice.deinit(); - const received_slice = received_message.toSlice(globalObject, globalObject.allocator()); + const received_slice = received_message.toSlice(globalThis, globalThis.allocator()); defer received_slice.deinit(); if (strings.contains(received_slice.slice(), expected_slice.slice())) return .undefined; } // error: message from received error does not match expected string - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + + const signature = comptime getSignature("toThrow", "expected", false); if (_received_message) |received_message| { - const expected_value_fmt = expected_value.toFmt(globalObject, &formatter); - const received_message_fmt = received_message.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\n" ++ "Expected substring: {any}\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ expected_value_fmt, received_message_fmt }); + const expected_value_fmt = expected_value.toFmt(globalThis, &formatter); + const received_message_fmt = received_message.toFmt(globalThis, &formatter); + this.throw(globalThis, signature, "\n\n" ++ "Expected substring: {any}\nReceived message: {any}\n", .{ expected_value_fmt, received_message_fmt }); return .zero; } - const expected_fmt = expected_value.toFmt(globalObject, &formatter); - const received_fmt = result.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\n" ++ "Expected substring: {any}\nReceived value: {any}"; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const expected_fmt = expected_value.toFmt(globalThis, &formatter); + const received_fmt = result.toFmt(globalThis, &formatter); + this.throw(globalThis, signature, "\n\n" ++ "Expected substring: {any}\nReceived value: {any}", .{ expected_fmt, received_fmt }); return .zero; } @@ -2094,72 +2108,74 @@ pub const Expect = struct { if (expected_value.isRegExp()) { if (_received_message) |received_message| { // TODO: REMOVE THIS GETTER! Expose a binding to call .test on the RegExp object directly. - if (expected_value.get(globalObject, "test")) |test_fn| { - const matches = test_fn.callWithThis(globalObject, expected_value, &.{received_message}); - if (matches.toBooleanSlow(globalObject)) return .undefined; + if (expected_value.get(globalThis, "test")) |test_fn| { + const matches = test_fn.callWithThis(globalThis, expected_value, &.{received_message}); + if (matches.toBooleanSlow(globalThis)) return .undefined; } } // error: message from received error does not match expected pattern - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; if (_received_message) |received_message| { - const expected_value_fmt = expected_value.toFmt(globalObject, &formatter); - const received_message_fmt = received_message.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\n" ++ "Expected pattern: {any}\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ expected_value_fmt, received_message_fmt }); + const expected_value_fmt = expected_value.toFmt(globalThis, &formatter); + const received_message_fmt = received_message.toFmt(globalThis, &formatter); + const signature = comptime getSignature("toThrow", "expected", false); + + this.throw(globalThis, signature, "\n\n" ++ "Expected pattern: {any}\nReceived message: {any}\n", .{ expected_value_fmt, received_message_fmt }); return .zero; } - const expected_fmt = expected_value.toFmt(globalObject, &formatter); - const received_fmt = result.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\n" ++ "Expected pattern: {any}\nReceived value: {any}"; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const expected_fmt = expected_value.toFmt(globalThis, &formatter); + const received_fmt = result.toFmt(globalThis, &formatter); + const signature = comptime getSignature("toThrow", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ "Expected pattern: {any}\nReceived value: {any}", .{ expected_fmt, received_fmt }); return .zero; } // If it's not an object, we are going to crash here. assert(expected_value.isObject()); - if (expected_value.get(globalObject, "message")) |expected_message| { + if (expected_value.get(globalThis, "message")) |expected_message| { + const signature = comptime getSignature("toThrow", "expected", false); + if (_received_message) |received_message| { - if (received_message.isSameValue(expected_message, globalObject)) return .undefined; + if (received_message.isSameValue(expected_message, globalThis)) return .undefined; } // error: message from received error does not match expected error message. - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; if (_received_message) |received_message| { - const expected_fmt = expected_message.toFmt(globalObject, &formatter); - const received_fmt = received_message.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\nExpected message: {any}\nReceived message: {any}\n"; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const expected_fmt = expected_message.toFmt(globalThis, &formatter); + const received_fmt = received_message.toFmt(globalThis, &formatter); + this.throw(globalThis, signature, "\n\nExpected message: {any}\nReceived message: {any}\n", .{ expected_fmt, received_fmt }); return .zero; } - const expected_fmt = expected_message.toFmt(globalObject, &formatter); - const received_fmt = result.toFmt(globalObject, &formatter); - const fmt = signature ++ "\n\nExpected message: {any}\nReceived value: {any}\n"; - globalObject.throwPretty(fmt, .{ expected_fmt, received_fmt }); + const expected_fmt = expected_message.toFmt(globalThis, &formatter); + const received_fmt = result.toFmt(globalThis, &formatter); + this.throw(globalThis, signature, "\n\nExpected message: {any}\nReceived value: {any}\n", .{ expected_fmt, received_fmt }); return .zero; } - if (result.isInstanceOf(globalObject, expected_value)) return .undefined; + if (result.isInstanceOf(globalThis, expected_value)) return .undefined; // error: received error not instance of received error constructor - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; var expected_class = ZigString.Empty; var received_class = ZigString.Empty; - expected_value.getClassName(globalObject, &expected_class); - result.getClassName(globalObject, &received_class); + expected_value.getClassName(globalThis, &expected_class); + result.getClassName(globalThis, &received_class); + const signature = comptime getSignature("toThrow", "expected", false); const fmt = signature ++ "\n\nExpected constructor: {s}\nReceived constructor: {s}\n\n"; if (_received_message) |received_message| { const message_fmt = fmt ++ "Received message: {any}\n"; - const received_message_fmt = received_message.toFmt(globalObject, &formatter); + const received_message_fmt = received_message.toFmt(globalThis, &formatter); - globalObject.throwPretty(message_fmt, .{ + globalThis.throwPretty(message_fmt, .{ expected_class, received_class, received_message_fmt, @@ -2167,10 +2183,10 @@ pub const Expect = struct { return .zero; } - const received_fmt = result.toFmt(globalObject, &formatter); + const received_fmt = result.toFmt(globalThis, &formatter); const value_fmt = fmt ++ "Received value: {any}\n"; - globalObject.throwPretty(value_fmt, .{ + globalThis.throwPretty(value_fmt, .{ expected_class, received_class, received_fmt, @@ -2179,46 +2195,44 @@ pub const Expect = struct { } // did not throw - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const received_line = "Received function did not throw\n"; if (expected_value.isEmpty() or expected_value.isUndefined()) { - const fmt = comptime getSignature("toThrow", "", false) ++ "\n\n" ++ received_line; - globalObject.throwPretty(fmt, .{}); + const signature = comptime getSignature("toThrow", "", false); + this.throw(globalThis, signature, "\n\n" ++ received_line, .{}); return .zero; } + const signature = comptime getSignature("toThrow", "expected", false); + if (expected_value.isString()) { const expected_fmt = "\n\nExpected substring: {any}\n\n" ++ received_line; - const fmt = signature ++ expected_fmt; - globalObject.throwPretty(fmt, .{expected_value.toFmt(globalObject, &formatter)}); + this.throw(globalThis, signature, expected_fmt, .{expected_value.toFmt(globalThis, &formatter)}); return .zero; } if (expected_value.isRegExp()) { const expected_fmt = "\n\nExpected pattern: {any}\n\n" ++ received_line; - const fmt = signature ++ expected_fmt; - globalObject.throwPretty(fmt, .{expected_value.toFmt(globalObject, &formatter)}); + this.throw(globalThis, signature, expected_fmt, .{expected_value.toFmt(globalThis, &formatter)}); return .zero; } - if (expected_value.get(globalObject, "message")) |expected_message| { + if (expected_value.get(globalThis, "message")) |expected_message| { const expected_fmt = "\n\nExpected message: {any}\n\n" ++ received_line; - const fmt = signature ++ expected_fmt; - globalObject.throwPretty(fmt, .{expected_message.toFmt(globalObject, &formatter)}); + this.throw(globalThis, signature, expected_fmt, .{expected_message.toFmt(globalThis, &formatter)}); return .zero; } const expected_fmt = "\n\nExpected constructor: {s}\n\n" ++ received_line; var expected_class = ZigString.Empty; - expected_value.getClassName(globalObject, &expected_class); - const fmt = signature ++ expected_fmt; - globalObject.throwPretty(fmt, .{expected_class}); + expected_value.getClassName(globalThis, &expected_class); + this.throw(globalThis, signature, expected_fmt, .{expected_class}); return .zero; } - pub fn toMatchSnapshot(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toMatchSnapshot(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(2); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; @@ -2228,14 +2242,12 @@ pub const Expect = struct { const not = this.flags.not; if (not) { const signature = comptime getSignature("toMatchSnapshot", "", true); - const fmt = signature ++ "\n\nMatcher error: Snapshot matchers cannot be used with not\n"; - globalObject.throwPretty(fmt, .{}); + this.throw(globalThis, signature, "\n\nMatcher error: Snapshot matchers cannot be used with not\n", .{}); } if (this.testScope() == null) { const signature = comptime getSignature("toMatchSnapshot", "", true); - const fmt = signature ++ "\n\nMatcher error: Snapshot matchers cannot be used outside of a test\n"; - globalObject.throwPretty(fmt, .{}); + this.throw(globalThis, signature, "\n\nMatcher error: Snapshot matchers cannot be used outside of a test\n", .{}); return .zero; } @@ -2245,7 +2257,7 @@ pub const Expect = struct { 0 => {}, 1 => { if (arguments[0].isString()) { - arguments[0].toZigString(&hint_string, globalObject); + arguments[0].toZigString(&hint_string, globalThis); } else if (arguments[0].isObject()) { property_matchers = arguments[0]; } @@ -2253,15 +2265,14 @@ pub const Expect = struct { else => { if (!arguments[0].isObject()) { const signature = comptime getSignature("toMatchSnapshot", "properties, hint", false); - const fmt = signature ++ "\n\nMatcher error: Expected properties must be an object\n"; - globalObject.throwPretty(fmt, .{}); + this.throw(globalThis, signature, "\n\nMatcher error: Expected properties must be an object\n", .{}); return .zero; } property_matchers = arguments[0]; if (arguments[1].isString()) { - arguments[1].toZigString(&hint_string, globalObject); + arguments[1].toZigString(&hint_string, globalThis); } }, } @@ -2269,48 +2280,47 @@ pub const Expect = struct { var hint = hint_string.toSlice(default_allocator); defer hint.deinit(); - const value: JSValue = this.getValue(globalObject, thisValue, "toMatchSnapshot", "properties, hint") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toMatchSnapshot", "properties, hint") orelse return .zero; if (!value.isObject() and property_matchers != null) { const signature = comptime getSignature("toMatchSnapshot", "properties, hint", false); - const fmt = signature ++ "\n\nMatcher error: received values must be an object when the matcher has properties\n"; - globalObject.throwPretty(fmt, .{}); + this.throw(globalThis, signature, "\n\nMatcher error: received values must be an object when the matcher has properties\n", .{}); return .zero; } if (property_matchers) |_prop_matchers| { const prop_matchers = _prop_matchers; - if (!value.jestDeepMatch(prop_matchers, globalObject, true)) { + if (!value.jestDeepMatch(prop_matchers, globalThis, true)) { // TODO: print diff with properties from propertyMatchers const signature = comptime getSignature("toMatchSnapshot", "propertyMatchers", false); const fmt = signature ++ "\n\nExpected propertyMatchers to match properties from received object" ++ "\n\nReceived: {any}\n"; - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject }; - globalObject.throwPretty(fmt, .{value.toFmt(globalObject, &formatter)}); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis }; + globalThis.throwPretty(fmt, .{value.toFmt(globalThis, &formatter)}); return .zero; } } - const result = Jest.runner.?.snapshots.getOrPut(this, value, hint.slice(), globalObject) catch |err| { - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject }; + const result = Jest.runner.?.snapshots.getOrPut(this, value, hint.slice(), globalThis) catch |err| { + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis }; const test_file_path = Jest.runner.?.files.get(this.testScope().?.describe.file_id).source.path.text; switch (err) { - error.FailedToOpenSnapshotFile => globalObject.throw("Failed to open snapshot file for test file: {s}", .{test_file_path}), - error.FailedToMakeSnapshotDirectory => globalObject.throw("Failed to make snapshot directory for test file: {s}", .{test_file_path}), - error.FailedToWriteSnapshotFile => globalObject.throw("Failed write to snapshot file: {s}", .{test_file_path}), - error.ParseError => globalObject.throw("Failed to parse snapshot file for: {s}", .{test_file_path}), - else => globalObject.throw("Failed to snapshot value: {any}", .{value.toFmt(globalObject, &formatter)}), + error.FailedToOpenSnapshotFile => globalThis.throw("Failed to open snapshot file for test file: {s}", .{test_file_path}), + error.FailedToMakeSnapshotDirectory => globalThis.throw("Failed to make snapshot directory for test file: {s}", .{test_file_path}), + error.FailedToWriteSnapshotFile => globalThis.throw("Failed write to snapshot file: {s}", .{test_file_path}), + error.ParseError => globalThis.throw("Failed to parse snapshot file for: {s}", .{test_file_path}), + else => globalThis.throw("Failed to snapshot value: {any}", .{value.toFmt(globalThis, &formatter)}), } return .zero; }; if (result) |saved_value| { var pretty_value: MutableString = MutableString.init(default_allocator, 0) catch unreachable; - value.jestSnapshotPrettyFormat(&pretty_value, globalObject) catch { - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject }; - globalObject.throw("Failed to pretty format value: {s}", .{value.toFmt(globalObject, &formatter)}); + value.jestSnapshotPrettyFormat(&pretty_value, globalThis) catch { + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis }; + globalThis.throw("Failed to pretty format value: {s}", .{value.toFmt(globalThis, &formatter)}); return .zero; }; defer pretty_value.deinit(); @@ -2326,35 +2336,35 @@ pub const Expect = struct { const diff_format = DiffFormatter{ .received_string = pretty_value.toOwnedSliceLeaky(), .expected_string = saved_value, - .globalObject = globalObject, + .globalThis = globalThis, }; - globalObject.throwPretty(fmt, .{diff_format}); + globalThis.throwPretty(fmt, .{diff_format}); return .zero; } return .undefined; } - pub fn toBeEmpty(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { - defer this.postMatch(globalObject); + pub fn toBeEmpty(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeEmpty", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeEmpty", "") orelse return .zero; incrementExpectCallCounter(); const not = this.flags.not; var pass = false; - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; - const actual_length = value.getLengthIfPropertyExistsInternal(globalObject); + const actual_length = value.getLengthIfPropertyExistsInternal(globalThis); if (actual_length == std.math.inf(f64)) { if (value.jsTypeLoose().isObject()) { - if (value.isIterable(globalObject)) { + if (value.isIterable(globalThis)) { var any_properties_in_iterator = false; - value.forEach(globalObject, &any_properties_in_iterator, struct { + value.forEach(globalThis, &any_properties_in_iterator, struct { pub fn anythingInIterator( _: *JSC.VM, _: *JSGlobalObject, @@ -2370,7 +2380,7 @@ pub const Expect = struct { .skip_empty_name = false, .include_value = true, - }).init(globalObject, value); + }).init(globalThis, value); defer props_iter.deinit(); pass = props_iter.len == 0; } @@ -2378,11 +2388,11 @@ pub const Expect = struct { const signature = comptime getSignature("toBeEmpty", "", false); const fmt = signature ++ "\n\nExpected value to be a string, object, or iterable" ++ "\n\nReceived: {any}\n"; - globalObject.throwPretty(fmt, .{value.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{value.toFmt(globalThis, &formatter)}); return .zero; } } else if (std.math.isNan(actual_length)) { - globalObject.throw("Received value has non-number length property: {}", .{actual_length}); + globalThis.throw("Received value has non-number length property: {}", .{actual_length}); return .zero; } else { pass = actual_length == 0; @@ -2392,7 +2402,7 @@ pub const Expect = struct { const signature = comptime getSignature("toBeEmpty", "", true); const fmt = signature ++ "\n\nExpected value not to be a string, object, or iterable" ++ "\n\nReceived: {any}\n"; - globalObject.throwPretty(fmt, .{value.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{value.toFmt(globalThis, &formatter)}); return .zero; } @@ -2403,14 +2413,14 @@ pub const Expect = struct { const signature = comptime getSignature("toBeEmpty", "", true); const fmt = signature ++ "\n\nExpected value not to be empty" ++ "\n\nReceived: {any}\n"; - globalObject.throwPretty(fmt, .{value.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{value.toFmt(globalThis, &formatter)}); return .zero; } const signature = comptime getSignature("toBeEmpty", "", false); const fmt = signature ++ "\n\nExpected value to be empty" ++ "\n\nReceived: {any}\n"; - globalObject.throwPretty(fmt, .{value.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{value.toFmt(globalThis, &formatter)}); return .zero; } @@ -2432,13 +2442,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeEmptyObject", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeEmptyObject", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeEmptyObject", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeEmptyObject", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2459,13 +2469,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeNil", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNil", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeNil", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNil", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2486,13 +2496,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeArray", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeArray", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeArray", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeArray", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2530,13 +2540,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeArrayOfSize", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeArrayOfSize", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeArrayOfSize", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeArrayOfSize", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2557,13 +2567,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeBoolean", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeBoolean", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeBoolean", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeBoolean", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2634,13 +2644,13 @@ pub const Expect = struct { const expected_str = expected.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeTypeOf", "", true) ++ "\n\n" ++ "Expected type: not {any}\n" ++ "Received type: \"{s}\"\nReceived value: {any}\n"; - globalThis.throwPretty(fmt, .{ expected_str, whatIsTheType, received }); + const signature = comptime getSignature("toBeTypeOf", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Expected type: not {any}\n" ++ "Received type: \"{s}\"\nReceived value: {any}\n", .{ expected_str, whatIsTheType, received }); return .zero; } - const fmt = comptime getSignature("toBeTypeOf", "", false) ++ "\n\n" ++ "Expected type: {any}\n" ++ "Received type: \"{s}\"\nReceived value: {any}\n"; - globalThis.throwPretty(fmt, .{ expected_str, whatIsTheType, received }); + const signature = comptime getSignature("toBeTypeOf", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Expected type: {any}\n" ++ "Received type: \"{s}\"\nReceived value: {any}\n", .{ expected_str, whatIsTheType, received }); return .zero; } @@ -2661,13 +2671,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeTrue", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeTrue", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeTrue", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeTrue", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2688,13 +2698,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeFalse", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFalse", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeFalse", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFalse", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2715,13 +2725,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeNumber", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNumber", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeNumber", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNumber", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2742,13 +2752,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeInteger", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeInteger", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeInteger", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeInteger", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2769,13 +2779,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeObject", "", true) ++ "\n\nExpected value not to be an object" ++ "\n\nReceived: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeObject", "", true); + this.throw(globalThis, signature, "\n\nExpected value not to be an object" ++ "\n\nReceived: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeObject", "", false) ++ "\n\nExpected value to be an object" ++ "\n\nReceived: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeObject", "", false); + this.throw(globalThis, signature, "\n\nExpected value to be an object" ++ "\n\nReceived: {any}\n", .{received}); return .zero; } @@ -2802,13 +2812,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeFinite", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFinite", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeFinite", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFinite", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2835,13 +2845,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBePositive", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBePositive", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBePositive", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBePositive", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2868,13 +2878,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeNegative", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNegative", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeNegative", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeNegative", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -2929,15 +2939,15 @@ pub const Expect = struct { if (not) { const expected_line = "Expected: not between {any} (inclusive) and {any} (exclusive)\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeWithin", "start, end", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ start_fmt, end_fmt, received_fmt }); + const signature = comptime getSignature("toBeWithin", "start, end", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ start_fmt, end_fmt, received_fmt }); return .zero; } const expected_line = "Expected: between {any} (inclusive) and {any} (exclusive)\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toBeWithin", "start, end", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ start_fmt, end_fmt, received_fmt }); + const signature = comptime getSignature("toBeWithin", "start, end", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ start_fmt, end_fmt, received_fmt }); return .zero; } @@ -3013,13 +3023,13 @@ pub const Expect = struct { const value_fmt = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toEqualIgnoringWhitespace", "expected", true) ++ "\n\n" ++ "Expected: not {any}\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toEqualIgnoringWhitespace", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ "Expected: not {any}\n" ++ "Received: {any}\n", .{ expected_fmt, value_fmt }); return .zero; } - const fmt = comptime getSignature("toEqualIgnoringWhitespace", "expected", false) ++ "\n\n" ++ "Expected: {any}\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toEqualIgnoringWhitespace", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ "Expected: {any}\n" ++ "Received: {any}\n", .{ expected_fmt, value_fmt }); return .zero; } @@ -3040,13 +3050,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeSymbol", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeSymbol", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeSymbol", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeSymbol", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -3067,13 +3077,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeFunction", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFunction", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeFunction", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeFunction", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -3094,13 +3104,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeDate", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeDate", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeDate", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeDate", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -3122,13 +3132,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeValidDate", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeValidDate", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeValidDate", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeValidDate", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -3149,13 +3159,13 @@ pub const Expect = struct { const received = value.toFmt(globalThis, &formatter); if (not) { - const fmt = comptime getSignature("toBeString", "", true) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeString", "", true); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } - const fmt = comptime getSignature("toBeString", "", false) ++ "\n\n" ++ "Received: {any}\n"; - globalThis.throwPretty(fmt, .{received}); + const signature = comptime getSignature("toBeString", "", false); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}\n", .{received}); return .zero; } @@ -3204,15 +3214,15 @@ pub const Expect = struct { if (not) { const expected_line = "Expected to not include: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toInclude", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toInclude", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected to include: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toInclude", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toInclude", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } @@ -3295,16 +3305,16 @@ pub const Expect = struct { if (not) { if (countAsNum == 0) { const expected_line = "Expected to include: {any} \n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, expect_string_fmt }); } else if (countAsNum == 1) { const expected_line = "Expected not to include: {any} \n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, expect_string_fmt }); } else { const expected_line = "Expected not to include: {any} {any} times \n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, times_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, times_fmt, expect_string_fmt }); } return .zero; @@ -3312,16 +3322,16 @@ pub const Expect = struct { if (countAsNum == 0) { const expected_line = "Expected to not include: {any}\n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, expect_string_fmt }); } else if (countAsNum == 1) { const expected_line = "Expected to include: {any}\n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, expect_string_fmt }); } else { const expected_line = "Expected to include: {any} {any} times \n"; - const fmt = comptime getSignature("toIncludeRepeated", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ substring_fmt, times_fmt, expect_string_fmt }); + const signature = comptime getSignature("toIncludeRepeated", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ substring_fmt, times_fmt, expect_string_fmt }); } return .zero; @@ -3377,16 +3387,13 @@ pub const Expect = struct { if (not) { const signature = comptime getSignature("toSatisfy", "expected", true); - const fmt = signature ++ "\n\nExpected: not {any}\n"; - globalThis.throwPretty(fmt, .{predicate.toFmt(globalThis, &formatter)}); + this.throw(globalThis, signature, "\n\nExpected: not {any}\n", .{predicate.toFmt(globalThis, &formatter)}); return .zero; } const signature = comptime getSignature("toSatisfy", "expected", false); - const fmt = signature ++ "\n\nExpected: {any}\nReceived: {any}\n"; - - globalThis.throwPretty(fmt, .{ + this.throw(globalThis, signature, "\n\nExpected: {any}\nReceived: {any}\n", .{ predicate.toFmt(globalThis, &formatter), value.toFmt(globalThis, &formatter), }); @@ -3439,15 +3446,15 @@ pub const Expect = struct { if (not) { const expected_line = "Expected to not start with: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toStartWith", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toStartWith", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected to start with: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toStartWith", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toStartWith", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } @@ -3496,103 +3503,103 @@ pub const Expect = struct { if (not) { const expected_line = "Expected to not end with: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toEndWith", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toEndWith", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected to end with: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toEndWith", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalThis.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toEndWith", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toBeInstanceOf(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer this.postMatch(globalObject); + pub fn toBeInstanceOf(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toBeInstanceOf() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toBeInstanceOf() requires 1 argument", .{}); return .zero; } incrementExpectCallCounter(); - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const expected_value = arguments[0]; if (!expected_value.isConstructor()) { - globalObject.throw("Expected value must be a function: {any}", .{expected_value.toFmt(globalObject, &formatter)}); + globalThis.throw("Expected value must be a function: {any}", .{expected_value.toFmt(globalThis, &formatter)}); return .zero; } expected_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toBeInstanceOf", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toBeInstanceOf", "expected") orelse return .zero; const not = this.flags.not; - var pass = value.isInstanceOf(globalObject, expected_value); + var pass = value.isInstanceOf(globalThis, expected_value); if (not) pass = !pass; if (pass) return .undefined; // handle failure - const expected_fmt = expected_value.toFmt(globalObject, &formatter); - const value_fmt = value.toFmt(globalObject, &formatter); + const expected_fmt = expected_value.toFmt(globalThis, &formatter); + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected constructor: not {any}\n"; const received_line = "Received value: {any}\n"; - const fmt = comptime getSignature("toBeInstanceOf", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeInstanceOf", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected constructor: {any}\n"; const received_line = "Received value: {any}\n"; - const fmt = comptime getSignature("toBeInstanceOf", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toBeInstanceOf", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toMatch(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn toMatch(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { JSC.markBinding(@src()); - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("toMatch() requires 1 argument", .{}); + globalThis.throwInvalidArguments("toMatch() requires 1 argument", .{}); return .zero; } incrementExpectCallCounter(); - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; const expected_value = arguments[0]; if (!expected_value.isString() and !expected_value.isRegExp()) { - globalObject.throw("Expected value must be a string or regular expression: {any}", .{expected_value.toFmt(globalObject, &formatter)}); + globalThis.throw("Expected value must be a string or regular expression: {any}", .{expected_value.toFmt(globalThis, &formatter)}); return .zero; } expected_value.ensureStillAlive(); - const value: JSValue = this.getValue(globalObject, thisValue, "toMatch", "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toMatch", "expected") orelse return .zero; if (!value.isString()) { - globalObject.throw("Received value must be a string: {any}", .{value.toFmt(globalObject, &formatter)}); + globalThis.throw("Received value must be a string: {any}", .{value.toFmt(globalThis, &formatter)}); return .zero; } const not = this.flags.not; var pass: bool = brk: { if (expected_value.isString()) { - break :brk value.stringIncludes(globalObject, expected_value); + break :brk value.stringIncludes(globalThis, expected_value); } else if (expected_value.isRegExp()) { - break :brk expected_value.toMatch(globalObject, value); + break :brk expected_value.toMatch(globalThis, value); } unreachable; }; @@ -3601,40 +3608,40 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - const expected_fmt = expected_value.toFmt(globalObject, &formatter); - const value_fmt = value.toFmt(globalObject, &formatter); + const expected_fmt = expected_value.toFmt(globalThis, &formatter); + const value_fmt = value.toFmt(globalThis, &formatter); if (not) { const expected_line = "Expected substring or pattern: not {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toMatch", "expected", true) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toMatch", "expected", true); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } const expected_line = "Expected substring or pattern: {any}\n"; const received_line = "Received: {any}\n"; - const fmt = comptime getSignature("toMatch", "expected", false) ++ "\n\n" ++ expected_line ++ received_line; - globalObject.throwPretty(fmt, .{ expected_fmt, value_fmt }); + const signature = comptime getSignature("toMatch", "expected", false); + this.throw(globalThis, signature, "\n\n" ++ expected_line ++ received_line, .{ expected_fmt, value_fmt }); return .zero; } - pub fn toHaveBeenCalled(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn toHaveBeenCalled(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveBeenCalled", "") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveBeenCalled", "") orelse return .zero; const calls = JSMockFunction__getCalls(value); incrementExpectCallCounter(); if (calls == .zero or !calls.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } - var pass = calls.getLength(globalObject) > 0; + var pass = calls.getLength(globalThis) > 0; const not = this.flags.not; if (not) pass = !pass; @@ -3643,43 +3650,41 @@ pub const Expect = struct { // handle failure if (not) { const signature = comptime getSignature("toHaveBeenCalled", "", true); - const fmt = signature ++ "\n\n" ++ "Expected number of calls: 0\n" ++ "Received number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{calls.getLength(globalObject)}); + this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: 0\n" ++ "Received number of calls: {any}\n", .{calls.getLength(globalThis)}); return .zero; } const signature = comptime getSignature("toHaveBeenCalled", "", false); - const fmt = signature ++ "\n\n" ++ "Expected number of calls: \\>= 1\n" ++ "Received number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{calls.getLength(globalObject)}); + this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: \\>= 1\n" ++ "Received number of calls: {any}\n", .{calls.getLength(globalThis)}); return .zero; } - pub fn toHaveBeenCalledTimes(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn toHaveBeenCalledTimes(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); const arguments_ = callframe.arguments(1); const arguments: []const JSValue = arguments_.ptr[0..arguments_.len]; - defer this.postMatch(globalObject); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveBeenCalledTimes", "expected") orelse return .zero; + defer this.postMatch(globalThis); + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveBeenCalledTimes", "expected") orelse return .zero; incrementExpectCallCounter(); const calls = JSMockFunction__getCalls(value); if (calls == .zero or !calls.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } if (arguments.len < 1 or !arguments[0].isUInt32AsAnyInt()) { - globalObject.throwInvalidArguments("toHaveBeenCalledTimes() requires 1 non-negative integer argument", .{}); + globalThis.throwInvalidArguments("toHaveBeenCalledTimes() requires 1 non-negative integer argument", .{}); return .zero; } - const times = arguments[0].coerce(i32, globalObject); + const times = arguments[0].coerce(i32, globalThis); - var pass = @as(i32, @intCast(calls.getLength(globalObject))) == times; + var pass = @as(i32, @intCast(calls.getLength(globalThis))) == times; const not = this.flags.not; if (not) pass = !pass; @@ -3688,21 +3693,19 @@ pub const Expect = struct { // handle failure if (not) { const signature = comptime getSignature("toHaveBeenCalledTimes", "expected", true); - const fmt = signature ++ "\n\n" ++ "Expected number of calls: not {any}\n" ++ "Received number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ times, calls.getLength(globalObject) }); + this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: not {any}\n" ++ "Received number of calls: {any}\n", .{ times, calls.getLength(globalThis) }); return .zero; } const signature = comptime getSignature("toHaveBeenCalledTimes", "expected", false); - const fmt = signature ++ "\n\n" ++ "Expected number of calls: {any}\n" ++ "Received number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ times, calls.getLength(globalObject) }); + this.throw(globalThis, signature, "\n\n" ++ "Expected number of calls: {any}\n" ++ "Received number of calls: {any}\n", .{ times, calls.getLength(globalThis) }); return .zero; } - pub fn toMatchObject(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn toMatchObject(this: *Expect, globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { JSC.markBinding(@src()); - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const thisValue = callFrame.this(); const args = callFrame.arguments(1).slice(); @@ -3710,36 +3713,36 @@ pub const Expect = struct { const not = this.flags.not; - const received_object: JSValue = this.getValue(globalObject, thisValue, "toMatchObject", "expected") orelse return .zero; + const received_object: JSValue = this.getValue(globalThis, thisValue, "toMatchObject", "expected") orelse return .zero; if (!received_object.isObject()) { const matcher_error = "\n\nMatcher error: received value must be a non-null object\n"; if (not) { - const fmt = comptime getSignature("toMatchObject", "expected", true) ++ matcher_error; - globalObject.throwPretty(fmt, .{}); + const signature = comptime getSignature("toMatchObject", "expected", true); + this.throw(globalThis, signature, matcher_error, .{}); return .zero; } - const fmt = comptime getSignature("toMatchObject", "expected", false) ++ matcher_error; - globalObject.throwPretty(fmt, .{}); + const signature = comptime getSignature("toMatchObject", "expected", false); + this.throw(globalThis, signature, matcher_error, .{}); return .zero; } if (args.len < 1 or !args[0].isObject()) { const matcher_error = "\n\nMatcher error: expected value must be a non-null object\n"; if (not) { - const fmt = comptime getSignature("toMatchObject", "", true) ++ matcher_error; - globalObject.throwPretty(fmt, .{}); + const signature = comptime getSignature("toMatchObject", "", true); + this.throw(globalThis, signature, matcher_error, .{}); return .zero; } - const fmt = comptime getSignature("toMatchObject", "", false) ++ matcher_error; - globalObject.throwPretty(fmt, .{}); + const signature = comptime getSignature("toMatchObject", "", false); + this.throw(globalThis, signature, matcher_error, .{}); return .zero; } const property_matchers = args[0]; - var pass = received_object.jestDeepMatch(property_matchers, globalObject, true); + var pass = received_object.jestDeepMatch(property_matchers, globalThis, true); if (not) pass = !pass; if (pass) return .undefined; @@ -3748,59 +3751,57 @@ pub const Expect = struct { const diff_formatter = DiffFormatter{ .received = received_object, .expected = property_matchers, - .globalObject = globalObject, + .globalThis = globalThis, .not = not, }; if (not) { const signature = comptime getSignature("toMatchObject", "expected", true); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } const signature = comptime getSignature("toMatchObject", "expected", false); - const fmt = signature ++ "\n\n{any}\n"; - globalObject.throwPretty(fmt, .{diff_formatter}); + this.throw(globalThis, signature, "\n\n{any}\n", .{diff_formatter}); return .zero; } - pub fn toHaveBeenCalledWith(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn toHaveBeenCalledWith(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); const arguments_ = callframe.argumentsPtr()[0..callframe.argumentsCount()]; const arguments: []const JSValue = arguments_.ptr[0..arguments_.len]; - defer this.postMatch(globalObject); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveBeenCalledWith", "expected") orelse return .zero; + defer this.postMatch(globalThis); + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveBeenCalledWith", "expected") orelse return .zero; incrementExpectCallCounter(); const calls = JSMockFunction__getCalls(value); if (calls == .zero or !calls.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } var pass = false; - if (calls.getLength(globalObject) > 0) { - var itr = calls.arrayIterator(globalObject); + if (calls.getLength(globalThis) > 0) { + var itr = calls.arrayIterator(globalThis); while (itr.next()) |callItem| { if (callItem == .zero or !callItem.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function with calls: {}", .{value}); + globalThis.throw("Expected value must be a mock function with calls: {}", .{value}); return .zero; } - if (callItem.getLength(globalObject) != arguments.len) { + if (callItem.getLength(globalThis) != arguments.len) { continue; } - var callItr = callItem.arrayIterator(globalObject); + var callItr = callItem.arrayIterator(globalThis); var match = true; while (callItr.next()) |callArg| { - if (!callArg.jestDeepEquals(arguments[callItr.i - 1], globalObject)) { + if (!callArg.jestDeepEquals(arguments[callItr.i - 1], globalThis)) { match = false; break; } @@ -3820,54 +3821,52 @@ pub const Expect = struct { // handle failure if (not) { const signature = comptime getSignature("toHaveBeenCalledWith", "expected", true); - const fmt = signature ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{calls.getLength(globalObject)}); + this.throw(globalThis, signature, "\n\n" ++ "Number of calls: {any}\n", .{calls.getLength(globalThis)}); return .zero; } const signature = comptime getSignature("toHaveBeenCalledWith", "expected", false); - const fmt = signature ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{calls.getLength(globalObject)}); + this.throw(globalThis, signature, "\n\n" ++ "Number of calls: {any}\n", .{calls.getLength(globalThis)}); return .zero; } - pub fn toHaveBeenLastCalledWith(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn toHaveBeenLastCalledWith(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); const arguments_ = callframe.argumentsPtr()[0..callframe.argumentsCount()]; const arguments: []const JSValue = arguments_.ptr[0..arguments_.len]; - defer this.postMatch(globalObject); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveBeenLastCalledWith", "expected") orelse return .zero; + defer this.postMatch(globalThis); + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveBeenLastCalledWith", "expected") orelse return .zero; incrementExpectCallCounter(); const calls = JSMockFunction__getCalls(value); if (calls == .zero or !calls.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } - const totalCalls = @as(u32, @intCast(calls.getLength(globalObject))); + const totalCalls = @as(u32, @intCast(calls.getLength(globalThis))); var lastCallValue: JSValue = .zero; var pass = totalCalls > 0; if (pass) { - lastCallValue = calls.getIndex(globalObject, totalCalls - 1); + lastCallValue = calls.getIndex(globalThis, totalCalls - 1); if (lastCallValue == .zero or !lastCallValue.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function with calls: {}", .{value}); + globalThis.throw("Expected value must be a mock function with calls: {}", .{value}); return .zero; } - if (lastCallValue.getLength(globalObject) != arguments.len) { + if (lastCallValue.getLength(globalThis) != arguments.len) { pass = false; } else { - var itr = lastCallValue.arrayIterator(globalObject); + var itr = lastCallValue.arrayIterator(globalThis); while (itr.next()) |callArg| { - if (!callArg.jestDeepEquals(arguments[itr.i - 1], globalObject)) { + if (!callArg.jestDeepEquals(arguments[itr.i - 1], globalThis)) { pass = false; break; } @@ -3880,65 +3879,63 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const received_fmt = lastCallValue.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const received_fmt = lastCallValue.toFmt(globalThis, &formatter); if (not) { const signature = comptime getSignature("toHaveBeenLastCalledWith", "expected", true); - const fmt = signature ++ "\n\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ received_fmt, totalCalls }); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n", .{ received_fmt, totalCalls }); return .zero; } const signature = comptime getSignature("toHaveBeenLastCalledWith", "expected", false); - const fmt = signature ++ "\n\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ received_fmt, totalCalls }); + this.throw(globalThis, signature, "\n\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n", .{ received_fmt, totalCalls }); return .zero; } - pub fn toHaveBeenNthCalledWith(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn toHaveBeenNthCalledWith(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); const arguments_ = callframe.argumentsPtr()[0..callframe.argumentsCount()]; const arguments: []const JSValue = arguments_.ptr[0..arguments_.len]; - defer this.postMatch(globalObject); - const value: JSValue = this.getValue(globalObject, thisValue, "toHaveBeenNthCalledWith", "expected") orelse return .zero; + defer this.postMatch(globalThis); + const value: JSValue = this.getValue(globalThis, thisValue, "toHaveBeenNthCalledWith", "expected") orelse return .zero; incrementExpectCallCounter(); const calls = JSMockFunction__getCalls(value); if (calls == .zero or !calls.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } - const nthCallNum = if (arguments.len > 0 and arguments[0].isUInt32AsAnyInt()) arguments[0].coerce(i32, globalObject) else 0; + const nthCallNum = if (arguments.len > 0 and arguments[0].isUInt32AsAnyInt()) arguments[0].coerce(i32, globalThis) else 0; if (nthCallNum < 1) { - globalObject.throwInvalidArguments("toHaveBeenNthCalledWith() requires a positive integer argument", .{}); + globalThis.throwInvalidArguments("toHaveBeenNthCalledWith() requires a positive integer argument", .{}); return .zero; } - const totalCalls = calls.getLength(globalObject); + const totalCalls = calls.getLength(globalThis); var nthCallValue: JSValue = .zero; var pass = totalCalls >= nthCallNum; if (pass) { - nthCallValue = calls.getIndex(globalObject, @as(u32, @intCast(nthCallNum)) - 1); + nthCallValue = calls.getIndex(globalThis, @as(u32, @intCast(nthCallNum)) - 1); if (nthCallValue == .zero or !nthCallValue.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function with calls: {}", .{value}); + globalThis.throw("Expected value must be a mock function with calls: {}", .{value}); return .zero; } - if (nthCallValue.getLength(globalObject) != (arguments.len - 1)) { + if (nthCallValue.getLength(globalThis) != (arguments.len - 1)) { pass = false; } else { - var itr = nthCallValue.arrayIterator(globalObject); + var itr = nthCallValue.arrayIterator(globalThis); while (itr.next()) |callArg| { - if (!callArg.jestDeepEquals(arguments[itr.i], globalObject)) { + if (!callArg.jestDeepEquals(arguments[itr.i], globalThis)) { pass = false; break; } @@ -3951,19 +3948,17 @@ pub const Expect = struct { if (pass) return .undefined; // handle failure - var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - const received_fmt = nthCallValue.toFmt(globalObject, &formatter); + var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + const received_fmt = nthCallValue.toFmt(globalThis, &formatter); if (not) { const signature = comptime getSignature("toHaveBeenNthCalledWith", "expected", true); - const fmt = signature ++ "\n\n" ++ "n: {any}\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ nthCallNum, received_fmt, totalCalls }); + this.throw(globalThis, signature, "\n\n" ++ "n: {any}\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n", .{ nthCallNum, received_fmt, totalCalls }); return .zero; } const signature = comptime getSignature("toHaveBeenNthCalledWith", "expected", false); - const fmt = signature ++ "\n\n" ++ "n: {any}\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n"; - globalObject.throwPretty(fmt, .{ nthCallNum, received_fmt, totalCalls }); + this.throw(globalThis, signature, "\n\n" ++ "n: {any}\n" ++ "Received: {any}" ++ "\n\n" ++ "Number of calls: {any}\n", .{ nthCallNum, received_fmt, totalCalls }); return .zero; } @@ -3975,44 +3970,44 @@ pub const Expect = struct { pub const Map = bun.ComptimeEnumMap(ReturnStatus); }; - inline fn toHaveReturnedTimesFn(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, comptime known_index: ?i32) JSC.JSValue { + inline fn toHaveReturnedTimesFn(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame, comptime known_index: ?i32) JSC.JSValue { JSC.markBinding(@src()); const thisValue = callframe.this(); const arguments = callframe.arguments(1).slice(); - defer this.postMatch(globalObject); + defer this.postMatch(globalThis); const name = comptime if (known_index != null and known_index.? == 0) "toHaveReturned" else "toHaveReturnedTimes"; - const value: JSValue = this.getValue(globalObject, thisValue, name, if (known_index != null and known_index.? == 0) "" else "expected") orelse return .zero; + const value: JSValue = this.getValue(globalThis, thisValue, name, if (known_index != null and known_index.? == 0) "" else "expected") orelse return .zero; incrementExpectCallCounter(); const returns = JSMockFunction__getReturns(value); if (returns == .zero or !returns.jsType().isArray()) { - globalObject.throw("Expected value must be a mock function: {}", .{value}); + globalThis.throw("Expected value must be a mock function: {}", .{value}); return .zero; } const return_count: i32 = if (known_index) |index| index else brk: { if (arguments.len < 1 or !arguments[0].isUInt32AsAnyInt()) { - globalObject.throwInvalidArguments(name ++ "() requires 1 non-negative integer argument", .{}); + globalThis.throwInvalidArguments(name ++ "() requires 1 non-negative integer argument", .{}); return .zero; } - break :brk arguments[0].coerce(i32, globalObject); + break :brk arguments[0].coerce(i32, globalThis); }; var pass = false; const index: u32 = @as(u32, @intCast(return_count)) -| 1; const times_value = returns.getDirectIndex( - globalObject, + globalThis, index, ); - const total_count = returns.getLength(globalObject); + const total_count = returns.getLength(globalThis); const return_status: ReturnStatus = brk: { // Returns is an array of: @@ -4020,11 +4015,11 @@ pub const Expect = struct { // { type: "throw" | "incomplete" | "return", value: any} // if (total_count >= return_count and times_value.isCell()) { - if (times_value.get(globalObject, "type")) |type_string| { + if (times_value.get(globalThis, "type")) |type_string| { if (type_string.isString()) { - break :brk ReturnStatus.Map.fromJS(globalObject, type_string) orelse { - if (!globalObject.hasException()) - globalObject.throw("Expected value must be a mock function with returns: {}", .{value}); + break :brk ReturnStatus.Map.fromJS(globalThis, type_string) orelse { + if (!globalThis.hasException()) + globalThis.throw("Expected value must be a mock function with returns: {}", .{value}); return .zero; }; } @@ -4033,7 +4028,7 @@ pub const Expect = struct { break :brk ReturnStatus.incomplete; }; - if (globalObject.hasException()) + if (globalThis.hasException()) return .zero; pass = return_status == ReturnStatus.@"return"; @@ -4046,29 +4041,28 @@ pub const Expect = struct { const signature = comptime getSignature(name, "expected", false); const fmt = signature ++ "\n\n" ++ "Function threw an exception\n{any}\n"; var formatter = JSC.ConsoleObject.Formatter{ - .globalThis = globalObject, + .globalThis = globalThis, .quote_strings = true, }; - globalObject.throwPretty(fmt, .{times_value.get(globalObject, "value").?.toFmt(globalObject, &formatter)}); + globalThis.throwPretty(fmt, .{times_value.get(globalThis, "value").?.toFmt(globalThis, &formatter)}); return .zero; } switch (not) { inline else => |is_not| { const signature = comptime getSignature(name, "expected", is_not); - const fmt = signature ++ "\n\n" ++ "Expected number of successful calls: {d}\n" ++ "Received number of calls: {d}\n"; - globalObject.throwPretty(fmt, .{ return_count, total_count }); + this.throw(globalThis, signature, "\n\n" ++ "Expected number of successful calls: {d}\n" ++ "Received number of calls: {d}\n", .{ return_count, total_count }); return .zero; }, } } - pub fn toHaveReturned(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { - return toHaveReturnedTimesFn(this, globalObject, callframe, 1); + pub fn toHaveReturned(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + return toHaveReturnedTimesFn(this, globalThis, callframe, 1); } - pub fn toHaveReturnedTimes(this: *Expect, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { - return toHaveReturnedTimesFn(this, globalObject, callframe, null); + pub fn toHaveReturnedTimes(this: *Expect, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + return toHaveReturnedTimesFn(this, globalThis, callframe, null); } pub const toHaveReturnedWith = notImplementedJSCFn; @@ -4078,73 +4072,73 @@ pub const Expect = struct { pub const toThrowErrorMatchingSnapshot = notImplementedJSCFn; pub const toThrowErrorMatchingInlineSnapshot = notImplementedJSCFn; - pub fn getStaticNot(globalObject: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { - return ExpectStatic.create(globalObject, .{ .not = true }); + pub fn getStaticNot(globalThis: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { + return ExpectStatic.create(globalThis, .{ .not = true }); } - pub fn getStaticResolvesTo(globalObject: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { - return ExpectStatic.create(globalObject, .{ .promise = .resolves }); + pub fn getStaticResolvesTo(globalThis: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { + return ExpectStatic.create(globalThis, .{ .promise = .resolves }); } - pub fn getStaticRejectsTo(globalObject: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { - return ExpectStatic.create(globalObject, .{ .promise = .rejects }); + pub fn getStaticRejectsTo(globalThis: *JSGlobalObject, _: JSValue, _: JSValue) callconv(.C) JSValue { + return ExpectStatic.create(globalThis, .{ .promise = .rejects }); } - pub fn any(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectAny.call(globalObject, callFrame); + pub fn any(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectAny.call(globalThis, callFrame); } - pub fn anything(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectAnything.call(globalObject, callFrame); + pub fn anything(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectAnything.call(globalThis, callFrame); } - pub fn closeTo(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectCloseTo.call(globalObject, callFrame); + pub fn closeTo(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectCloseTo.call(globalThis, callFrame); } - pub fn objectContaining(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectObjectContaining.call(globalObject, callFrame); + pub fn objectContaining(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectObjectContaining.call(globalThis, callFrame); } - pub fn stringContaining(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectStringContaining.call(globalObject, callFrame); + pub fn stringContaining(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectStringContaining.call(globalThis, callFrame); } - pub fn stringMatching(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectStringMatching.call(globalObject, callFrame); + pub fn stringMatching(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectStringMatching.call(globalThis, callFrame); } - pub fn arrayContaining(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return ExpectArrayContaining.call(globalObject, callFrame); + pub fn arrayContaining(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return ExpectArrayContaining.call(globalThis, callFrame); } /// Implements `expect.extend({ ... })` - pub fn extend(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn extend(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isObject()) { - globalObject.throwPretty("expect.extend(matchers)\n\nExpected an object containing matchers\n", .{}); + globalThis.throwPretty("expect.extend(matchers)\n\nExpected an object containing matchers\n", .{}); return .zero; } - var expect_proto = Expect__getPrototype(globalObject); - var expect_constructor = Expect.getConstructor(globalObject); - var expect_static_proto = ExpectStatic__getPrototype(globalObject); + var expect_proto = Expect__getPrototype(globalThis); + var expect_constructor = Expect.getConstructor(globalThis); + var expect_static_proto = ExpectStatic__getPrototype(globalThis); const matchers_to_register = args[0]; { var iter = JSC.JSPropertyIterator(.{ .skip_empty_name = false, .include_value = true, - }).init(globalObject, matchers_to_register); + }).init(globalThis, matchers_to_register); defer iter.deinit(); while (iter.next()) |*matcher_name| { const matcher_fn: JSValue = iter.value; if (!matcher_fn.jsType().isFunction()) { - const type_name = if (matcher_fn.isNull()) bun.String.static("null") else bun.String.init(matcher_fn.jsTypeString(globalObject).getZigString(globalObject)); - globalObject.throwInvalidArguments("expect.extend: `{s}` is not a valid matcher. Must be a function, is \"{s}\"", .{ matcher_name, type_name }); + const type_name = if (matcher_fn.isNull()) bun.String.static("null") else bun.String.init(matcher_fn.jsTypeString(globalThis).getZigString(globalThis)); + globalThis.throwInvalidArguments("expect.extend: `{s}` is not a valid matcher. Must be a function, is \"{s}\"", .{ matcher_name, type_name }); return .zero; } @@ -4152,28 +4146,28 @@ pub const Expect = struct { // Even though they point to the same native functions for all matchers, // multiple instances are created because each instance will hold the matcher_fn as a property - const wrapper_fn = Bun__JSWrappingFunction__create(globalObject, matcher_name, &Expect.applyCustomMatcher, matcher_fn, true); + const wrapper_fn = Bun__JSWrappingFunction__create(globalThis, matcher_name, &Expect.applyCustomMatcher, matcher_fn, true); - expect_proto.put(globalObject, matcher_name, wrapper_fn); - expect_constructor.put(globalObject, matcher_name, wrapper_fn); - expect_static_proto.put(globalObject, matcher_name, wrapper_fn); + expect_proto.put(globalThis, matcher_name, wrapper_fn); + expect_constructor.put(globalThis, matcher_name, wrapper_fn); + expect_static_proto.put(globalThis, matcher_name, wrapper_fn); } } - globalObject.bunVM().autoGarbageCollect(); + globalThis.bunVM().autoGarbageCollect(); return .undefined; } const CustomMatcherParamsFormatter = struct { colors: bool, - globalObject: *JSC.JSGlobalObject, + globalThis: *JSC.JSGlobalObject, matcher_fn: JSValue, pub fn format(this: CustomMatcherParamsFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { // try to detect param names from matcher_fn (user function) source code if (JSC.JSFunction.getSourceCode(this.matcher_fn)) |source_str| { - var source_slice = source_str.toSlice(this.globalObject.allocator()); + var source_slice = source_str.toSlice(this.globalThis.allocator()); defer source_slice.deinit(); var source: string = source_slice.slice(); @@ -4212,11 +4206,11 @@ pub const Expect = struct { } }; - fn throwInvalidMatcherError(globalObject: *JSC.JSGlobalObject, matcher_name: bun.String, result: JSValue) void { + fn throwInvalidMatcherError(globalThis: *JSC.JSGlobalObject, matcher_name: bun.String, result: JSValue) void { @setCold(true); var formatter = JSC.ConsoleObject.Formatter{ - .globalThis = globalObject, + .globalThis = globalThis, .quote_strings = true, }; @@ -4226,38 +4220,38 @@ pub const Expect = struct { " {{message?: string | function, pass: boolean}}\n" ++ "'{any}' was returned"; const err = switch (Output.enable_ansi_colors) { - inline else => |colors| globalObject.createErrorInstance(Output.prettyFmt(fmt, colors), .{ matcher_name, result.toFmt(globalObject, &formatter) }), + inline else => |colors| globalThis.createErrorInstance(Output.prettyFmt(fmt, colors), .{ matcher_name, result.toFmt(globalThis, &formatter) }), }; - err.put(globalObject, ZigString.static("name"), ZigString.init("InvalidMatcherError").toValueGC(globalObject)); - globalObject.throwValue(err); + err.put(globalThis, ZigString.static("name"), ZigString.init("InvalidMatcherError").toValueGC(globalThis)); + globalThis.throwValue(err); } /// Execute the custom matcher for the given args (the left value + the args passed to the matcher call). /// This function is called both for symmetric and asymmetric matching. /// If silent=false, throws an exception in JS if the matcher result didn't result in a pass (or if the matcher result is invalid). - pub fn executeCustomMatcher(globalObject: *JSC.JSGlobalObject, matcher_name: bun.String, matcher_fn: JSValue, args: []const JSValue, flags: Expect.Flags, silent: bool) bool { + pub fn executeCustomMatcher(globalThis: *JSC.JSGlobalObject, matcher_name: bun.String, matcher_fn: JSValue, args: []const JSValue, flags: Expect.Flags, silent: bool) bool { // prepare the this object - const matcher_context = globalObject.bunVM().allocator.create(ExpectMatcherContext) catch { - globalObject.throwOutOfMemory(); + const matcher_context = globalThis.bunVM().allocator.create(ExpectMatcherContext) catch { + globalThis.throwOutOfMemory(); return false; }; matcher_context.flags = flags; - const matcher_context_jsvalue = matcher_context.toJS(globalObject); + const matcher_context_jsvalue = matcher_context.toJS(globalThis); matcher_context_jsvalue.ensureStillAlive(); // call the custom matcher implementation - var result = matcher_fn.callWithThis(globalObject, matcher_context_jsvalue, args); + var result = matcher_fn.callWithThis(globalThis, matcher_context_jsvalue, args); assert(!result.isEmpty()); if (result.toError()) |err| { - globalObject.throwValue(err); + globalThis.throwValue(err); return false; } // support for async matcher results if (result.asAnyPromise()) |promise| { - const vm = globalObject.vm(); + const vm = globalThis.vm(); promise.setHandled(vm); - globalObject.bunVM().waitForPromise(promise); + globalThis.bunVM().waitForPromise(promise); result = promise.result(vm); result.ensureStillAlive(); @@ -4268,7 +4262,7 @@ pub const Expect = struct { .Rejected => { // TODO: rewrite this code to use .then() instead of blocking the event loop JSC.VirtualMachine.get().runErrorHandler(result, null); - globalObject.throw("Matcher `{s}` returned a promise that rejected", .{matcher_name}); + globalThis.throw("Matcher `{s}` returned a promise that rejected", .{matcher_name}); return false; }, } @@ -4280,12 +4274,12 @@ pub const Expect = struct { // Parse and validate the custom matcher result, which should conform to: { pass: boolean, message?: () => string } const is_valid = valid: { if (result.isObject()) { - if (result.get(globalObject, "pass")) |pass_value| { - pass = pass_value.toBooleanSlow(globalObject); - if (globalObject.hasException()) return false; + if (result.get(globalThis, "pass")) |pass_value| { + pass = pass_value.toBooleanSlow(globalThis); + if (globalThis.hasException()) return false; - if (result.get(globalObject, "message")) |message_value| { - if (!message_value.isString() and !message_value.isCallable(globalObject.vm())) { + if (result.get(globalThis, "message")) |message_value| { + if (!message_value.isString() and !message_value.isCallable(globalThis.vm())) { break :valid false; } message = message_value; @@ -4299,7 +4293,7 @@ pub const Expect = struct { break :valid false; }; if (!is_valid) { - throwInvalidMatcherError(globalObject, matcher_name, result); + throwInvalidMatcherError(globalThis, matcher_name, result); return false; } @@ -4312,28 +4306,28 @@ pub const Expect = struct { if (message.isUndefined()) { message_text = bun.String.static("No message was specified for this matcher."); } else if (message.isString()) { - message_text = message.toBunString(globalObject); + message_text = message.toBunString(globalThis); } else { if (comptime Environment.allow_assert) - assert(message.isCallable(globalObject.vm())); // checked above + assert(message.isCallable(globalThis.vm())); // checked above - var message_result = message.callWithGlobalThis(globalObject, &[_]JSValue{}); + var message_result = message.callWithGlobalThis(globalThis, &[_]JSValue{}); assert(!message_result.isEmpty()); if (message_result.toError()) |err| { - globalObject.throwValue(err); + globalThis.throwValue(err); return false; } - if (bun.String.tryFromJS(message_result, globalObject)) |str| { + if (bun.String.tryFromJS(message_result, globalThis)) |str| { message_text = str; } else { - if (globalObject.hasException()) return false; + if (globalThis.hasException()) return false; var formatter = JSC.ConsoleObject.Formatter{ - .globalThis = globalObject, + .globalThis = globalThis, .quote_strings = true, }; - globalObject.throw( + globalThis.throw( "Expected custom matcher message to return a string, but got: {}", - .{message_result.toFmt(globalObject, &formatter)}, + .{message_result.toFmt(globalThis, &formatter)}, ); return false; } @@ -4341,23 +4335,23 @@ pub const Expect = struct { const matcher_params = CustomMatcherParamsFormatter{ .colors = Output.enable_ansi_colors, - .globalObject = globalObject, + .globalThis = globalThis, .matcher_fn = matcher_fn, }; - throwPrettyMatcherError(globalObject, matcher_name, matcher_params, .{}, "{s}", .{message_text}); + throwPrettyMatcherError(globalThis, bun.String.empty, matcher_name, matcher_params, .{}, "{s}", .{message_text}); return false; } /// Function that is run for either `expect.myMatcher()` call or `expect().myMatcher` call, /// and we can known which case it is based on if the `callFrame.this()` value is an instance of Expect - pub fn applyCustomMatcher(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer globalObject.bunVM().autoGarbageCollect(); + pub fn applyCustomMatcher(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer globalThis.bunVM().autoGarbageCollect(); // retrieve the user-provided matcher function (matcher_fn) const func: JSValue = callFrame.callee(); - var matcher_fn = getCustomMatcherFn(func, globalObject) orelse JSValue.undefined; + var matcher_fn = getCustomMatcherFn(func, globalThis) orelse JSValue.undefined; if (!matcher_fn.jsType().isFunction()) { - globalObject.throw("Internal consistency error: failed to retrieve the matcher function for a custom matcher!", .{}); + globalThis.throw("Internal consistency error: failed to retrieve the matcher function for a custom matcher!", .{}); return .zero; } matcher_fn.ensureStillAlive(); @@ -4366,27 +4360,27 @@ pub const Expect = struct { const thisValue: JSValue = callFrame.this(); const expect: *Expect = Expect.fromJS(thisValue) orelse { // if no Expect instance, assume it is a static call (`expect.myMatcher()`), so create an ExpectCustomAsymmetricMatcher instance - return ExpectCustomAsymmetricMatcher.create(globalObject, callFrame, matcher_fn); + return ExpectCustomAsymmetricMatcher.create(globalThis, callFrame, matcher_fn); }; // if we got an Expect instance, then it's a non-static call (`expect().myMatcher`), // so now execute the symmetric matching // retrieve the matcher name - const matcher_name = matcher_fn.getName(globalObject); + const matcher_name = matcher_fn.getName(globalThis); const matcher_params = CustomMatcherParamsFormatter{ .colors = Output.enable_ansi_colors, - .globalObject = globalObject, + .globalThis = globalThis, .matcher_fn = matcher_fn, }; // retrieve the captured expected value var value = Expect.capturedValueGetCached(thisValue) orelse { - globalObject.throw("Internal consistency error: failed to retrieve the captured value", .{}); + globalThis.throw("Internal consistency error: failed to retrieve the captured value", .{}); return .zero; }; - value = Expect.processPromise(expect.flags, globalObject, value, matcher_name, matcher_params, false) orelse return .zero; + value = Expect.processPromise(expect.custom_label, expect.flags, globalThis, value, matcher_name, matcher_params, false) orelse return .zero; value.ensureStillAlive(); incrementExpectCallCounter(); @@ -4394,16 +4388,16 @@ pub const Expect = struct { // prepare the args array const args_ptr = callFrame.argumentsPtr(); const args_count = callFrame.argumentsCount(); - var allocator = std.heap.stackFallback(8 * @sizeOf(JSValue), globalObject.allocator()); + var allocator = std.heap.stackFallback(8 * @sizeOf(JSValue), globalThis.allocator()); var matcher_args = std.ArrayList(JSValue).initCapacity(allocator.get(), args_count + 1) catch { - globalObject.throwOutOfMemory(); + globalThis.throwOutOfMemory(); return .zero; }; matcher_args.appendAssumeCapacity(value); for (0..args_count) |i| matcher_args.appendAssumeCapacity(args_ptr[i]); // call the matcher, which will throw a js exception when failed - if (!executeCustomMatcher(globalObject, matcher_name, matcher_fn, matcher_args.items, expect.flags, false) or globalObject.hasException()) { + if (!executeCustomMatcher(globalThis, matcher_name, matcher_fn, matcher_args.items, expect.flags, false) or globalThis.hasException()) { return .zero; } @@ -4412,38 +4406,38 @@ pub const Expect = struct { pub const addSnapshotSerializer = notImplementedStaticFn; - pub fn hasAssertions(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn hasAssertions(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { _ = callFrame; - defer globalObject.bunVM().autoGarbageCollect(); + defer globalThis.bunVM().autoGarbageCollect(); is_expecting_assertions = true; return .undefined; } - pub fn assertions(globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - defer globalObject.bunVM().autoGarbageCollect(); + pub fn assertions(globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer globalThis.bunVM().autoGarbageCollect(); const arguments_ = callFrame.arguments(1); const arguments = arguments_.ptr[0..arguments_.len]; if (arguments.len < 1) { - globalObject.throwInvalidArguments("expect.assertions() takes 1 argument", .{}); + globalThis.throwInvalidArguments("expect.assertions() takes 1 argument", .{}); return .zero; } const expected: JSValue = arguments[0]; if (!expected.isNumber()) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalThis, &fmt)}); return .zero; } const expected_assertions: f64 = expected.asNumber(); if (@round(expected_assertions) != expected_assertions or std.math.isInf(expected_assertions) or std.math.isNan(expected_assertions) or expected_assertions < 0) { - var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true }; - globalObject.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalObject, &fmt)}); + var fmt = JSC.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true }; + globalThis.throw("Expected value must be a non-negative integer: {any}", .{expected.toFmt(globalThis, &fmt)}); return .zero; } @@ -4455,49 +4449,49 @@ pub const Expect = struct { return .undefined; } - pub fn notImplementedJSCFn(_: *Expect, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue { - globalObject.throw("Not implemented", .{}); + pub fn notImplementedJSCFn(_: *Expect, globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue { + globalThis.throw("Not implemented", .{}); return .zero; } - pub fn notImplementedStaticFn(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue { - globalObject.throw("Not implemented", .{}); + pub fn notImplementedStaticFn(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue { + globalThis.throw("Not implemented", .{}); return .zero; } - pub fn notImplementedJSCProp(_: *Expect, _: JSC.JSValue, globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { - globalObject.throw("Not implemented", .{}); + pub fn notImplementedJSCProp(_: *Expect, _: JSC.JSValue, globalThis: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + globalThis.throw("Not implemented", .{}); return .zero; } - pub fn notImplementedStaticProp(globalObject: *JSC.JSGlobalObject, _: JSC.JSValue, _: JSC.JSValue) callconv(.C) JSC.JSValue { - globalObject.throw("Not implemented", .{}); + pub fn notImplementedStaticProp(globalThis: *JSC.JSGlobalObject, _: JSC.JSValue, _: JSC.JSValue) callconv(.C) JSC.JSValue { + globalThis.throw("Not implemented", .{}); return .zero; } - pub fn postMatch(_: *Expect, globalObject: *JSC.JSGlobalObject) void { - var vm = globalObject.bunVM(); + pub fn postMatch(_: *Expect, globalThis: *JSC.JSGlobalObject) void { + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); } - pub fn doUnreachable(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn doUnreachable(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { const arg = callframe.arguments(1).ptr[0]; if (arg.isEmptyOrUndefinedOrNull()) { - const error_value = bun.String.init("reached unreachable code").toErrorInstance(globalObject); - error_value.put(globalObject, ZigString.static("name"), bun.String.init("UnreachableError").toJS(globalObject)); - globalObject.throwValue(error_value); + const error_value = bun.String.init("reached unreachable code").toErrorInstance(globalThis); + error_value.put(globalThis, ZigString.static("name"), bun.String.init("UnreachableError").toJS(globalThis)); + globalThis.throwValue(error_value); return .zero; } if (arg.isString()) { - const error_value = arg.toBunString(globalObject).toErrorInstance(globalObject); - error_value.put(globalObject, ZigString.static("name"), bun.String.init("UnreachableError").toJS(globalObject)); - globalObject.throwValue(error_value); + const error_value = arg.toBunString(globalThis).toErrorInstance(globalThis); + error_value.put(globalThis, ZigString.static("name"), bun.String.init("UnreachableError").toJS(globalThis)); + globalThis.throwValue(error_value); return .zero; } - globalObject.throwValue(arg); + globalThis.throwValue(arg); return .zero; } }; @@ -4515,55 +4509,55 @@ pub const ExpectStatic = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn create(globalObject: *JSC.JSGlobalObject, flags: Expect.Flags) JSValue { - var expect = globalObject.bunVM().allocator.create(ExpectStatic) catch { - globalObject.throwOutOfMemory(); + pub fn create(globalThis: *JSC.JSGlobalObject, flags: Expect.Flags) JSValue { + var expect = globalThis.bunVM().allocator.create(ExpectStatic) catch { + globalThis.throwOutOfMemory(); return .zero; }; expect.flags = flags; - const value = expect.toJS(globalObject); + const value = expect.toJS(globalThis); value.ensureStillAlive(); return value; } - pub fn getNot(this: *ExpectStatic, _: JSValue, globalObject: *JSGlobalObject) callconv(.C) JSValue { + pub fn getNot(this: *ExpectStatic, _: JSValue, globalThis: *JSGlobalObject) callconv(.C) JSValue { var flags = this.flags; flags.not = !this.flags.not; - return create(globalObject, flags); + return create(globalThis, flags); } - pub fn getResolvesTo(this: *ExpectStatic, _: JSValue, globalObject: *JSGlobalObject) callconv(.C) JSValue { + pub fn getResolvesTo(this: *ExpectStatic, _: JSValue, globalThis: *JSGlobalObject) callconv(.C) JSValue { var flags = this.flags; - if (flags.promise != .none) return asyncChainingError(globalObject, flags, "resolvesTo"); + if (flags.promise != .none) return asyncChainingError(globalThis, flags, "resolvesTo"); flags.promise = .resolves; - return create(globalObject, flags); + return create(globalThis, flags); } - pub fn getRejectsTo(this: *ExpectStatic, _: JSValue, globalObject: *JSGlobalObject) callconv(.C) JSValue { + pub fn getRejectsTo(this: *ExpectStatic, _: JSValue, globalThis: *JSGlobalObject) callconv(.C) JSValue { var flags = this.flags; - if (flags.promise != .none) return asyncChainingError(globalObject, flags, "rejectsTo"); + if (flags.promise != .none) return asyncChainingError(globalThis, flags, "rejectsTo"); flags.promise = .rejects; - return create(globalObject, flags); + return create(globalThis, flags); } - fn asyncChainingError(globalObject: *JSGlobalObject, flags: Expect.Flags, name: string) JSValue { + fn asyncChainingError(globalThis: *JSGlobalObject, flags: Expect.Flags, name: string) JSValue { @setCold(true); const str = switch (flags.promise) { .resolves => "resolvesTo", .rejects => "rejectsTo", else => unreachable, }; - globalObject.throw("expect.{s}: already called expect.{s} on this chain", .{ name, str }); + globalThis.throw("expect.{s}: already called expect.{s} on this chain", .{ name, str }); return .zero; } - fn createAsymmetricMatcherWithFlags(T: anytype, this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { + fn createAsymmetricMatcherWithFlags(T: anytype, this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) JSValue { //const this: *ExpectStatic = ExpectStatic.fromJS(callFrame.this()); - const instance_jsvalue = T.call(globalObject, callFrame); + const instance_jsvalue = T.call(globalThis, callFrame); if (!instance_jsvalue.isEmpty() and !instance_jsvalue.isAnyError()) { var instance = T.fromJS(instance_jsvalue) orelse { - globalObject.throwOutOfMemory(); + globalThis.throwOutOfMemory(); return .zero; }; instance.flags = this.flags; @@ -4571,32 +4565,32 @@ pub const ExpectStatic = struct { return instance_jsvalue; } - pub fn anything(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectAnything, this, globalObject, callFrame); + pub fn anything(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectAnything, this, globalThis, callFrame); } - pub fn any(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectAny, this, globalObject, callFrame); + pub fn any(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectAny, this, globalThis, callFrame); } - pub fn arrayContaining(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectArrayContaining, this, globalObject, callFrame); + pub fn arrayContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectArrayContaining, this, globalThis, callFrame); } - pub fn closeTo(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectCloseTo, this, globalObject, callFrame); + pub fn closeTo(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectCloseTo, this, globalThis, callFrame); } - pub fn objectContaining(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectObjectContaining, this, globalObject, callFrame); + pub fn objectContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectObjectContaining, this, globalThis, callFrame); } - pub fn stringContaining(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectStringContaining, this, globalObject, callFrame); + pub fn stringContaining(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectStringContaining, this, globalThis, callFrame); } - pub fn stringMatching(this: *ExpectStatic, globalObject: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { - return createAsymmetricMatcherWithFlags(ExpectStringMatching, this, globalObject, callFrame); + pub fn stringMatching(this: *ExpectStatic, globalThis: *JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + return createAsymmetricMatcherWithFlags(ExpectStringMatching, this, globalThis, callFrame); } }; @@ -4611,17 +4605,17 @@ pub const ExpectAnything = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSValue { - const anything = globalObject.bunVM().allocator.create(ExpectAnything) catch { - globalObject.throwOutOfMemory(); + pub fn call(globalThis: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSValue { + const anything = globalThis.bunVM().allocator.create(ExpectAnything) catch { + globalThis.throwOutOfMemory(); return .zero; }; anything.* = .{}; - const anything_js_value = anything.toJS(globalObject); + const anything_js_value = anything.toJS(globalThis); anything_js_value.ensureStillAlive(); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return anything_js_value; @@ -4639,27 +4633,27 @@ pub const ExpectStringMatching = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or (!args[0].isString() and !args[0].isRegExp())) { const fmt = "expect.stringContaining(string)\n\nExpected a string or regular expression\n"; - globalObject.throwPretty(fmt, .{}); + globalThis.throwPretty(fmt, .{}); return .zero; } const test_value = args[0]; - const string_matching = globalObject.bunVM().allocator.create(ExpectStringMatching) catch { - globalObject.throwOutOfMemory(); + const string_matching = globalThis.bunVM().allocator.create(ExpectStringMatching) catch { + globalThis.throwOutOfMemory(); return .zero; }; string_matching.* = .{}; - const string_matching_js_value = string_matching.toJS(globalObject); - ExpectStringMatching.testValueSetCached(string_matching_js_value, globalObject, test_value); + const string_matching_js_value = string_matching.toJS(globalThis); + ExpectStringMatching.testValueSetCached(string_matching_js_value, globalThis, test_value); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return string_matching_js_value; } @@ -4676,11 +4670,11 @@ pub const ExpectCloseTo = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(2).slice(); if (args.len == 0 or !args[0].isNumber()) { - globalObject.throwPretty("expect.closeTo(number, precision?)\n\nExpected a number value", .{}); + globalThis.throwPretty("expect.closeTo(number, precision?)\n\nExpected a number value", .{}); return .zero; } const number_value = args[0]; @@ -4690,23 +4684,23 @@ pub const ExpectCloseTo = struct { precision_value = JSValue.jsNumberFromInt32(2); // default value from jest } if (!precision_value.isNumber()) { - globalObject.throwPretty("expect.closeTo(number, precision?)\n\nPrecision must be a number or undefined", .{}); + globalThis.throwPretty("expect.closeTo(number, precision?)\n\nPrecision must be a number or undefined", .{}); return .zero; } - const instance = globalObject.bunVM().allocator.create(ExpectCloseTo) catch { - globalObject.throwOutOfMemory(); + const instance = globalThis.bunVM().allocator.create(ExpectCloseTo) catch { + globalThis.throwOutOfMemory(); return .zero; }; instance.* = .{}; - const instance_jsvalue = instance.toJS(globalObject); + const instance_jsvalue = instance.toJS(globalThis); number_value.ensureStillAlive(); precision_value.ensureStillAlive(); - ExpectCloseTo.numberValueSetCached(instance_jsvalue, globalObject, number_value); - ExpectCloseTo.digitsValueSetCached(instance_jsvalue, globalObject, precision_value); + ExpectCloseTo.numberValueSetCached(instance_jsvalue, globalThis, number_value); + ExpectCloseTo.digitsValueSetCached(instance_jsvalue, globalThis, precision_value); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return instance_jsvalue; } @@ -4723,27 +4717,27 @@ pub const ExpectObjectContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isObject()) { const fmt = "expect.objectContaining(object)\n\nExpected an object\n"; - globalObject.throwPretty(fmt, .{}); + globalThis.throwPretty(fmt, .{}); return .zero; } const object_value = args[0]; - const instance = globalObject.bunVM().allocator.create(ExpectObjectContaining) catch { - globalObject.throwOutOfMemory(); + const instance = globalThis.bunVM().allocator.create(ExpectObjectContaining) catch { + globalThis.throwOutOfMemory(); return .zero; }; instance.* = .{}; - const instance_jsvalue = instance.toJS(globalObject); - ExpectObjectContaining.objectValueSetCached(instance_jsvalue, globalObject, object_value); + const instance_jsvalue = instance.toJS(globalThis); + ExpectObjectContaining.objectValueSetCached(instance_jsvalue, globalThis, object_value); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return instance_jsvalue; } @@ -4760,27 +4754,27 @@ pub const ExpectStringContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].isString()) { const fmt = "expect.stringContaining(string)\n\nExpected a string\n"; - globalObject.throwPretty(fmt, .{}); + globalThis.throwPretty(fmt, .{}); return .zero; } const string_value = args[0]; - const string_containing = globalObject.bunVM().allocator.create(ExpectStringContaining) catch { - globalObject.throwOutOfMemory(); + const string_containing = globalThis.bunVM().allocator.create(ExpectStringContaining) catch { + globalThis.throwOutOfMemory(); return .zero; }; string_containing.* = .{}; - const string_containing_js_value = string_containing.toJS(globalObject); - ExpectStringContaining.stringValueSetCached(string_containing_js_value, globalObject, string_value); + const string_containing_js_value = string_containing.toJS(globalThis); + ExpectStringContaining.stringValueSetCached(string_containing_js_value, globalThis, string_value); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return string_containing_js_value; } @@ -4797,12 +4791,12 @@ pub const ExpectAny = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue { const _arguments = callFrame.arguments(1); const arguments: []const JSValue = _arguments.ptr[0.._arguments.len]; if (arguments.len == 0) { - globalObject.throw("any() expects to be passed a constructor function. Please pass one or use anything() to match any object.", .{}); + globalThis.throw("any() expects to be passed a constructor function. Please pass one or use anything() to match any object.", .{}); return .zero; } @@ -4810,22 +4804,22 @@ pub const ExpectAny = struct { constructor.ensureStillAlive(); if (!constructor.isConstructor()) { const fmt = "expect.any(constructor)\n\nExpected a constructor\n"; - globalObject.throwPretty(fmt, .{}); + globalThis.throwPretty(fmt, .{}); return .zero; } - var any = globalObject.bunVM().allocator.create(ExpectAny) catch { - globalObject.throwOutOfMemory(); + var any = globalThis.bunVM().allocator.create(ExpectAny) catch { + globalThis.throwOutOfMemory(); return .zero; }; any.* = .{}; - const any_js_value = any.toJS(globalObject); + const any_js_value = any.toJS(globalThis); any_js_value.ensureStillAlive(); - ExpectAny.constructorValueSetCached(any_js_value, globalObject, constructor); + ExpectAny.constructorValueSetCached(any_js_value, globalThis, constructor); any_js_value.ensureStillAlive(); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return any_js_value; @@ -4843,27 +4837,27 @@ pub const ExpectArrayContaining = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn call(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + pub fn call(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { const args = callFrame.arguments(1).slice(); if (args.len == 0 or !args[0].jsType().isArray()) { const fmt = "expect.arrayContaining(array)\n\nExpected a array\n"; - globalObject.throwPretty(fmt, .{}); + globalThis.throwPretty(fmt, .{}); return .zero; } const array_value = args[0]; - const array_containing = globalObject.bunVM().allocator.create(ExpectArrayContaining) catch { - globalObject.throwOutOfMemory(); + const array_containing = globalThis.bunVM().allocator.create(ExpectArrayContaining) catch { + globalThis.throwOutOfMemory(); return .zero; }; array_containing.* = .{}; - const array_containing_js_value = array_containing.toJS(globalObject); - ExpectArrayContaining.arrayValueSetCached(array_containing_js_value, globalObject, array_value); + const array_containing_js_value = array_containing.toJS(globalThis); + ExpectArrayContaining.arrayValueSetCached(array_containing_js_value, globalThis, array_value); - var vm = globalObject.bunVM(); + var vm = globalThis.bunVM(); vm.autoGarbageCollect(); return array_containing_js_value; } @@ -4887,7 +4881,7 @@ pub const ExpectCustomAsymmetricMatcher = struct { /// Implements the static call of the custom matcher (`expect.myCustomMatcher()`), /// which creates an asymmetric matcher instance (`ExpectCustomAsymmetricMatcher`). /// This will not run the matcher, but just capture the args etc. - pub fn create(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, matcher_fn: JSValue) callconv(.C) JSValue { + pub fn create(globalThis: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame, matcher_fn: JSValue) callconv(.C) JSValue { var flags: Expect.Flags = undefined; // try to retrieve the ExpectStatic instance (to get the flags) @@ -4899,126 +4893,126 @@ pub const ExpectCustomAsymmetricMatcher = struct { } // create the matcher instance - const instance = globalObject.bunVM().allocator.create(ExpectCustomAsymmetricMatcher) catch { - globalObject.throwOutOfMemory(); + const instance = globalThis.bunVM().allocator.create(ExpectCustomAsymmetricMatcher) catch { + globalThis.throwOutOfMemory(); return .zero; }; instance.* = .{}; - const instance_jsvalue = instance.toJS(globalObject); + const instance_jsvalue = instance.toJS(globalThis); instance_jsvalue.ensureStillAlive(); // store the flags instance.flags = flags; // store the user-provided matcher function into the instance - ExpectCustomAsymmetricMatcher.matcherFnSetCached(instance_jsvalue, globalObject, matcher_fn); + ExpectCustomAsymmetricMatcher.matcherFnSetCached(instance_jsvalue, globalThis, matcher_fn); // capture the args as a JS array saved in the instance, so the matcher can be executed later on with them const args_ptr = callFrame.argumentsPtr(); const args_count: usize = callFrame.argumentsCount(); - var args = JSValue.createEmptyArray(globalObject, args_count); + var args = JSValue.createEmptyArray(globalThis, args_count); for (0..args_count) |i| { - args.putIndex(globalObject, @truncate(i), args_ptr[i]); + args.putIndex(globalThis, @truncate(i), args_ptr[i]); } args.ensureStillAlive(); - ExpectCustomAsymmetricMatcher.capturedArgsSetCached(instance_jsvalue, globalObject, args); + ExpectCustomAsymmetricMatcher.capturedArgsSetCached(instance_jsvalue, globalThis, args); // return the same instance, now fully initialized including the captured args (previously it was incomplete) return instance_jsvalue; } /// Function called by c++ function "matchAsymmetricMatcher" to execute the custom matcher against the provided leftValue - pub fn execute(this: *ExpectCustomAsymmetricMatcher, thisValue: JSValue, globalObject: *JSC.JSGlobalObject, received: JSValue) callconv(.C) bool { + pub fn execute(this: *ExpectCustomAsymmetricMatcher, thisValue: JSValue, globalThis: *JSC.JSGlobalObject, received: JSValue) callconv(.C) bool { // retrieve the user-provided matcher implementation function (the function passed to expect.extend({ ... })) const matcher_fn: JSValue = ExpectCustomAsymmetricMatcher.matcherFnGetCached(thisValue) orelse { - globalObject.throw("Internal consistency error: the ExpectCustomAsymmetricMatcher(matcherFn) was garbage collected but it should not have been!", .{}); + globalThis.throw("Internal consistency error: the ExpectCustomAsymmetricMatcher(matcherFn) was garbage collected but it should not have been!", .{}); return false; }; matcher_fn.ensureStillAlive(); if (!matcher_fn.jsType().isFunction()) { - globalObject.throw("Internal consistency error: the ExpectCustomMatcher(matcherFn) is not a function!", .{}); + globalThis.throw("Internal consistency error: the ExpectCustomMatcher(matcherFn) is not a function!", .{}); return false; } // retrieve the matcher name - const matcher_name = matcher_fn.getName(globalObject); + const matcher_name = matcher_fn.getName(globalThis); // retrieve the asymmetric matcher args // if null, it means the function has not yet been called to capture the args, which is a misuse of the matcher const captured_args: JSValue = ExpectCustomAsymmetricMatcher.capturedArgsGetCached(thisValue) orelse { - globalObject.throw("expect.{s} misused, it needs to be instantiated by calling it with 0 or more arguments", .{matcher_name}); + globalThis.throw("expect.{s} misused, it needs to be instantiated by calling it with 0 or more arguments", .{matcher_name}); return false; }; captured_args.ensureStillAlive(); // prepare the args array as `[received, ...captured_args]` - const args_count = captured_args.getLength(globalObject); - var allocator = std.heap.stackFallback(8 * @sizeOf(JSValue), globalObject.allocator()); + const args_count = captured_args.getLength(globalThis); + var allocator = std.heap.stackFallback(8 * @sizeOf(JSValue), globalThis.allocator()); var matcher_args = std.ArrayList(JSValue).initCapacity(allocator.get(), args_count + 1) catch { - globalObject.throwOutOfMemory(); + globalThis.throwOutOfMemory(); return false; }; matcher_args.appendAssumeCapacity(received); for (0..args_count) |i| { - matcher_args.appendAssumeCapacity(captured_args.getIndex(globalObject, @truncate(i))); + matcher_args.appendAssumeCapacity(captured_args.getIndex(globalThis, @truncate(i))); } - return Expect.executeCustomMatcher(globalObject, matcher_name, matcher_fn, matcher_args.items, this.flags, true); + return Expect.executeCustomMatcher(globalThis, matcher_name, matcher_fn, matcher_args.items, this.flags, true); } - pub fn asymmetricMatch(this: *ExpectCustomAsymmetricMatcher, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn asymmetricMatch(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { const arguments = callframe.arguments(1).slice(); const received_value = if (arguments.len < 1) JSC.JSValue.jsUndefined() else arguments[0]; - const matched = execute(this, callframe.this(), globalObject, received_value); + const matched = execute(this, callframe.this(), globalThis, received_value); return JSValue.jsBoolean(matched); } /// Calls a custom implementation (if provided) to stringify this asymmetric matcher, and returns true if it was provided and it succeed - pub fn customPrint(_: *ExpectCustomAsymmetricMatcher, thisValue: JSValue, globalObject: *JSC.JSGlobalObject, writer: anytype, comptime dontThrow: bool) !bool { + pub fn customPrint(_: *ExpectCustomAsymmetricMatcher, thisValue: JSValue, globalThis: *JSC.JSGlobalObject, writer: anytype, comptime dontThrow: bool) !bool { const matcher_fn: JSValue = ExpectCustomAsymmetricMatcher.matcherFnGetCached(thisValue) orelse return false; - if (matcher_fn.get(globalObject, "toAsymmetricMatcher")) |fn_value| { + if (matcher_fn.get(globalThis, "toAsymmetricMatcher")) |fn_value| { if (fn_value.jsType().isFunction()) { const captured_args: JSValue = ExpectCustomAsymmetricMatcher.capturedArgsGetCached(thisValue) orelse return false; - var stack_fallback = std.heap.stackFallback(256, globalObject.allocator()); - const args_len = captured_args.getLength(globalObject); + var stack_fallback = std.heap.stackFallback(256, globalThis.allocator()); + const args_len = captured_args.getLength(globalThis); var args = try std.ArrayList(JSValue).initCapacity(stack_fallback.get(), args_len); - var iter = captured_args.arrayIterator(globalObject); + var iter = captured_args.arrayIterator(globalThis); while (iter.next()) |arg| { args.appendAssumeCapacity(arg); } - var result = matcher_fn.callWithThis(globalObject, thisValue, args.items); + var result = matcher_fn.callWithThis(globalThis, thisValue, args.items); if (result.toError()) |err| { if (dontThrow) { return false; } else { - globalObject.throwValue(globalObject, err); + globalThis.throwValue(globalThis, err); return error.JSError; } } - try writer.print("{}", .{result.toBunString(globalObject)}); + try writer.print("{}", .{result.toBunString(globalThis)}); } } return false; } - pub fn toAsymmetricMatcher(this: *ExpectCustomAsymmetricMatcher, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { - var stack_fallback = std.heap.stackFallback(512, globalObject.allocator()); + pub fn toAsymmetricMatcher(this: *ExpectCustomAsymmetricMatcher, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + var stack_fallback = std.heap.stackFallback(512, globalThis.allocator()); var mutable_string = bun.MutableString.init2048(stack_fallback.get()) catch { - globalObject.throwOutOfMemory(); + globalThis.throwOutOfMemory(); return .zero; }; defer mutable_string.deinit(); - const printed = customPrint(this, callframe.this(), globalObject, mutable_string.writer()) catch { - globalObject.throwOutOfMemory(); + const printed = customPrint(this, callframe.this(), globalThis, mutable_string.writer()) catch { + globalThis.throwOutOfMemory(); return .zero; }; if (printed) { return bun.String.init(mutable_string.toOwnedSliceLeaky()).toJS(); } - return ExpectMatcherUtils.printValue(globalObject, this, null); + return ExpectMatcherUtils.printValue(globalThis, this, null); } }; @@ -5034,36 +5028,36 @@ pub const ExpectMatcherContext = struct { VirtualMachine.get().allocator.destroy(this); } - pub fn getUtils(_: *ExpectMatcherContext, globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { - return ExpectMatcherUtils__getSingleton(globalObject); + pub fn getUtils(_: *ExpectMatcherContext, globalThis: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + return ExpectMatcherUtils__getSingleton(globalThis); } pub fn getIsNot(this: *ExpectMatcherContext, _: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { return JSValue.jsBoolean(this.flags.not); } - pub fn getPromise(this: *ExpectMatcherContext, globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + pub fn getPromise(this: *ExpectMatcherContext, globalThis: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { return switch (this.flags.promise) { - .rejects => bun.String.static("rejects").toJS(globalObject), - .resolves => bun.String.static("resolves").toJS(globalObject), - else => bun.String.empty.toJS(globalObject), + .rejects => bun.String.static("rejects").toJS(globalThis), + .resolves => bun.String.static("resolves").toJS(globalThis), + else => bun.String.empty.toJS(globalThis), }; } - pub fn getExpand(_: *ExpectMatcherContext, globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { - _ = globalObject; + pub fn getExpand(_: *ExpectMatcherContext, globalThis: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue { + _ = globalThis; // TODO: this should return whether running tests in verbose mode or not (jest flag --expand), but bun currently doesn't have this switch return JSValue.false; } - pub fn equals(_: *ExpectMatcherContext, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { + pub fn equals(_: *ExpectMatcherContext, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue { var arguments = callframe.arguments(3); if (arguments.len < 2) { - globalObject.throw("expect.extends matcher: this.util.equals expects at least 2 arguments", .{}); + globalThis.throw("expect.extends matcher: this.util.equals expects at least 2 arguments", .{}); return .zero; } const args = arguments.slice(); - return JSValue.jsBoolean(args[0].jestDeepEquals(args[1], globalObject)); + return JSValue.jsBoolean(args[0].jestDeepEquals(args[1], globalThis)); } }; @@ -5071,12 +5065,12 @@ pub const ExpectMatcherContext = struct { pub const ExpectMatcherUtils = struct { pub usingnamespace JSC.Codegen.JSExpectMatcherUtils; - fn createSingleton(globalObject: *JSC.JSGlobalObject) callconv(.C) JSValue { - var instance = globalObject.bunVM().allocator.create(ExpectMatcherUtils) catch { - globalObject.throwOutOfMemory(); + fn createSingleton(globalThis: *JSC.JSGlobalObject) callconv(.C) JSValue { + var instance = globalThis.bunVM().allocator.create(ExpectMatcherUtils) catch { + globalThis.throwOutOfMemory(); return .zero; }; - return instance.toJS(globalObject); + return instance.toJS(globalThis); } pub fn finalize( @@ -5085,8 +5079,8 @@ pub const ExpectMatcherUtils = struct { VirtualMachine.get().allocator.destroy(this); } - fn printValue(globalObject: *JSC.JSGlobalObject, value: JSValue, comptime color_or_null: ?[]const u8) !JSValue { - var stack_fallback = std.heap.stackFallback(512, globalObject.allocator()); + fn printValue(globalThis: *JSC.JSGlobalObject, value: JSValue, comptime color_or_null: ?[]const u8) !JSValue { + var stack_fallback = std.heap.stackFallback(512, globalThis.allocator()); var mutable_string = try bun.MutableString.init2048(stack_fallback.get()); defer mutable_string.deinit(); @@ -5100,10 +5094,10 @@ pub const ExpectMatcherUtils = struct { } var formatter = JSC.ConsoleObject.Formatter{ - .globalThis = globalObject, + .globalThis = globalThis, .quote_strings = true, }; - try writer.print("{}", .{value.toFmt(globalObject, &formatter)}); + try writer.print("{}", .{value.toFmt(globalThis, &formatter)}); if (comptime color_or_null) |_| { if (Output.enable_ansi_colors) { @@ -5113,46 +5107,46 @@ pub const ExpectMatcherUtils = struct { try buffered_writer.flush(); - return bun.String.createUTF8(mutable_string.toOwnedSlice()).toJS(globalObject); + return bun.String.createUTF8(mutable_string.toOwnedSlice()).toJS(globalThis); } - inline fn printValueCatched(globalObject: *JSC.JSGlobalObject, value: JSValue, comptime color_or_null: ?[]const u8) JSValue { - return printValue(globalObject, value, color_or_null) catch { - globalObject.throwOutOfMemory(); + inline fn printValueCatched(globalThis: *JSC.JSGlobalObject, value: JSValue, comptime color_or_null: ?[]const u8) JSValue { + return printValue(globalThis, value, color_or_null) catch { + globalThis.throwOutOfMemory(); return .zero; }; } - pub fn stringify(_: *ExpectMatcherUtils, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { + pub fn stringify(_: *ExpectMatcherUtils, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSC.JSValue.jsUndefined() else arguments[0]; - return printValueCatched(globalObject, value, null); + return printValueCatched(globalThis, value, null); } - pub fn printExpected(_: *ExpectMatcherUtils, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { + pub fn printExpected(_: *ExpectMatcherUtils, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSC.JSValue.jsUndefined() else arguments[0]; - return printValueCatched(globalObject, value, ""); + return printValueCatched(globalThis, value, ""); } - pub fn printReceived(_: *ExpectMatcherUtils, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { + pub fn printReceived(_: *ExpectMatcherUtils, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { const arguments = callframe.arguments(1).slice(); const value = if (arguments.len < 1) JSC.JSValue.jsUndefined() else arguments[0]; - return printValueCatched(globalObject, value, ""); + return printValueCatched(globalThis, value, ""); } - pub fn matcherHint(_: *ExpectMatcherUtils, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { + pub fn matcherHint(_: *ExpectMatcherUtils, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { const arguments = callframe.arguments(4).slice(); if (arguments.len == 0 or !arguments[0].isString()) { - globalObject.throw("matcherHint: the first argument (matcher name) must be a string", .{}); + globalThis.throw("matcherHint: the first argument (matcher name) must be a string", .{}); return .zero; } - const matcher_name = arguments[0].toBunString(globalObject); + const matcher_name = arguments[0].toBunString(globalThis); defer matcher_name.deref(); - const received = if (arguments.len > 1) arguments[1] else bun.String.static("received").toJS(globalObject); - const expected = if (arguments.len > 2) arguments[2] else bun.String.static("expected").toJS(globalObject); + const received = if (arguments.len > 1) arguments[1] else bun.String.static("received").toJS(globalThis); + const expected = if (arguments.len > 2) arguments[2] else bun.String.static("expected").toJS(globalThis); const options = if (arguments.len > 3) arguments[3] else JSValue.jsUndefined(); var is_not = false; @@ -5166,42 +5160,42 @@ pub const ExpectMatcherUtils = struct { if (!options.isUndefinedOrNull()) { if (!options.isObject()) { - globalObject.throw("matcherHint: options must be an object (or undefined)", .{}); + globalThis.throw("matcherHint: options must be an object (or undefined)", .{}); return .zero; } - if (options.get(globalObject, "isNot")) |val| { - is_not = val.coerce(bool, globalObject); + if (options.get(globalThis, "isNot")) |val| { + is_not = val.coerce(bool, globalThis); } - if (options.get(globalObject, "comment")) |val| { - comment = val.toStringOrNull(globalObject); + if (options.get(globalThis, "comment")) |val| { + comment = val.toStringOrNull(globalThis); } - if (options.get(globalObject, "promise")) |val| { - promise = val.toStringOrNull(globalObject); + if (options.get(globalThis, "promise")) |val| { + promise = val.toStringOrNull(globalThis); } - if (options.get(globalObject, "secondArgument")) |val| { - second_argument = val.toStringOrNull(globalObject); + if (options.get(globalThis, "secondArgument")) |val| { + second_argument = val.toStringOrNull(globalThis); } } const diff_formatter = DiffFormatter{ .received = received, .expected = expected, - .globalObject = globalObject, + .globalThis = globalThis, .not = is_not, }; if (is_not) { const signature = comptime Expect.getSignature("{s}", "expected", true); const fmt = signature ++ "\n\n{any}\n"; - return JSValue.printStringPretty(globalObject, 2048, fmt, .{ matcher_name, diff_formatter }) catch { - globalObject.throwOutOfMemory(); + return JSValue.printStringPretty(globalThis, 2048, fmt, .{ matcher_name, diff_formatter }) catch { + globalThis.throwOutOfMemory(); return .zero; }; } else { const signature = comptime Expect.getSignature("{s}", "expected", false); const fmt = signature ++ "\n\n{any}\n"; - return JSValue.printStringPretty(globalObject, 2048, fmt, .{ matcher_name, diff_formatter }) catch { - globalObject.throwOutOfMemory(); + return JSValue.printStringPretty(globalThis, 2048, fmt, .{ matcher_name, diff_formatter }) catch { + globalThis.throwOutOfMemory(); return .zero; }; } @@ -5209,8 +5203,8 @@ pub const ExpectMatcherUtils = struct { }; // Extract the matcher_fn from a JSCustomExpectMatcherFunction instance -inline fn getCustomMatcherFn(thisValue: JSValue, globalObject: *JSGlobalObject) ?JSValue { - var matcher_fn = Bun__JSWrappingFunction__getWrappedFunction(thisValue, globalObject); +inline fn getCustomMatcherFn(thisValue: JSValue, globalThis: *JSGlobalObject) ?JSValue { + var matcher_fn = Bun__JSWrappingFunction__getWrappedFunction(thisValue, globalThis); return if (matcher_fn.isEmpty()) null else matcher_fn; } @@ -5222,13 +5216,13 @@ extern fn JSMockFunction__getCalls(JSValue) JSValue; /// If there were no calls, it returns an empty JSArray* extern fn JSMockFunction__getReturns(JSValue) JSValue; -extern fn Bun__JSWrappingFunction__create(globalObject: *JSC.JSGlobalObject, symbolName: *const bun.String, functionPointer: JSC.JSHostFunctionPtr, wrappedFn: JSValue, strong: bool) JSValue; -extern fn Bun__JSWrappingFunction__getWrappedFunction(this: JSC.JSValue, globalObject: *JSC.JSGlobalObject) JSValue; +extern fn Bun__JSWrappingFunction__create(globalThis: *JSC.JSGlobalObject, symbolName: *const bun.String, functionPointer: JSC.JSHostFunctionPtr, wrappedFn: JSValue, strong: bool) JSValue; +extern fn Bun__JSWrappingFunction__getWrappedFunction(this: JSC.JSValue, globalThis: *JSC.JSGlobalObject) JSValue; -extern fn ExpectMatcherUtils__getSingleton(globalObject: *JSC.JSGlobalObject) JSC.JSValue; +extern fn ExpectMatcherUtils__getSingleton(globalThis: *JSC.JSGlobalObject) JSC.JSValue; -extern fn Expect__getPrototype(globalObject: *JSC.JSGlobalObject) JSC.JSValue; -extern fn ExpectStatic__getPrototype(globalObject: *JSC.JSGlobalObject) JSC.JSValue; +extern fn Expect__getPrototype(globalThis: *JSC.JSGlobalObject) JSC.JSValue; +extern fn ExpectStatic__getPrototype(globalThis: *JSC.JSGlobalObject) JSC.JSValue; comptime { @export(ExpectMatcherUtils.createSingleton, .{ .name = "ExpectMatcherUtils_createSigleton" }); diff --git a/test/js/bun/test/expect-label.test.ts b/test/js/bun/test/expect-label.test.ts new file mode 100644 index 00000000000000..e8742e98c6d0d2 --- /dev/null +++ b/test/js/bun/test/expect-label.test.ts @@ -0,0 +1,42 @@ +import { test, expect } from "bun:test"; +import stripAnsiColors from "strip-ansi"; + +test("expect-label", () => { + let err; + try { + expect("a", "lol!").toBe("b"); + expect.unreachable(); + } catch (e) { + err = e; + } + + expect(stripAnsiColors(err.message)).toContain("lol!\n\nExpected"); + expect(stripAnsiColors(err.message)).not.toContain("to be"); + expect(stripAnsiColors(err.message)).not.toContain("toBe"); +}); + +test("expect-label toEqual", () => { + let err; + try { + expect("a", "lol!").toEqual("b"); + expect.unreachable(); + } catch (e) { + err = e; + } + + expect(stripAnsiColors(err.message)).toContain("lol!\n\nExpected"); +}); + +test("non-strings do not crash", () => { + try { + expect("a", undefined).toEqual("b"); + } catch {} + try { + // @ts-ignore + expect("a", Symbol("a")).toEqual("b"); + } catch {} + try { + // @ts-ignore + expect("a", null).toEqual("b"); + } catch {} +});