Skip to content

Commit e89ca3d

Browse files
committed
refactor(api): move isThenable to utils
1 parent 6083b31 commit e89ca3d

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/jest/utils/is-thenable.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
export function isThenable<T>(src: T | Promise<T>): src is Promise<T> {
2-
return src && typeof src === "object" && typeof (src as any).then === "function";
1+
/**
2+
* @internal
3+
*/
4+
export function isThenable<T>(value: T | Promise<T>): value is Promise<T> {
5+
return value && typeof value === "object" && "then" in value && typeof value.then === "function";
36
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { type CompatibilityOptions } from "./compatibility-check";
22
export { computeHash } from "./compute-hash";
33
export { interpolate } from "./interpolate";
4+
export { isThenable } from "./is-thenable";
45
export { requireUncached } from "./require-uncached";
56
export { ruleExists } from "./rule-exists";

src/utils/is-thenable.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isThenable } from "./is-thenable";
2+
3+
it("should return true if given a promise", () => {
4+
expect.assertions(1);
5+
const value = Promise.resolve("foo");
6+
expect(isThenable(value)).toBeTruthy();
7+
});
8+
9+
it("should return false if given a primitive value", () => {
10+
expect.assertions(1);
11+
const value = "foo";
12+
expect(isThenable(value)).toBeFalsy();
13+
});

src/utils/is-thenable.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Test if value is a Promise (thenable).
3+
*
4+
* @internal
5+
*/
6+
export function isThenable<T>(value: T | Promise<T>): value is Promise<T> {
7+
return value && typeof value === "object" && "then" in value && typeof value.then === "function";
8+
}

0 commit comments

Comments
 (0)