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: correct isPromise implementation #13314

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import {equals, iterableEquality, subsetEquality} from '@jest/expect-utils';
import * as matcherUtils from 'jest-matcher-utils';
import {isPromise} from 'jest-util';
import {
any,
anything,
Expand Down Expand Up @@ -69,12 +70,6 @@ export class JestAssertionError extends Error {
matcherResult?: Omit<SyncExpectationResult, 'message'> & {message: string};
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
const isPromise = <T extends any>(obj: any): obj is PromiseLike<T> =>
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';

const createToThrowErrorMatchingSnapshotMatcher = function (
matcher: RawMatcherFn,
) {
Expand Down
15 changes: 7 additions & 8 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import slash = require('slash');
import StackUtils = require('stack-utils');
import type {AssertionResult, Status} from '@jest/test-result';
import type {Circus, Global} from '@jest/types';
import {ErrorWithStack, convertDescriptorToString, formatTime} from 'jest-util';
import {
ErrorWithStack,
convertDescriptorToString,
formatTime,
isPromise,
} from 'jest-util';
import {format as prettyFormat} from 'pretty-format';
import {ROOT_DESCRIBE_BLOCK_NAME, getState} from './state';

Expand Down Expand Up @@ -266,13 +271,7 @@ export const callAsyncCircusFn = (
}
}

// If it's a Promise, return it. Test for an object with a `then` function
// to support custom Promise implementations.
if (
typeof returnedValue === 'object' &&
returnedValue !== null &&
typeof returnedValue.then === 'function'
) {
if (isPromise(returnedValue)) {
returnedValue.then(() => resolve(), reject);
return;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ import co from 'co';
import isGeneratorFn from 'is-generator-fn';
import pLimit = require('p-limit');
import type {Config, Global} from '@jest/types';
import {isPromise} from 'jest-util';
import isError from './isError';
import type Spec from './jasmine/Spec';
import type {DoneFn, QueueableFn} from './queueRunner';
import type {Jasmine} from './types';

function isPromise(obj: any): obj is PromiseLike<unknown> {
return obj && typeof obj.then === 'function';
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
const doneFnNoop = () => {};

Expand Down
10 changes: 4 additions & 6 deletions packages/jest-util/src/isPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

// capture globalThis.Promise before it may potentially be overwritten
const Promise = globalThis.Promise;

// see ES2015 spec 25.4.4.5, https://stackoverflow.com/a/38339199
const isPromise = (candidate: unknown): candidate is Promise<unknown> =>
Promise.resolve(candidate) === candidate;
const isPromise = (candidate: unknown): candidate is PromiseLike<unknown> =>
candidate != null &&
(typeof candidate === 'object' || typeof candidate === 'function') &&
typeof (candidate as any).then === 'function';
Copy link
Contributor

@mrazauskas mrazauskas Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps? Just to avoid any? Or the point is to be explicit that this is still any?

Suggested change
typeof (candidate as any).then === 'function';
typeof (candidate as PromiseLike<unknown>).then === 'function';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cast would lie - I just want to be able to access a prop. I guess as Record<string, unknown> | (...args: unknown[]) => unknown or something is more precise looking at the typeof checks. But not really, as typeof new Map === 'object'. Just saying any seems cleanest

export default isPromise;
6 changes: 1 addition & 5 deletions packages/jest-worker/src/workers/processChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {isPromise} from 'jest-util';
import {
CHILD_MESSAGE_CALL,
CHILD_MESSAGE_END,
Expand Down Expand Up @@ -158,11 +159,6 @@ function execMethod(method: string, args: Array<unknown>): void {
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}

const isPromise = (obj: any): obj is PromiseLike<unknown> =>
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';

function execFunction(
fn: UnknownFunction,
ctx: unknown,
Expand Down
6 changes: 1 addition & 5 deletions packages/jest-worker/src/workers/threadChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {isMainThread, parentPort} from 'worker_threads';
import {isPromise} from 'jest-util';
import {
CHILD_MESSAGE_CALL,
CHILD_MESSAGE_END,
Expand Down Expand Up @@ -160,11 +161,6 @@ function execMethod(method: string, args: Array<unknown>): void {
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}

const isPromise = (obj: any): obj is PromiseLike<unknown> =>
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';

function execFunction(
fn: UnknownFunction,
ctx: unknown,
Expand Down