Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/runtime/test_runner/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ impl Expect {
bun_jsc::JSPropertyIteratorOptions {
skip_empty_name: false,
include_value: true,
own_properties_only: false,
own_properties_only: true,
observable: true,
only_non_index_properties: false,
},
Expand Down
42 changes: 30 additions & 12 deletions test/js/bun/test/expect-extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ describe("async support", () => {

it("should not crash under intensive usage", () => {
withoutAggressiveGC(() => {
for (let i = 0; i < 10000; ++i) {
for (let i = 0; i < 2000; ++i) {
expect(i)._toBeDivisibleBy(1);
expect(i).toEqual(expect._toBeDivisibleBy(1));
}
Expand All @@ -355,28 +355,46 @@ it("should support asymmetric matchers", () => {
expect(() => expect(1).not._toCustomEqual(expect.any(Number))).toThrow();
});

it("works on prototypes", () => {
it("only registers own properties, not inherited ones", () => {
const Bar = {
_toBeBar() {
_toBeInherited() {
return { pass: true };
},
};
const Foo = Object.create(Bar);
Foo._toBeOwned = function () {
return { pass: true };
};

// Only own properties are registered (matches Jest's Object.keys behavior);
// the inherited _toBeInherited must not become a matcher.
expect.extend(Foo);
Comment thread
claude[bot] marked this conversation as resolved.
expect(123)._toBeBar();
expect(123)._toBeOwned();
expect(typeof expect(123)._toBeOwned).toBe("function");
expect(typeof expect(123)._toBeInherited).toBe("undefined");
});

it("works on classes", () => {
class Bar {
_toBeBar() {
it("works on plain objects", () => {
const matchers = {
_toBeBar2() {
return { pass: true };
}
}
class Foo extends Bar {}
},
};

expect.extend(matchers);
expect(123)._toBeBar2();
});

it("does not crash when prototype has non-function properties", () => {
const proto = { [Symbol.toStringTag]: "CustomMatcher", inheritedProp: "not a function" };
const matchers = Object.create(proto);
matchers._toBeOwnProp = function (actual, expected) {
return { pass: actual === expected, message: () => `expected ${actual} to be ${expected}` };
};

expect.extend(new Foo());
expect(123)._toBeBar();
// Should not throw due to inherited non-function properties on the prototype
expect.extend(matchers);
expect(42)._toBeOwnProp(42);
});

test("expect.extend with numeric index keys does not crash", () => {
Expand Down
Loading