Skip to content

Commit 0b911e6

Browse files
Claude Botclaude
andcommitted
fix: expect().resolves and expect().rejects now return promises
Previously, matchers under `expect().resolves` and `expect().rejects` would return `undefined` instead of a promise. This caused issues when trying to chain or await matcher results. This fix adds a helper function `returnMatcherValue()` that checks if async flags (.resolves/.rejects) are set and returns a resolved promise instead of undefined. All matchers now use this helper. Fixes #23420 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6e3359d commit 0b911e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+138
-83
lines changed

src/bun.js/test/expect.zig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ pub const Expect = struct {
848848
if (existing_value) |saved_value| {
849849
if (strings.eqlLong(pretty_value.slice(), saved_value, true)) {
850850
Jest.runner.?.snapshots.passed += 1;
851-
return .js_undefined;
851+
return this.returnMatcherValue(globalThis);
852852
}
853853

854854
Jest.runner.?.snapshots.failed += 1;
@@ -863,7 +863,7 @@ pub const Expect = struct {
863863
return globalThis.throwPretty(fmt, .{diff_format});
864864
}
865865

866-
return .js_undefined;
866+
return this.returnMatcherValue(globalThis);
867867
}
868868

869869
pub fn getStaticNot(globalThis: *JSGlobalObject, _: JSValue, _: JSValue) bun.JSError!JSValue {
@@ -1231,6 +1231,16 @@ pub const Expect = struct {
12311231
vm.autoGarbageCollect();
12321232
}
12331233

1234+
/// Helper function for matchers to return the correct value based on whether
1235+
/// .resolves or .rejects was used. When async flags are set, returns a resolved promise.
1236+
/// Otherwise returns undefined.
1237+
pub inline fn returnMatcherValue(this: *const Expect, globalThis: *JSGlobalObject) JSValue {
1238+
if (this.flags.promise != .none) {
1239+
return JSPromise.resolvedPromiseValue(globalThis, .js_undefined);
1240+
}
1241+
return .js_undefined;
1242+
}
1243+
12341244
pub fn doUnreachable(globalThis: *JSGlobalObject, callframe: *CallFrame) bun.JSError!JSValue {
12351245
const arg = callframe.arguments_old(1).ptr[0];
12361246

@@ -2246,6 +2256,7 @@ const strings = bun.strings;
22462256
const jsc = bun.jsc;
22472257
const CallFrame = jsc.CallFrame;
22482258
const JSGlobalObject = jsc.JSGlobalObject;
2259+
const JSPromise = jsc.JSPromise;
22492260
const JSValue = jsc.JSValue;
22502261
const VirtualMachine = jsc.VirtualMachine;
22512262
const ZigString = jsc.ZigString;

src/bun.js/test/expect/toBe.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn toBe(
2222
var pass = try right.isSameValue(left, globalThis);
2323

2424
if (not) pass = !pass;
25-
if (pass) return .js_undefined;
25+
if (pass) return this.returnMatcherValue(globalThis);
2626

2727
// handle failure
2828
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };

src/bun.js/test/expect/toBeArray.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn toBeArray(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFra
99
const not = this.flags.not;
1010
const pass = value.jsType().isArray() != not;
1111

12-
if (pass) return .js_undefined;
12+
if (pass) return this.returnMatcherValue(globalThis);
1313

1414
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
1515
defer formatter.deinit();

src/bun.js/test/expect/toBeArrayOfSize.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn toBeArrayOfSize(this: *Expect, globalThis: *JSGlobalObject, callFrame: *C
2424
var pass = value.jsType().isArray() and @as(i32, @intCast(try value.getLength(globalThis))) == size.toInt32();
2525

2626
if (not) pass = !pass;
27-
if (pass) return .js_undefined;
27+
if (pass) return this.returnMatcherValue(globalThis);
2828

2929
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
3030
defer formatter.deinit();

src/bun.js/test/expect/toBeBoolean.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn toBeBoolean(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallF
99
const not = this.flags.not;
1010
const pass = value.isBoolean() != not;
1111

12-
if (pass) return .js_undefined;
12+
if (pass) return this.returnMatcherValue(globalThis);
1313

1414
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
1515
defer formatter.deinit();

src/bun.js/test/expect/toBeCloseTo.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn toBeCloseTo(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallF
4343
}
4444

4545
if (std.math.isPositiveInf(expected) and std.math.isPositiveInf(received)) {
46-
return .js_undefined;
46+
return this.returnMatcherValue(globalThis);
4747
}
4848

4949
const expected_diff = bun.pow(10, -precision) / 2;
@@ -53,7 +53,7 @@ pub fn toBeCloseTo(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallF
5353
const not = this.flags.not;
5454
if (not) pass = !pass;
5555

56-
if (pass) return .js_undefined;
56+
if (pass) return this.returnMatcherValue(globalThis);
5757

5858
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
5959
defer formatter.deinit();

src/bun.js/test/expect/toBeDate.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn toBeDate(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFram
99
const not = this.flags.not;
1010
const pass = value.isDate() != not;
1111

12-
if (pass) return .js_undefined;
12+
if (pass) return this.returnMatcherValue(globalThis);
1313

1414
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
1515
defer formatter.deinit();

src/bun.js/test/expect/toBeDefined.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn toBeDefined(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallF
99
const not = this.flags.not;
1010
var pass = !value.isUndefined();
1111
if (not) pass = !pass;
12-
if (pass) return .js_undefined;
12+
if (pass) return this.returnMatcherValue(globalThis);
1313

1414
// handle failure
1515
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };

src/bun.js/test/expect/toBeEmpty.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn toBeEmpty(this: *Expect, globalThis: *JSGlobalObject, callFrame: *CallFra
6161
}
6262

6363
if (not) pass = !pass;
64-
if (pass) return .js_undefined;
64+
if (pass) return this.returnMatcherValue(globalThis);
6565

6666
if (not) {
6767
const signature = comptime getSignature("toBeEmpty", "", true);

src/bun.js/test/expect/toBeEmptyObject.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn toBeEmptyObject(this: *Expect, globalThis: *JSGlobalObject, callFrame: *C
1010
var pass = try value.isObjectEmpty(globalThis);
1111

1212
if (not) pass = !pass;
13-
if (pass) return thisValue;
13+
if (pass) return this.returnMatcherValue(globalThis);
1414

1515
var formatter = jsc.ConsoleObject.Formatter{ .globalThis = globalThis, .quote_strings = true };
1616
defer formatter.deinit();

0 commit comments

Comments
 (0)