diff --git a/CHANGELOG.md b/CHANGELOG.md index 0db12a656313..ca6c0a550e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[@jest/test-sequencer]` Make sure sharding does not produce empty groups ([#13476](https://github.com/facebook/jest/pull/13476)) +- `[jest-mock]` Ensure mock resolved and rejected values are promises from correct realm ([#13503](https://github.com/facebook/jest/pull/13503)) ### Chore & Maintenance diff --git a/e2e/__tests__/mockFunctions.test.ts b/e2e/__tests__/mockFunctions.test.ts new file mode 100644 index 000000000000..2951b0fb51cc --- /dev/null +++ b/e2e/__tests__/mockFunctions.test.ts @@ -0,0 +1,14 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import runJest from '../runJest'; + +test('supports instanceof Promise', () => { + const {exitCode} = runJest('mock-functions'); + + expect(exitCode).toBe(0); +}); diff --git a/e2e/mock-functions/__tests__/index.js b/e2e/mock-functions/__tests__/index.js new file mode 100644 index 000000000000..31e9f75c4e49 --- /dev/null +++ b/e2e/mock-functions/__tests__/index.js @@ -0,0 +1,19 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +test('instanceof promise', async () => { + const resolvedPromise = jest.fn().mockResolvedValue('hello')(); + const rejectedPromise = jest.fn().mockRejectedValue('hello')(); + + expect(resolvedPromise).toBeInstanceOf(Promise); + expect(rejectedPromise).toBeInstanceOf(Promise); + + await expect(resolvedPromise).resolves.toBe('hello'); + await expect(rejectedPromise).rejects.toBe('hello'); +}); diff --git a/e2e/mock-functions/package.json b/e2e/mock-functions/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/mock-functions/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 71680852eca7..4d134554108f 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -652,7 +652,7 @@ describe('moduleMocker', () => { const promise = fn(); - expect(promise).toBeInstanceOf(Promise); + expect(promise).toBeInstanceOf(mockGlobals.Promise); return expect(promise).resolves.toBe('abcd'); }); @@ -675,7 +675,7 @@ describe('moduleMocker', () => { const promise = fn(); - expect(promise).toBeInstanceOf(Promise); + expect(promise).toBeInstanceOf(mockGlobals.Promise); return expect(promise).rejects.toBe(err); }); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 3c0b6f108bd5..b74d14f53f16 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -768,20 +768,28 @@ export class ModuleMocker { f.mockImplementationOnce(() => value); f.mockResolvedValueOnce = (value: ResolveType) => - f.mockImplementationOnce(() => Promise.resolve(value)); + f.mockImplementationOnce(() => + this._environmentGlobal.Promise.resolve(value), + ); f.mockRejectedValueOnce = (value: unknown) => - f.mockImplementationOnce(() => Promise.reject(value)); + f.mockImplementationOnce(() => + this._environmentGlobal.Promise.reject(value), + ); f.mockReturnValue = (value: ReturnType) => // next function call will return specified return value or this one f.mockImplementation(() => value); f.mockResolvedValue = (value: ResolveType) => - f.mockImplementation(() => Promise.resolve(value)); + f.mockImplementation(() => + this._environmentGlobal.Promise.resolve(value), + ); f.mockRejectedValue = (value: unknown) => - f.mockImplementation(() => Promise.reject(value)); + f.mockImplementation(() => + this._environmentGlobal.Promise.reject(value), + ); f.mockImplementationOnce = (fn: UnknownFunction) => { // next function call will use this mock implementation return value