Skip to content

Commit

Permalink
fix(jest-mock): ensure mock resolved and rejected values are promises…
Browse files Browse the repository at this point in the history
… from correct realm (#13503)
  • Loading branch information
SimenB committed Oct 24, 2022
1 parent 610b280 commit 5fea130
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions 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);
});
19 changes: 19 additions & 0 deletions 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');
});
5 changes: 5 additions & 0 deletions e2e/mock-functions/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
4 changes: 2 additions & 2 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -652,7 +652,7 @@ describe('moduleMocker', () => {

const promise = fn();

expect(promise).toBeInstanceOf(Promise);
expect(promise).toBeInstanceOf(mockGlobals.Promise);

return expect(promise).resolves.toBe('abcd');
});
Expand All @@ -675,7 +675,7 @@ describe('moduleMocker', () => {

const promise = fn();

expect(promise).toBeInstanceOf(Promise);
expect(promise).toBeInstanceOf(mockGlobals.Promise);

return expect(promise).rejects.toBe(err);
});
Expand Down
16 changes: 12 additions & 4 deletions packages/jest-mock/src/index.ts
Expand Up @@ -768,20 +768,28 @@ export class ModuleMocker {
f.mockImplementationOnce(() => value);

f.mockResolvedValueOnce = (value: ResolveType<T>) =>
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<T>) =>
// next function call will return specified return value or this one
f.mockImplementation(() => value);

f.mockResolvedValue = (value: ResolveType<T>) =>
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
Expand Down

0 comments on commit 5fea130

Please sign in to comment.