From c3dc5ce4ba6f9c8eaae391971fd442940f6d1090 Mon Sep 17 00:00:00 2001 From: Alexander Chaschin Date: Thu, 29 Dec 2022 15:09:17 +0300 Subject: [PATCH] fix: change extend-expect types definition to support explicit jest globals (#137) Co-authored-by: Maciej Jastrzebski --- extend-expect.d.ts | 38 ++++++++++++-------- jest.config.js | 2 +- src/__types__/jest-explicit-extend.test-d.ts | 15 ++++++++ src/__types__/jest-implicit-extend.test-d.ts | 12 +++++++ 4 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 src/__types__/jest-explicit-extend.test-d.ts create mode 100644 src/__types__/jest-implicit-extend.test-d.ts diff --git a/extend-expect.d.ts b/extend-expect.d.ts index 35e7722..666d8da 100644 --- a/extend-expect.d.ts +++ b/extend-expect.d.ts @@ -2,23 +2,31 @@ import type { AccessibilityState, ImageStyle, StyleProp, TextStyle, ViewStyle } import type { ReactTestInstance } from 'react-test-renderer'; import type { AccessibilityValueMatcher } from './src/to-have-accessibility-value'; +export interface JestNativeMatchers { + toBeDisabled(): R; + toBeEmptyElement(): R; + toBeEnabled(): R; + toBeVisible(): R; + toContainElement(element: ReactTestInstance | null): R; + toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R; + toHaveProp(attr: string, value?: unknown): R; + toHaveStyle(style: StyleProp): R; + toHaveAccessibilityState(state: AccessibilityState): R; + toHaveAccessibilityValue(value: AccessibilityValueMatcher): R; + + /** @deprecated This function has been renamed to `toBeEmptyElement`. */ + toBeEmpty(): R; +} + +// Implicit Jest global `expect`. declare global { namespace jest { // eslint-disable-next-line @typescript-eslint/no-unused-vars - interface Matchers { - toBeDisabled(): R; - toBeEmptyElement(): R; - toBeEnabled(): R; - toBeVisible(): R; - toContainElement(element: ReactTestInstance | null): R; - toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R; - toHaveProp(attr: string, value?: unknown): R; - toHaveStyle(style: StyleProp): R; - toHaveAccessibilityState(state: AccessibilityState): R; - toHaveAccessibilityValue(state: AccessibilityValueMatcher): R; - - /** @deprecated This function has been renamed to `toBeEmptyElement`. */ - toBeEmpty(): R; - } + interface Matchers extends JestNativeMatchers {} } } + +// Explicit `@jest/globals` `expect` matchers. +declare module '@jest/expect' { + interface Matchers> extends JestNativeMatchers {} +} diff --git a/jest.config.js b/jest.config.js index e779a2b..58132ec 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,6 +3,6 @@ module.exports = { testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'], setupFilesAfterEnv: ['/setup-tests.ts'], snapshotSerializers: ['@relmify/jest-serializer-strip-ansi/always'], - collectCoverageFrom: ['src/**/*.+(js|jsx|ts|tsx)'], + collectCoverageFrom: ['src/**/*.(js|jsx|ts|tsx)', '!src/**/*.test-d.(ts|tsx)'], testPathIgnorePatterns: ['/node_modules/', '/__tests__/helpers/', '/dist/', '__mocks__'], }; diff --git a/src/__types__/jest-explicit-extend.test-d.ts b/src/__types__/jest-explicit-extend.test-d.ts new file mode 100644 index 0000000..989e04b --- /dev/null +++ b/src/__types__/jest-explicit-extend.test-d.ts @@ -0,0 +1,15 @@ +// This file checks whether explicit Jest `extend` from '@jest/expect' is correctly extended with Jest Matchers. + +// eslint-disable-next-line import/no-extraneous-dependencies +import { expect as jestExpect } from '@jest/globals'; + +jestExpect(null).toBeDisabled(); +jestExpect(null).toBeEmptyElement(); +jestExpect(null).toBeEnabled(); +jestExpect(null).toBeVisible(); +jestExpect(null).toContainElement(null); +jestExpect(null).toHaveTextContent(''); +jestExpect(null).toHaveProp('foo'); +jestExpect(null).toHaveStyle({}); +jestExpect(null).toHaveAccessibilityState({}); +jestExpect(null).toHaveAccessibilityValue({}); diff --git a/src/__types__/jest-implicit-extend.test-d.ts b/src/__types__/jest-implicit-extend.test-d.ts new file mode 100644 index 0000000..70609d2 --- /dev/null +++ b/src/__types__/jest-implicit-extend.test-d.ts @@ -0,0 +1,12 @@ +// This file checks whether implicit Jest `extend` is correctly extended with Jest Matchers. + +expect(null).toBeDisabled(); +expect(null).toBeEmptyElement(); +expect(null).toBeEnabled(); +expect(null).toBeVisible(); +expect(null).toContainElement(null); +expect(null).toHaveTextContent(''); +expect(null).toHaveProp('foo'); +expect(null).toHaveStyle({}); +expect(null).toHaveAccessibilityState({}); +expect(null).toHaveAccessibilityValue({});