From 1d69b3b609c46335c9afbc93a9e679de022b9aff Mon Sep 17 00:00:00 2001 From: Carson Full Date: Wed, 24 Oct 2018 11:04:04 -0500 Subject: [PATCH] Jest - Fix returning message as string for custom matchers. Added tests. --- types/jest/index.d.ts | 9 ++++++++- types/jest/jest-tests.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 8451987011..2b351bbf70 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -345,7 +345,14 @@ declare namespace jest { } interface ExpectExtendMap { - [key: string]: (this: MatcherUtils, received: any, ...actual: any[]) => { message(): string | (() => string), pass: boolean } | Promise<{ message(): string, pass: boolean }>; + [key: string]: CustomMatcher; + } + + type CustomMatcher = (this: MatcherUtils, received: any, ...actual: any[]) => CustomMatcherResult | Promise; + + interface CustomMatcherResult { + pass: boolean; + message: string | (() => string); } interface SnapshotSerializerOptions { diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 4082bd9e31..18543b8f79 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -433,6 +433,14 @@ expect.extend({ }; } }); +expect.extend({ + foo(this: jest.MatcherUtils, received: {}, ...actual: Array<{}>) { + return { + message: JSON.stringify(received), + pass: false, + }; + } +}); expect.extend({ async foo(this: jest.MatcherUtils, received: {}, ...actual: Array<{}>) { return { @@ -441,6 +449,14 @@ expect.extend({ }; } }); +expect.extend({ + async foo(this: jest.MatcherUtils, received: {}, ...actual: Array<{}>) { + return { + message: JSON.stringify(received), + pass: false + }; + } +}); expect.extend({ foo(this: jest.MatcherUtils) { const isNot: boolean = this.isNot;