From 025b17ecc88094f588dfd7b4b20a0fc08343a175 Mon Sep 17 00:00:00 2001 From: javinor Date: Sun, 4 Mar 2018 19:36:36 +0200 Subject: [PATCH] implement mockResolved/RejectedValue methods using mockImplementation --- CHANGELOG.md | 2 ++ docs/MockFunctionAPI.md | 8 ++++---- packages/jest-mock/src/index.js | 26 ++++++++------------------ 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 226c9f3d4268..73cc50ad23e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ ([#5670](https://github.com/facebook/jest/pull/5670)) * `[jest-runtime]` Prevent Babel warnings on large files ([#5702](https://github.com/facebook/jest/pull/5702)) +* `[jest-mock]` Prevent `mockRejectedValue` from causing unhandled rejection + ([#5720](https://github.com/facebook/jest/pull/5720)) ### Chore & Maintenance diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 734ac9dbe008..38d9b7f9236f 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -250,7 +250,7 @@ console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); Simple sugar function for: ```js -jest.fn().mockReturnValue(Promise.resolve(value)); +jest.fn().mockImplementation(() => Promise.resolve(value)); ``` Useful to mock async functions in async tests: @@ -270,7 +270,7 @@ test('async test', async () => { Simple sugar function for: ```js -jest.fn().mockReturnValueOnce(Promise.resolve(value)); +jest.fn().mockImplementationOnce(() => Promise.resolve(value)); ``` Useful to resolve different values over multiple async calls: @@ -297,7 +297,7 @@ test('async test', async () => { Simple sugar function for: ```js -jest.fn().mockReturnValue(Promise.reject(value)); +jest.fn().mockImplementation(() => Promise.reject(value)); ``` Useful to create async mock functions that will always reject: @@ -317,7 +317,7 @@ test('async test', async () => { Simple sugar function for: ```js -jest.fn().mockReturnValueOnce(Promise.reject(value)); +jest.fn().mockImplementationOnce(() => Promise.reject(value)); ``` Example usage: diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 7d658ee5dab2..b62d81fae5fa 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -227,17 +227,6 @@ function getSlots(object?: Object): Array { return Object.keys(slots); } -function wrapAsyncParam( - fn: any => any, - asyncAction: 'resolve' | 'reject', -): any => any { - if (asyncAction === 'reject') { - return value => fn(Promise.reject(value)); - } - - return value => fn(Promise.resolve(value)); -} - class ModuleMockerClass { _environmentGlobal: Global; _mockState: WeakMap; @@ -418,12 +407,11 @@ class ModuleMockerClass { return f; }; - f.mockResolvedValueOnce = wrapAsyncParam( - f.mockReturnValueOnce, - 'resolve', - ); + f.mockResolvedValueOnce = value => + f.mockImplementationOnce(() => Promise.resolve(value)); - f.mockRejectedValueOnce = wrapAsyncParam(f.mockReturnValueOnce, 'reject'); + f.mockRejectedValueOnce = value => + f.mockImplementationOnce(() => Promise.reject(value)); f.mockReturnValue = value => { // next function call will return specified return value or this one @@ -433,9 +421,11 @@ class ModuleMockerClass { return f; }; - f.mockResolvedValue = wrapAsyncParam(f.mockReturnValue, 'resolve'); + f.mockResolvedValue = value => + f.mockImplementation(() => Promise.resolve(value)); - f.mockRejectedValue = wrapAsyncParam(f.mockReturnValue, 'reject'); + f.mockRejectedValue = value => + f.mockImplementation(() => Promise.reject(value)); f.mockImplementationOnce = fn => { // next function call will use this mock implementation return value