-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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): clear mock when jest.restoreAllMocks()
is called
#13867
Merged
SimenB
merged 5 commits into
jestjs:main
from
mrazauskas:fix-clear-mocks-when-jest.restoreAllMocks()-is-called
Feb 8, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1312,6 +1312,36 @@ describe('moduleMocker', () => { | |
); | ||
}); | ||
|
||
it('supports restoring a spy', () => { | ||
let methodOneCalls = 0; | ||
const obj = { | ||
methodOne() { | ||
methodOneCalls++; | ||
}, | ||
}; | ||
|
||
const spy1 = moduleMocker.spyOn(obj, 'methodOne'); | ||
|
||
obj.methodOne(); | ||
|
||
// The spy and the original function got called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
|
||
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true); | ||
|
||
spy1.mockRestore(); | ||
|
||
// After restoring the spy, the method is not mock function. | ||
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(false); | ||
|
||
obj.methodOne(); | ||
|
||
// After restoring the spy only the real method bumps its call count, not the spy. | ||
expect(methodOneCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('supports restoring all spies', () => { | ||
let methodOneCalls = 0; | ||
let methodTwoCalls = 0; | ||
|
@@ -1327,25 +1357,32 @@ describe('moduleMocker', () => { | |
const spy1 = moduleMocker.spyOn(obj, 'methodOne'); | ||
const spy2 = moduleMocker.spyOn(obj, 'methodTwo'); | ||
|
||
// First, we call with the spies: both spies and both original functions | ||
// should be called. | ||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// Both spies and both original functions got called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(methodTwoCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
|
||
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true); | ||
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true); | ||
|
||
moduleMocker.restoreAllMocks(); | ||
|
||
// Then, after resetting all mocks, we call methods again. Only the real | ||
// methods should bump their count, not the spies. | ||
// After restoring all mocks, the methods are not mock functions. | ||
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(false); | ||
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(false); | ||
|
||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// After restoring all mocks only the real methods bump their count, not the spies. | ||
expect(methodOneCalls).toBe(2); | ||
expect(methodTwoCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
expect(spy2.mock.calls).toHaveLength(0); | ||
Comment on lines
-1347
to
+1385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But after calling So this is fixed. |
||
}); | ||
|
||
it('should work with getters', () => { | ||
|
@@ -1482,6 +1519,33 @@ describe('moduleMocker', () => { | |
); | ||
}); | ||
|
||
it('supports restoring a spy', () => { | ||
let methodOneCalls = 0; | ||
const obj = { | ||
get methodOne() { | ||
return function () { | ||
methodOneCalls++; | ||
}; | ||
}, | ||
}; | ||
|
||
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get'); | ||
|
||
obj.methodOne(); | ||
|
||
// The spy and the original function are called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
|
||
spy1.mockRestore(); | ||
|
||
obj.methodOne(); | ||
|
||
// After restoring the spy only the real method bumps its call count, not the spy. | ||
expect(methodOneCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('supports restoring all spies', () => { | ||
let methodOneCalls = 0; | ||
let methodTwoCalls = 0; | ||
|
@@ -1501,25 +1565,25 @@ describe('moduleMocker', () => { | |
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get'); | ||
const spy2 = moduleMocker.spyOn(obj, 'methodTwo', 'get'); | ||
|
||
// First, we call with the spies: both spies and both original functions | ||
// should be called. | ||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// Both spies and both original functions got called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(methodTwoCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
|
||
moduleMocker.restoreAllMocks(); | ||
|
||
// Then, after resetting all mocks, we call methods again. Only the real | ||
// methods should bump their count, not the spies. | ||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// After restoring all mocks only the real methods bump their count, not the spies. | ||
expect(methodOneCalls).toBe(2); | ||
expect(methodTwoCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
expect(spy2.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('should work with getters on the prototype chain', () => { | ||
|
@@ -1587,6 +1651,34 @@ describe('moduleMocker', () => { | |
expect(obj.property).toBe(true); | ||
}); | ||
|
||
it('supports restoring a spy on the prototype chain', () => { | ||
let methodOneCalls = 0; | ||
const prototype = { | ||
get methodOne() { | ||
return function () { | ||
methodOneCalls++; | ||
}; | ||
}, | ||
}; | ||
const obj = Object.create(prototype, {}); | ||
|
||
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get'); | ||
|
||
obj.methodOne(); | ||
|
||
// The spy and the original function are called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
|
||
spy1.mockRestore(); | ||
|
||
obj.methodOne(); | ||
|
||
// After restoring the spy only the real method bumps its call count, not the spy. | ||
expect(methodOneCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
}); | ||
|
||
it('supports restoring all spies on the prototype chain', () => { | ||
let methodOneCalls = 0; | ||
let methodTwoCalls = 0; | ||
|
@@ -1607,25 +1699,25 @@ describe('moduleMocker', () => { | |
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get'); | ||
const spy2 = moduleMocker.spyOn(obj, 'methodTwo', 'get'); | ||
|
||
// First, we call with the spies: both spies and both original functions | ||
// should be called. | ||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// Both spies and both original functions got called. | ||
expect(methodOneCalls).toBe(1); | ||
expect(methodTwoCalls).toBe(1); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
|
||
moduleMocker.restoreAllMocks(); | ||
|
||
// Then, after resetting all mocks, we call methods again. Only the real | ||
// methods should bump their count, not the spies. | ||
obj.methodOne(); | ||
obj.methodTwo(); | ||
|
||
// After restoring all mocks only the real methods bump their count, not the spies. | ||
expect(methodOneCalls).toBe(2); | ||
expect(methodTwoCalls).toBe(2); | ||
expect(spy1.mock.calls).toHaveLength(1); | ||
expect(spy2.mock.calls).toHaveLength(1); | ||
expect(spy1.mock.calls).toHaveLength(0); | ||
expect(spy2.mock.calls).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
|
@@ -1664,7 +1756,7 @@ describe('moduleMocker', () => { | |
expect(obj.property).toBe(1); | ||
}); | ||
|
||
it('should allow mocking with value of different value', () => { | ||
it('should allow mocking with value of different type', () => { | ||
const obj = { | ||
property: 1, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
mockRestore()
the state is reset. This is passing onmain
as well.