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(jest-mock): prevent mockImplementationOnce bleeding into withImplementation #13888

Merged
merged 2 commits into from
Feb 10, 2023
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 @@ -5,6 +5,7 @@
### Fixes

- `[jest-mock]` Clear mock state when `jest.restoreAllMocks()` is called ([#13867](https://github.com/facebook/jest/pull/13867))
- `[jest-mock]` Prevent `mockImplementationOnce` and `mockReturnValueOnce` bleeding into `withImplementation` ([#13888](https://github.com/facebook/jest/pull/13888))

### Chore & Maintenance

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

expect.assertions(4);
});

it('mockImplementationOnce does not bleed into withImplementation', () => {
const mock = jest
.fn(() => 'outside callback')
.mockImplementationOnce(() => 'once');

mock.withImplementation(
() => 'inside callback',
() => {
expect(mock()).toBe('inside callback');
},
);

expect(mock()).toBe('once');
expect(mock()).toBe('outside callback');
});

it('mockReturnValueOnce does not bleed into withImplementation', () => {
const mock = jest
.fn(() => 'outside callback')
.mockReturnValueOnce('once');

mock.withImplementation(
() => 'inside callback',
() => {
expect(mock()).toBe('inside callback');
},
);

expect(mock()).toBe('once');
expect(mock()).toBe('outside callback');
});
});

test('mockReturnValue does not override mockImplementationOnce', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,20 @@ export class ModuleMocker {
// Remember previous mock implementation, then set new one
const mockConfig = this._ensureMockConfig(f);
const previousImplementation = mockConfig.mockImpl;
const previousSpecificImplementations = mockConfig.specificMockImpls;
mockConfig.mockImpl = fn;
mockConfig.specificMockImpls = [];

const returnedValue = callback();

if (isPromise(returnedValue)) {
return returnedValue.then(() => {
mockConfig.mockImpl = previousImplementation;
mockConfig.specificMockImpls = previousSpecificImplementations;
});
} else {
mockConfig.mockImpl = previousImplementation;
mockConfig.specificMockImpls = previousSpecificImplementations;
}
}

Expand Down