Skip to content

Commit

Permalink
Implement expect(value, customMessage?: string) (#11624)
Browse files Browse the repository at this point in the history
Co-authored-by: dave caruso <[email protected]>
  • Loading branch information
Jarred-Sumner and paperclover authored Jun 6, 2024
1 parent e5ff1fd commit fe7b040
Show file tree
Hide file tree
Showing 6 changed files with 1,126 additions and 1,087 deletions.
7 changes: 6 additions & 1 deletion packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,12 @@ declare module "bun:test" {

export interface Expect extends AsymmetricMatchers {
// the `expect()` callable signature
<T = unknown>(actual?: T): Matchers<T>;
/**
* @param actual the actual value
* @param customFailMessage an optional custom message to display if the test fails.
* */

<T = unknown>(actual?: T, customFailMessage?: string): Matchers<T>;

/**
* Access to negated asymmetric matchers.
Expand Down
16 changes: 4 additions & 12 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand Down
18 changes: 9 additions & 9 deletions src/bun.js/test/diff_format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -104,7 +104,7 @@ pub const DiffFormatter = struct {
};
ConsoleObject.format2(
.Debug,
this.globalObject,
this.globalThis,
@as([*]const JSValue, @ptrCast(&received)),
1,
Writer,
Expand All @@ -118,7 +118,7 @@ pub const DiffFormatter = struct {

ConsoleObject.format2(
.Debug,
this.globalObject,
this.globalThis,
@as([*]const JSValue, @ptrCast(&this.expected)),
1,
Writer,
Expand All @@ -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: <green>{any}<r>\nReceived: <red>{any}<r>";
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;
},
Expand Down
Loading

0 comments on commit fe7b040

Please sign in to comment.