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
4 changes: 3 additions & 1 deletion src/jsc/bindings/JSMockFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,9 @@ JSC_DEFINE_HOST_FUNCTION(jsMockFunctionCall, (JSGlobalObject * lexicalGlobalObje
}

JSC::ArgList args = JSC::ArgList(callframe);
JSValue thisValue = callframe->thisValue();
// For `fn()` calls JSC places the resolved scope (a JSScope) in the `this` slot and expects the
// callee to sanitize it. Do so before exposing it to JS, like ProxyObject::performCall does.
JSValue thisValue = callframe->thisValue().toThis(globalObject, JSC::ECMAMode::strict());
JSC::JSArray* argumentsArray = nullptr;
{
JSC::ObjectInitializationScope object(vm);
Expand Down
14 changes: 14 additions & 0 deletions test/js/bun/test/mock-fn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ describe("mock()", () => {
const obj = { fn };
expect(obj.fn()).toBe(obj);
});
test("bare call through a captured binding has an undefined this value", () => {
const fn = jest.fn().mockReturnThis();
let returned;
{
const ref = fn;
// Capturing `ref` in a closure makes the engine resolve `ref()` through a
// scope object; that scope must not be observable as the call's `this`.
const capture = () => ref;
returned = ref();
expect(capture()).toBe(fn);
}
expect(returned).toBeUndefined();
expect(fn.mock.contexts).toEqual([undefined]);
});
if (isBun) {
test("jest.fn(10) return value shorthand", () => {
expect(jest.fn(10)()).toBe(10);
Expand Down
Loading