Skip to content

Commit

Permalink
fix: mockImplementation doesn't clear once stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 16, 2024
1 parent f91f41f commit 7d406e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 15 additions & 0 deletions __test__/jest/mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ test('methods are overridable', () => {
expect(f()).toBe(1)
})

test('mockImplementationOnce stacks and takes precedence even when is called before', () => {
const f = jest.fn(() => 2)
expect(f()).toBe(2)
expect(f()).toBe(2)
f.mockImplementationOnce(() => 4)
f.mockImplementationOnce(() => 5)
expect(f()).toBe(4)
f.mockImplementationOnce(() => 6)
f.mockImplementation(() => 3)
expect(f()).toBe(5)
expect(f()).toBe(6)
expect(f()).toBe(3)
expect(f()).toBe(3)
})

test('mockReset resets to undefined', () => {
const f = jest.fn(() => 1)
expect(f()).toBe(1)
Expand Down
1 change: 0 additions & 1 deletion src/jest.fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const jestfn = (baseimpl, parent, property) => {

const queuedMock = (impl) => {
mockimpl = impl || noop
onceStack.length = 0
}

// getMockImplementation() is undocumented and is changed only in real mockImplementation() call
Expand Down

0 comments on commit 7d406e9

Please sign in to comment.