mirror of https://github.com/facebook/jest.git
fix(jest-mock): ensure mock resolved and rejected values are promises from correct realm (#13503)
This commit is contained in:
parent
610b280254
commit
5fea130e05
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
|
@ -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');
|
||||||
|
});
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"jest": {
|
||||||
|
"testEnvironment": "node"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue