jest-mock
Advanced tools
Comparing version 11.0.0 to 11.0.1
{ | ||
"name": "jest-mock", | ||
"version": "11.0.0", | ||
"version": "11.0.1", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -93,5 +93,10 @@ # jest-mock | ||
##### `.mockImplementationOnce(function)` | ||
Pushes the given mock implementation onto a FIFO queue of mock | ||
implementations for the function. | ||
##### `.mockImplementation(function)` | ||
Sets a mock implementation for the function. | ||
Sets the default mock implementation for the function. | ||
@@ -104,10 +109,11 @@ ##### `.mockReturnThis()` | ||
In case both `mockImplementation()` and | ||
In case both `mockImplementationOnce()/mockImplementation()` and | ||
`mockReturnValueOnce()/mockReturnValue()` are called. The priority of which to | ||
use is based on what is the last call: | ||
- if the last call is mockReturnValueOnce() or mockReturnValue(), | ||
use the specific return specific return value or default return value. | ||
use the specific return value or default return value. | ||
If specific return values are used up or no default return value is set, | ||
fall back to try mockImplementation(); | ||
- if the last call is mockImplementation(), run the given implementation | ||
and return the result. | ||
- if the last call is mockImplementationOnce() or mockImplementation(), | ||
run the specific implementation and return the result or run default | ||
implementation and return the result. |
@@ -126,2 +126,35 @@ /** | ||
}); | ||
describe('mockImplementationOnce', () => { | ||
it('should mock single call to a mock function', () => { | ||
const mockFn = moduleMocker.getMockFunction(); | ||
mockFn.mockImplementationOnce(() => { | ||
return 'Foo'; | ||
}).mockImplementationOnce(() => { | ||
return 'Bar'; | ||
}); | ||
expect(mockFn()).toBe('Foo'); | ||
expect(mockFn()).toBe('Bar'); | ||
expect(mockFn()).toBeUndefined(); | ||
}); | ||
it('should fallback to default mock function when no specific mock is available', () => { | ||
const mockFn = moduleMocker.getMockFunction(); | ||
mockFn.mockImplementationOnce(() => { | ||
return 'Foo'; | ||
}).mockImplementationOnce(() => { | ||
return 'Bar'; | ||
}).mockImplementation(() => { | ||
return 'Default'; | ||
}); | ||
expect(mockFn()).toBe('Foo'); | ||
expect(mockFn()).toBe('Bar'); | ||
expect(mockFn()).toBe('Default'); | ||
expect(mockFn()).toBe('Default'); | ||
}); | ||
}); | ||
}); |
@@ -192,2 +192,3 @@ /** | ||
const specificReturnValues = []; | ||
const specificMockImpls = []; | ||
const calls = []; | ||
@@ -224,3 +225,3 @@ const instances = []; | ||
// mockReturnValueOnce()/mockReturnValue() is called and no | ||
// mockImplementation() is called after that. | ||
// mockImplementationOnce()/mockImplementation() is called after that. | ||
// use the set return value. | ||
@@ -234,6 +235,13 @@ if (isReturnValueLastSet) { | ||
// If mockImplementation() is last set, or specific return values | ||
// are used up, use the mock implementation. | ||
if (mockImpl && returnValue === undefined) { | ||
return mockImpl.apply(this, arguments); | ||
// If mockImplementationOnce()/mockImplementation() is last set, | ||
// or specific return values are used up, use the mock implementation. | ||
let specificMockImpl; | ||
if (returnValue === undefined) { | ||
specificMockImpl = specificMockImpls.shift(); | ||
if (specificMockImpl === undefined) { | ||
specificMockImpl = mockImpl; | ||
} | ||
if (specificMockImpl) { | ||
return specificMockImpl.apply(this, arguments); | ||
} | ||
} | ||
@@ -273,2 +281,10 @@ | ||
f.mockImplementationOnce = fn => { | ||
// next function call will use this mock implementation return value | ||
// or default mock implementation return value | ||
isReturnValueLastSet = false; | ||
specificMockImpls.push(fn); | ||
return f; | ||
}; | ||
f.mockImplementation = f.mockImpl = fn => { | ||
@@ -342,3 +358,3 @@ // next function call will use mock implementation return value | ||
const metadata = {type: type}; | ||
const metadata = {type}; | ||
if ( | ||
@@ -345,0 +361,0 @@ type === 'constant' || |
19675
536
118