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

chore: extend and improve type tests for jest object #12442

Merged
merged 9 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ export class ModuleMocker {
isMockFunction<T, Y extends Array<unknown> = Array<unknown>>(
fn: (...args: Y) => T,
): fn is Mock<T, Y>;
isMockFunction(fn: unknown): boolean;
isMockFunction(fn: unknown): fn is Mock<unknown>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SimenB Might be this is better idea. See tests. Thanks for good questions!

Copy link
Member

Choose a reason for hiding this comment

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

perfect, thanks!

isMockFunction<T>(fn: unknown): fn is Mock<T> {
return !!fn && (fn as any)._isMockFunction === true;
}
Expand Down
30 changes: 15 additions & 15 deletions packages/jest-types/__typetests__/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,34 @@ if (!jest.isMockFunction(surelyMock)) {
expectType<never>(surelyMock);
}

declare const stringNotMock: string;
declare const stringMaybeMock: string;

if (!jest.isMockFunction(stringNotMock)) {
expectType<string>(stringNotMock);
if (!jest.isMockFunction(stringMaybeMock)) {
expectType<string>(stringMaybeMock);
}

if (jest.isMockFunction(stringNotMock)) {
expectType<string>(stringNotMock);
if (jest.isMockFunction(stringMaybeMock)) {
expectType<string & Mock<unknown, Array<unknown>>>(stringMaybeMock);
}

declare const anyNotMock: any;
declare const anyMaybeMock: any;

if (!jest.isMockFunction(anyNotMock)) {
expectType<any>(anyNotMock);
if (!jest.isMockFunction(anyMaybeMock)) {
expectType<any>(anyMaybeMock);
}

if (jest.isMockFunction(anyNotMock)) {
expectType<any>(anyNotMock);
if (jest.isMockFunction(anyMaybeMock)) {
expectType<Mock<unknown, Array<unknown>>>(anyMaybeMock);
}

declare const unknownNotMock: unknown;
declare const unknownMaybeMock: unknown;

if (!jest.isMockFunction(unknownNotMock)) {
expectType<unknown>(unknownNotMock);
if (!jest.isMockFunction(unknownMaybeMock)) {
expectType<unknown>(unknownMaybeMock);
}

if (jest.isMockFunction(unknownNotMock)) {
expectType<unknown>(unknownNotMock);
if (jest.isMockFunction(unknownMaybeMock)) {
expectType<Mock<unknown, Array<unknown>>>(unknownMaybeMock);
}

expectType<Mock<unknown>>(jest.fn());
Expand Down