diff options
author | 2023-03-14 23:30:50 +0800 | |
---|---|---|
committer | 2023-03-14 08:30:50 -0700 | |
commit | 6d50b90070d49607f6732769850ee4dd39fc0579 (patch) | |
tree | 6ebe715f7b2f39b693eac692ddcac5d2ef907e3e /src/bun.js | |
parent | 27f5012f50b998fec5b45531de2439a11a72eaa2 (diff) | |
download | bun-6d50b90070d49607f6732769850ee4dd39fc0579.tar.gz bun-6d50b90070d49607f6732769850ee4dd39fc0579.tar.zst bun-6d50b90070d49607f6732769850ee4dd39fc0579.zip |
feat: add toBeInstanceOf matcher in expect (#2389)
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/test/jest.zig | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/src/bun.js/test/jest.zig b/src/bun.js/test/jest.zig index 5571294c7..2cb86c4f7 100644 --- a/src/bun.js/test/jest.zig +++ b/src/bun.js/test/jest.zig @@ -2080,6 +2080,70 @@ pub const Expect = struct { return .zero; } + pub fn toBeInstanceOf(this: *Expect, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue { + defer this.postMatch(globalObject); + + 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", .{}); + return .zero; + } + + if (this.scope.tests.items.len <= this.test_id) { + globalObject.throw("toBeInstanceOf() must be called in a test", .{}); + return .zero; + } + + active_test_expectation_counter.actual += 1; + + const expected_value = arguments[0]; + if (!expected_value.jsType().isFunction()) { + var fmt = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true }; + globalObject.throw("Expected value must be a function: {any}", .{expected_value.toFmt(globalObject, &fmt)}); + return .zero; + } + expected_value.ensureStillAlive(); + + const value = Expect.capturedValueGetCached(thisValue) orelse { + globalObject.throw("Internal consistency error: the expect(value) was garbage collected but it should not have been!", .{}); + return .zero; + }; + value.ensureStillAlive(); + + const not = this.op.contains(.not); + var pass = value.isInstanceOf(globalObject, expected_value); + if (not) pass = !pass; + if (pass) return thisValue; + + // handle failure + var formatter = JSC.ZigConsoleClient.Formatter{ .globalThis = globalObject, .quote_strings = true }; + const value_fmt = value.toFmt(globalObject, &formatter); + if (not) { + const received_line = "Received: <red>{any}<r>\n"; + const fmt = comptime getSignature("toBeInstanceOf", "", true) ++ "\n\n" ++ received_line; + if (Output.enable_ansi_colors) { + globalObject.throw(Output.prettyFmt(fmt, true), .{value_fmt}); + return .zero; + } + + globalObject.throw(Output.prettyFmt(fmt, false), .{value_fmt}); + return .zero; + } + + const received_line = "Received: <red>{any}<r>\n"; + const fmt = comptime getSignature("toBeInstanceOf", "", false) ++ "\n\n" ++ received_line; + if (Output.enable_ansi_colors) { + globalObject.throw(Output.prettyFmt(fmt, true), .{value_fmt}); + return .zero; + } + + globalObject.throw(Output.prettyFmt(fmt, false), .{value_fmt}); + return .zero; + } + pub const toHaveBeenCalledTimes = notImplementedJSCFn; pub const toHaveBeenCalledWith = notImplementedJSCFn; pub const toHaveBeenLastCalledWith = notImplementedJSCFn; @@ -2089,7 +2153,6 @@ pub const Expect = struct { pub const toHaveLastReturnedWith = notImplementedJSCFn; pub const toHaveNthReturnedWith = notImplementedJSCFn; pub const toBeCloseTo = notImplementedJSCFn; - pub const toBeInstanceOf = notImplementedJSCFn; pub const toContainEqual = notImplementedJSCFn; pub const toMatch = notImplementedJSCFn; pub const toMatchObject = notImplementedJSCFn; |