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 mock reassigning to previously unexisting method #8631

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- `[jest-haste-map]` Don't throw on missing mapper in Node crawler ([#8558](https://github.com/facebook/jest/pull/8558))
- `[jest-core]` Fix incorrect `passWithNoTests` warning ([#8595](https://github.com/facebook/jest/pull/8595))
- `[jest-snapshots]` Fix test retries that contain snapshots ([#8629](https://github.com/facebook/jest/pull/8629))
- `[jest-mock]` Fix incorrect assignments when restoring mocks in instances where they originally didn't exist ([#8631](https://github.com/facebook/jest/pull/8631))

### Chore & Maintenance

Expand Down
26 changes: 26 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,32 @@ describe('moduleMocker', () => {
});
});

it('mocks the method in the passed object itself', () => {
const parent = {func: () => 'abcd'};
const child = Object.create(parent);

moduleMocker.spyOn(child, 'func').mockReturnValue('efgh');

expect(child.hasOwnProperty('func')).toBe(true);
expect(child.func()).toEqual('efgh');
expect(parent.func()).toEqual('abcd');
});

it('should delete previously inexistent methods when restoring', () => {
const parent = {func: () => 'abcd'};
const child = Object.create(parent);

moduleMocker.spyOn(child, 'func').mockReturnValue('efgh');

moduleMocker.restoreAllMocks();
expect(child.func()).toEqual('abcd');

moduleMocker.spyOn(parent, 'func').mockReturnValue('jklm');
jeysal marked this conversation as resolved.
Show resolved Hide resolved

expect(child.hasOwnProperty('func')).toBe(false);
expect(child.func()).toEqual('jklm');
});

it('supports mock value returning undefined', () => {
const obj = {
func: () => 'some text',
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,9 +1009,15 @@ class ModuleMockerClass {
);
}

const isMethodOwner = object.hasOwnProperty(methodName);

// @ts-ignore overriding original method with a Mock
object[methodName] = this._makeComponent({type: 'function'}, () => {
object[methodName] = original;
if (isMethodOwner) {
object[methodName] = original;
} else {
delete object[methodName];
}
});

// @ts-ignore original method is now a Mock
Expand Down