Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "jest-when", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "An extension lib for jest", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -36,2 +36,4 @@ # jest-when | ||
* `mockRejectedValueOnce` | ||
* `mockImplementation` | ||
* `mockImplementationOnce` | ||
@@ -184,2 +186,14 @@ ### Usage | ||
### Supports custom mock implementation via callback | ||
```javascript | ||
when(fn).mockImplementation((...args) => { | ||
// custom mock logic | ||
if (args.length === 1 && args[0] === "a") { | ||
return 'foo' | ||
} | ||
return 'bar' | ||
}) | ||
``` | ||
### Contributors (in order of contribution) | ||
@@ -190,1 +204,3 @@ * [@timkindberg](https://github.com/timkindberg/) (original author) | ||
* [@danielhusar](https://github.com/danielhusar) | ||
* [@idan-at](https://github.com/idan-at) | ||
@@ -0,0 +0,0 @@ const bunyan = require('bunyan') |
@@ -0,0 +0,0 @@ const log = require('./log') |
@@ -26,2 +26,4 @@ const utils = require('expect/build/jasmine_utils') | ||
constructor (fn, defaultValue = { isSet: false, val: undefined }) { | ||
// Incrementing ids assigned to each call mock to help with sorting as new mocks are added | ||
this.nextCallMockId = 0 | ||
this.fn = fn | ||
@@ -43,5 +45,12 @@ this.callMocks = [] | ||
.filter((callMock) => once || callMock.once || !utils.equals(callMock.matchers, matchers)) | ||
.concat({ matchers, val, assertCall, once }) | ||
.sort(({ once }) => !once ? 1 : 0) | ||
.concat({ matchers, val, assertCall, once, id: this.nextCallMockId }) | ||
.sort((a, b) => { | ||
// Reduce their id by 1000 if they are a once mock, to sort them at the front | ||
const aId = a.id - (a.once ? 1000 : 0) | ||
const bId = b.id - (b.once ? 1000 : 0) | ||
return aId - bId | ||
}) | ||
this.nextCallMockId++ | ||
this.fn.mockImplementation((...args) => { | ||
@@ -63,3 +72,3 @@ logger.debug('mocked impl', args) | ||
}) | ||
return val | ||
return typeof val === 'function' ? val(...args) : val | ||
} | ||
@@ -83,3 +92,5 @@ } | ||
mockRejectedValue: err => _mockReturnValue(matchers, assertCall)(Promise.reject(err)), | ||
mockRejectedValueOnce: err => _mockReturnValue(matchers, assertCall, true)(Promise.reject(err)) | ||
mockRejectedValueOnce: err => _mockReturnValue(matchers, assertCall, true)(Promise.reject(err)), | ||
mockImplementation: implementation => _mockReturnValue(matchers, assertCall)(implementation), | ||
mockImplementationOnce: implementation => _mockReturnValue(matchers, assertCall, true)(implementation) | ||
}) | ||
@@ -86,0 +97,0 @@ |
@@ -385,3 +385,23 @@ const { stringContaining } = expect | ||
}) | ||
it('allows using mockImplementation', () => { | ||
const fn = jest.fn() | ||
when(fn).calledWith('foo', 'bar').mockImplementation((...args) => args) | ||
expect(fn('foo', 'bar')).toEqual(['foo', 'bar']) | ||
expect(fn('foo', 'bar')).toEqual(['foo', 'bar']) | ||
expect(fn('not-foo')).toBeUndefined() | ||
}) | ||
it('allows using mockImplementationOnce', () => { | ||
const fn = jest.fn() | ||
when(fn).calledWith('foo', 'bar').mockImplementationOnce((...args) => args) | ||
expect(fn('foo', 'bar')).toEqual(['foo', 'bar']) | ||
expect(fn('foo')).toBeUndefined() | ||
}) | ||
}) | ||
}) |
@@ -0,0 +0,0 @@ module.exports = function (config) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
202766
458
204