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

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Sep 23, 2022

Summary

The linked SO post says not to use it 🙈

Test plan

Green CI

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

@@ -23,3 +23,12 @@ test('a resolved Promise', () => {
test('a rejected Promise', () => {
expect(isPromise(Promise.reject().catch(() => {}))).toBe(true);
});

test('a thenable', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

this test fails with the old implementation, fwiw

@SimenB SimenB merged commit 2e608c1 into jestjs:main Sep 23, 2022
@SimenB SimenB deleted the fix-is-promise branch September 23, 2022 13:34
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants