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({});