@@ -241,3 +241,84 @@ const myMockFn = jest.fn()
241241console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
242242> 'first call', 'second call', 'default', 'default'
243243```
244+
245+ ### ` mockFn.mockResolvedValue(value) `
246+
247+ Simple sugar function for:
248+
249+ ``` js
250+ jest .fn ().mockReturnValue (Promise .resolve (value));
251+ ```
252+
253+ Useful to mock async functions in async tests:
254+
255+ ``` js
256+ test (' async test' , async () => {
257+ const asyncMock = jest .fn ().mockResolvedValue (43 );
258+
259+ await asyncMock (); // 43
260+ });
261+ ```
262+
263+ ### ` mockFn.mockRejectedValueOnce(value) `
264+
265+ Simple sugar function for:
266+
267+ ``` js
268+ jest .fn ().mockReturnValueOnce (Promise .resolve (value));
269+ ```
270+
271+ Useful to resolve different values over multiple async calls:
272+
273+ ``` js
274+ test (' async test' , async () => {
275+ const asyncMock = jest .fn ()
276+ .mockResolvedValue (' default' )
277+ .mockResolvedValueOnce (' first call' )
278+ .mockResolvedValueOnce (' second call' );
279+
280+ await asyncMock (); // first call
281+ await asyncMock (); // second call
282+ await asyncMock (); // default
283+ await asyncMock (); // default
284+ });
285+ ```
286+
287+ ### ` mockFn.mockRejectedValue(value) `
288+
289+ Simple sugar function for:
290+
291+ ``` js
292+ jest .fn ().mockReturnValue (Promise .reject (value));
293+ ```
294+
295+ Useful to create async mock functions that will always reject:
296+
297+ ``` js
298+ test (' async test' , async () => {
299+ const asyncMock = jest .fn ().mockRejectedValue (new Error (' Async error' ));
300+
301+ await asyncMock (); // throws "Async error"
302+ });
303+ ```
304+
305+ ### ` mockFn.mockRejectedValueOnce(value) `
306+
307+ Simple sugar function for:
308+
309+ ``` js
310+ jest .fn ().mockReturnValueOnce (Promise .reject (value));
311+ ```
312+
313+ Example usage:
314+
315+ ``` js
316+ test (' async test' , async () => {
317+ const asyncMock = jest .fn ()
318+ .mockResolvedValueOnce (' first call' )
319+ .mockRejectedValueOnce (new Error (' Async error' ));
320+
321+ await asyncMock (); // first call
322+ await asyncMock (); // throws "Async error"
323+ });
324+ ```
0 commit comments