Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Do not throw when trying to fill readonly properties #14402

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
6 changes: 5 additions & 1 deletion packages/core/src/utils-hoist/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export function fill(source: { [key: string]: any }, name: string, replacementFa
markFunctionWrapped(wrapped, original);
}

source[name] = wrapped;
try {
source[name] = wrapped;
} catch {
DEBUG_BUILD && logger.log(`Failed to replace method "${name}" in object`, source);
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/utils-hoist/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ describe('fill()', () => {
expect(replacement).toBeCalled();
});

test('does not throw on readonly properties', () => {
const originalFn = () => 41;
const source = {
get prop() {
return originalFn;
},
set prop(_fn: () => number) {
throw new Error('OH NO, this is not writeable...');
},
};

expect(source.prop()).toEqual(41);

const replacement = jest.fn().mockImplementation(() => {
return () => 42;
});
fill(source, 'prop', replacement);
expect(replacement).toBeCalled();

expect(source.prop).toBe(originalFn);
expect(source.prop()).toEqual(41);
});

test('can do anything inside replacement function', () => {
const source = {
foo: (): number => 42,
Expand Down
Loading