fix(jest-mock): ensure mock resolved and rejected values are promises from correct realm (#13503)

This commit is contained in:
Simen Bekkhus 2022-10-24 11:30:23 +02:00 committed by GitHub
parent 610b280254
commit 5fea130e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 6 deletions

View File

@ -5,6 +5,7 @@
### Fixes ### Fixes
- `[@jest/test-sequencer]` Make sure sharding does not produce empty groups ([#13476](https://github.com/facebook/jest/pull/13476)) - `[@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 ### Chore & Maintenance

View File

@ -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);
});

View File

@ -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');
});

View File

@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}

View File

@ -652,7 +652,7 @@ describe('moduleMocker', () => {
const promise = fn(); const promise = fn();
expect(promise).toBeInstanceOf(Promise); expect(promise).toBeInstanceOf(mockGlobals.Promise);
return expect(promise).resolves.toBe('abcd'); return expect(promise).resolves.toBe('abcd');
}); });
@ -675,7 +675,7 @@ describe('moduleMocker', () => {
const promise = fn(); const promise = fn();
expect(promise).toBeInstanceOf(Promise); expect(promise).toBeInstanceOf(mockGlobals.Promise);
return expect(promise).rejects.toBe(err); return expect(promise).rejects.toBe(err);
}); });

View File

@ -768,20 +768,28 @@ export class ModuleMocker {
f.mockImplementationOnce(() => value); f.mockImplementationOnce(() => value);
f.mockResolvedValueOnce = (value: ResolveType<T>) => f.mockResolvedValueOnce = (value: ResolveType<T>) =>
f.mockImplementationOnce(() => Promise.resolve(value)); f.mockImplementationOnce(() =>
this._environmentGlobal.Promise.resolve(value),
);
f.mockRejectedValueOnce = (value: unknown) => f.mockRejectedValueOnce = (value: unknown) =>
f.mockImplementationOnce(() => Promise.reject(value)); f.mockImplementationOnce(() =>
this._environmentGlobal.Promise.reject(value),
);
f.mockReturnValue = (value: ReturnType<T>) => f.mockReturnValue = (value: ReturnType<T>) =>
// next function call will return specified return value or this one // next function call will return specified return value or this one
f.mockImplementation(() => value); f.mockImplementation(() => value);
f.mockResolvedValue = (value: ResolveType<T>) => f.mockResolvedValue = (value: ResolveType<T>) =>
f.mockImplementation(() => Promise.resolve(value)); f.mockImplementation(() =>
this._environmentGlobal.Promise.resolve(value),
);
f.mockRejectedValue = (value: unknown) => f.mockRejectedValue = (value: unknown) =>
f.mockImplementation(() => Promise.reject(value)); f.mockImplementation(() =>
this._environmentGlobal.Promise.reject(value),
);
f.mockImplementationOnce = (fn: UnknownFunction) => { f.mockImplementationOnce = (fn: UnknownFunction) => {
// next function call will use this mock implementation return value // next function call will use this mock implementation return value