Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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