aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-03-06 03:33:38 +0200
committerGravatar GitHub <noreply@github.com> 2023-03-06 03:33:38 +0200
commitd4bd156d9d3bebbf447e84183d4b49dea0b531cd (patch)
tree81a20e93fc3788c2a47e02786359ae4e32aed644 /src
parentc7bfb3aa3acdd1c8b11782530110aaf417cb79b8 (diff)
downloadbun-d4bd156d9d3bebbf447e84183d4b49dea0b531cd.tar.gz
bun-d4bd156d9d3bebbf447e84183d4b49dea0b531cd.tar.zst
bun-d4bd156d9d3bebbf447e84183d4b49dea0b531cd.zip
support `expect().toThrow(/pattern/)` (#2314)
- fix time-zone-dependent test failure
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/test/jest.zig77
1 files changed, 74 insertions, 3 deletions
diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig
index 20e11e132..5571294c7 100644
--- a/src/bun.js/test/jest.zig
+++ b/src/bun.js/test/jest.zig
@@ -1768,7 +1768,7 @@ pub const Expect = struct {
if (expected_value.isString()) {
const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7);
- // partial match (regex not supported)
+ // partial match
{
var expected_string = ZigString.Empty;
var received_string = ZigString.Empty;
@@ -1798,6 +1798,29 @@ pub const Expect = struct {
return .zero;
}
+ if (expected_value.isRegExp()) {
+ const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7);
+
+ if (expected_value.get(globalObject, "test")) |test_fn| {
+ const matches = test_fn.callWithThis(globalObject, expected_value, &.{received_message});
+ if (!matches.toBooleanSlow(globalObject)) return thisValue;
+ }
+
+ const fmt = signature ++ "\n\nExpected pattern: not <green>{any}<r>\nReceived message: <red>{any}<r>\n";
+ if (Output.enable_ansi_colors) {
+ globalObject.throw(Output.prettyFmt(fmt, true), .{
+ expected_value.toFmt(globalObject, &formatter),
+ received_message.toFmt(globalObject, &formatter),
+ });
+ return .zero;
+ }
+ globalObject.throw(Output.prettyFmt(fmt, false), .{
+ expected_value.toFmt(globalObject, &formatter),
+ received_message.toFmt(globalObject, &formatter),
+ });
+ return .zero;
+ }
+
if (expected_value.get(globalObject, "message")) |expected_message| {
const received_message = result.getIfPropertyExistsImpl(globalObject, "message", 7);
// no partial match for this case
@@ -1835,7 +1858,7 @@ pub const Expect = struct {
if (expected_value.isString()) {
if (_received_message) |received_message| {
- // partial match (regex not supported)
+ // partial match
var expected_string = ZigString.Empty;
var received_string = ZigString.Empty;
expected_value.toZigString(&expected_string, globalObject);
@@ -1877,6 +1900,42 @@ pub const Expect = struct {
return .zero;
}
+ if (expected_value.isRegExp()) {
+ if (_received_message) |received_message| {
+ if (expected_value.get(globalObject, "test")) |test_fn| {
+ const matches = test_fn.callWithThis(globalObject, expected_value, &.{received_message});
+ if (matches.toBooleanSlow(globalObject)) return thisValue;
+ }
+ }
+
+ // error: message from received error does not match expected pattern
+ var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .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: <green>{any}<r>\nReceived message: <red>{any}<r>\n";
+ if (Output.enable_ansi_colors) {
+ globalObject.throw(Output.prettyFmt(fmt, true), .{ expected_value_fmt, received_message_fmt });
+ return .zero;
+ }
+
+ globalObject.throw(Output.prettyFmt(fmt, false), .{ 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: <green>{any}<r>\nReceived value: <red>{any}<r>";
+ if (Output.enable_ansi_colors) {
+ globalObject.throw(Output.prettyFmt(fmt, true), .{ expected_fmt, received_fmt });
+ return .zero;
+ }
+
+ globalObject.throw(Output.prettyFmt(fmt, false), .{ expected_fmt, received_fmt });
+ return .zero;
+ }
+
if (expected_value.get(globalObject, "message")) |expected_message| {
if (_received_message) |received_message| {
if (received_message.isSameValue(expected_message, globalObject)) return thisValue;
@@ -1985,6 +2044,18 @@ pub const Expect = struct {
return .zero;
}
+ if (expected_value.isRegExp()) {
+ const expected_fmt = "\n\nExpected pattern: <green>{any}<r>\n\n" ++ received_line;
+ const fmt = signature ++ expected_fmt;
+ if (Output.enable_ansi_colors) {
+ globalObject.throw(Output.prettyFmt(fmt, true), .{expected_value.toFmt(globalObject, &formatter)});
+ return .zero;
+ }
+
+ globalObject.throw(Output.prettyFmt(fmt, false), .{expected_value.toFmt(globalObject, &formatter)});
+ return .zero;
+ }
+
if (expected_value.get(globalObject, "message")) |expected_message| {
const expected_fmt = "\n\nExpected message: <green>{any}<r>\n\n" ++ received_line;
const fmt = signature ++ expected_fmt;
@@ -2275,7 +2346,7 @@ pub const TestScope = struct {
task,
);
task.done_callback_state = .pending;
- initial_value = JSValue.fromRef(callback.?).call(vm.global, &.{callback_func});
+ initial_value = JSValue.fromRef(callback).call(vm.global, &.{callback_func});
} else {
initial_value = js.JSObjectCallAsFunctionReturnValue(vm.global, callback, null, 0, null);
}