Skip to content

Commit

Permalink
Merge pull request #82 from mfressdorf/master
Browse files Browse the repository at this point in the history
Keep original function implementation when not matched
  • Loading branch information
timkindberg authored Sep 19, 2021
2 parents bbb5396 + 9b81e2b commit fe6dba9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ class WhenMock {
return mockImplementation(...args)
}
}

return defaultImplementation ? defaultImplementation(...args) : undefined
return defaultImplementation ? defaultImplementation(...args)
: (typeof fn.__whenMock__._origMock === 'function'
? fn.__whenMock__._origMock(...args) : undefined)
})

return {
Expand Down
25 changes: 25 additions & 0 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,5 +819,30 @@ describe('When', () => {
const returnValue = theInstance.theMethod(1)
expect(returnValue).toBe('mock')
})

it('keeps default function implementation when not matched', () => {
class TheClass {
fn () {
return 'real'
}
}
const instance = new TheClass()
const spy = jest.spyOn(instance, 'fn')
when(spy)
.calledWith(1)
.mockReturnValue('mock')
expect(instance.fn(2)).toBe('real')
})

it('keeps default mock implementation when not matched', () => {
const fn = jest.fn(() => {
return 'real'
})
when(fn)
.calledWith(1)
.mockReturnValue('mock')
expect(fn(1)).toBe('mock')
expect(fn(2)).toBe('real')
})
})
})

0 comments on commit fe6dba9

Please sign in to comment.