Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1555,9 +1555,12 @@ declare module "bun:test" {
/**
* Asserts that a value is an empty `object`.
*
* An array, a function, a `Map`, a `Set`, a `Date` and a `RegExp` are not an `object` here.
*
* @example
* expect({}).toBeEmptyObject();
* expect({ a: 'hello' }).not.toBeEmptyObject();
* expect([]).not.toBeEmptyObject();
*/
toBeEmptyObject(): void;

Expand Down Expand Up @@ -1599,6 +1602,7 @@ declare module "bun:test" {
* @example
* expect(true).toBeBoolean();
* expect(false).toBeBoolean();
* expect(new Boolean(true)).toBeBoolean();
* expect(null).not.toBeBoolean();
* expect(0).not.toBeBoolean();
*/
Expand Down
33 changes: 29 additions & 4 deletions src/jsc/JSValue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2849,14 +2849,39 @@ impl JSValue {
JSC__JSValue__isConstructor(self)
}

/// `Array.isArray(self)`, but a revoked `Proxy` is `false` instead of a TypeError.
pub fn is_array_or_proxied_array(self) -> bool {
let mut value = self;
while value.is_cell() {
let ty = value.js_type();
if ty.is_array() {
return true;
}
// `revoke()` clears the handler and keeps the target.
if ty != JSType::ProxyObject
|| value
.get_proxy_internal_field(ProxyField::Handler)
.is_undefined_or_null()
{
return false;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
value = value.get_proxy_internal_field(ProxyField::Target);
}
false
}

// ── Jest "is empty object". ────────────────────────
/// `JSValue.isObjectEmpty` — Jest-extended `toBeEmptyObject` semantics:
/// Map/Set/RegExp/Date are *not* empty objects; otherwise an object with
/// zero own-enumerable keys.
/// jest-extended `toBeEmptyObject`: a `jest-get-type` "object" with no own enumerable keys.
pub fn is_object_empty(self, global: &JSGlobalObject) -> JsResult<bool> {
let ty = self.js_type();
// https://github.com/jestjs/jest/blob/main/packages/jest-get-type/src/index.ts#L26
if ty.is_map() || ty.is_set() || ty == JSType::RegExpObject || self.is_date() {
if self.is_array_or_proxied_array()
|| self.is_callable()
|| ty.is_map()
|| ty.is_set()
|| ty == JSType::RegExpObject
|| self.is_date()
{
return Ok(false);
}
Ok(ty.is_object() && self.keys(global)?.get_length(global)? == 0)
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/test_runner/expect/simple_matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bun_core::strings;
use bun_jsc::{CallFrame, JSGlobalObject, JSValue, JsResult};

// ── unary predicates: expect(x).toBe<Kind>() ───────────────────────────────
crate::unary_predicate_matcher!(to_be_array, "toBeArray", |v| v.js_type().is_array());
crate::unary_predicate_matcher!(to_be_boolean, "toBeBoolean", |v| v.is_boolean());
crate::unary_predicate_matcher!(to_be_array, "toBeArray", |v| v.is_array_or_proxied_array());
// jest-extended: `typeof v === "boolean" || v instanceof Boolean`
crate::unary_predicate_matcher!(to_be_boolean, "toBeBoolean", |v| v.is_boolean()
|| v.js_type() == bun_jsc::JSType::BooleanObject);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
crate::unary_predicate_matcher!(to_be_date, "toBeDate", |v| v.is_date());
crate::unary_predicate_matcher!(to_be_defined, "toBeDefined", |v| !v.is_undefined());
crate::unary_predicate_matcher!(to_be_false, "toBeFalse", |v| v.is_boolean()
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/test_runner/expect/toBeArrayOfSize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn to_be_array_of_size(
this.increment_expect_call_counter();

let not = this.flags.get().not();
let mut pass = value.js_type().is_array()
let mut pass = value.is_array_or_proxied_array()
&& value.get_length(global)? as i64 == size.to_int64();

if not {
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/test_runner/expect/toContainAllValues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ impl Expect {
let count = values.get_length(g)?;
if count != expected.get_length(g)? { return Ok(ContainOutcome::pass(false)); }
let mut itr = expected.array_iterator(g)?;
let mut pass = false;
'outer: while let Some(item) = itr.next()? {
let mut i: u32 = 0;
while u64::from(i) < count {
if values.get_index(g, i)?.jest_deep_equals(item, g)? { pass = true; continue 'outer; }
if values.get_index(g, i)?.jest_deep_equals(item, g)? { continue 'outer; }
i += 1;
}
return Ok(ContainOutcome::pass(false));
}
Ok(ContainOutcome::pass(pass))
Ok(ContainOutcome::pass(true))
})
}
}
36 changes: 35 additions & 1 deletion test/js/bun/test/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,18 @@ describe("expect()", () => {
expect(deepArray).not.toContainAllValues(["duck", [{ foo: "bar" }]]);
});

test_skipIf(!hasJestExtended)("toContainAllValues with no values", () => {
expect({}).toContainAllValues([]);
expect(Object.create(null)).toContainAllValues([]);
expect(() => expect({}).not.toContainAllValues([])).toThrow("toContainAllValues");
expect({}).not.toContainAllValues([undefined]);
expect({ a: 1 }).not.toContainAllValues([]);
// The other empty cases already agree with jest-extended.
expect({}).toContainAllKeys([]);
expect({}).toContainValues([]);
expect({}).not.toContainAnyValues([]);
});

test("toContainAnyValues", () => {
let o = { a: "foo", b: "bar", c: "baz" };
expect(o).toContainAnyValues(["qux", "foo"]);
Expand Down Expand Up @@ -3818,7 +3830,7 @@ describe("expect()", () => {
expect(new Map()).not.toBeEmptyObject();
expect(new Set()).not.toBeEmptyObject();
expect(new Set().add("1")).not.toBeEmptyObject();
expect([]).toBeEmptyObject();
expect([]).not.toBeEmptyObject();
expect({}).toBeEmptyObject();
expect([1, 2]).not.toBeEmptyObject();
expect({ a: "hello" }).not.toBeEmptyObject();
Expand Down Expand Up @@ -3846,6 +3858,28 @@ describe("expect()", () => {
expect(/(foo|bar)/g).not.toBeEmptyObject();
});

test_skipIf(!hasJestExtended)("toBeEmptyObject() rejects a function and an array", () => {
// jest-get-type reports "function" and "array" for these, not "object".
expect(function () {}).not.toBeEmptyObject();
expect(() => {}).not.toBeEmptyObject();
expect(async () => {}).not.toBeEmptyObject();
expect(class {}).not.toBeEmptyObject();
expect(Symbol).not.toBeEmptyObject();
expect(function () {}.bind(null)).not.toBeEmptyObject();
expect(new Proxy(function () {}, {})).not.toBeEmptyObject();
expect([]).not.toBeEmptyObject();
expect(new Array(3)).not.toBeEmptyObject();
expect(new Proxy([], {})).not.toBeEmptyObject();
expect(() => expect(function () {}).toBeEmptyObject()).toThrow("toBeEmptyObject");
expect(() => expect([]).toBeEmptyObject()).toThrow("toBeEmptyObject");

expect(Object.create(null)).toBeEmptyObject();
expect(new (class {})()).toBeEmptyObject();
expect(new Proxy({}, {})).toBeEmptyObject();
expect(new Proxy({ a: 1 }, {})).not.toBeEmptyObject();
expect(() => expect({}).not.toBeEmptyObject()).toThrow("toBeEmptyObject");
});

test("toBeNil()", () => {
expect(null).toBeNil();
expect(undefined).toBeNil();
Expand Down
38 changes: 38 additions & 0 deletions test/js/bun/test/jest-extended.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,31 @@ describe("jest-extended", () => {
expect(new Array(2 ** 32 - 1)).toBeArrayOfSize(2 ** 32 - 1);
});

test("toBeArray() and toBeArrayOfSize() see through a Proxy, like Array.isArray()", () => {
expect(new Proxy([], {})).toBeArray();
expect(new Proxy(new Proxy([1, 2], {}), {})).toBeArray();
expect(new Proxy({}, {})).not.toBeArray();
expect(new Proxy(function () {}, {})).not.toBeArray();

expect(new Proxy([], {})).toBeArrayOfSize(0);
expect(new Proxy([1, 2], {})).toBeArrayOfSize(2);
expect(new Proxy(new Proxy([1, 2], {}), {})).toBeArrayOfSize(2);
expect(new Proxy([1, 2], {})).not.toBeArrayOfSize(3);
expect(new Proxy({ length: 0 }, {})).not.toBeArrayOfSize(0);
expect(() => expect(new Proxy([1, 2], {})).toBeArrayOfSize(3)).toThrow("toBeArrayOfSize");
expect(() => expect(new Proxy([], {})).not.toBeArray()).toThrow("toBeArray");
});

// Array.isArray() throws a TypeError for a revoked Proxy, so jest-extended does too.
(isBun ? test : test.skip)("toBeArray() and toBeArrayOfSize() reject a revoked Proxy", () => {
const { proxy, revoke } = Proxy.revocable([], {});
revoke();
expect(proxy).not.toBeArray();
expect(proxy).not.toBeArrayOfSize(0);
expect(new Proxy(proxy, {})).not.toBeArray();
expect(() => expect(proxy).toBeArray()).toThrow("<Revoked Proxy>");
});

// test('toIncludeAllMembers()')
// test('toIncludeAllPartialMembers()')
// test('toIncludeAnyMembers()')
Expand All @@ -238,6 +263,19 @@ describe("jest-extended", () => {
expect({}).not.toBeBoolean();
});

test("toBeBoolean() accepts a Boolean object, toBeTrue() and toBeFalse() do not", () => {
expect(new Boolean(true)).toBeBoolean();
expect(new Boolean(false)).toBeBoolean();
expect(Object(false)).toBeBoolean();
expect(new (class extends Boolean {})(true)).toBeBoolean();
expect(new Number(1)).not.toBeBoolean();
expect(new String("true")).not.toBeBoolean();
expect(() => expect(new Boolean(true)).not.toBeBoolean()).toThrow("toBeBoolean");

expect(new Boolean(true)).not.toBeTrue();
expect(new Boolean(false)).not.toBeFalse();
});

test("toBeTrue()", () => {
expect(true).toBeTrue();
expect(false).not.toBeTrue();
Expand Down
Loading