Skip to content

Commit

Permalink
fix: change extend-expect types definition to support explicit jest g…
Browse files Browse the repository at this point in the history
…lobals (#137)

Co-authored-by: Maciej Jastrzebski <[email protected]>
  • Loading branch information
alexamy and mdjastrzebski authored Dec 29, 2022
1 parent fafd6ed commit c3dc5ce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
38 changes: 23 additions & 15 deletions extend-expect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> {
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<ViewStyle | TextStyle | ImageStyle>): 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<R, T> {
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<ViewStyle | TextStyle | ImageStyle>): R;
toHaveAccessibilityState(state: AccessibilityState): R;
toHaveAccessibilityValue(state: AccessibilityValueMatcher): R;

/** @deprecated This function has been renamed to `toBeEmptyElement`. */
toBeEmpty(): R;
}
interface Matchers<R, T = {}> extends JestNativeMatchers<R> {}
}
}

// Explicit `@jest/globals` `expect` matchers.
declare module '@jest/expect' {
interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module.exports = {
testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
setupFilesAfterEnv: ['<rootDir>/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__'],
};
15 changes: 15 additions & 0 deletions src/__types__/jest-explicit-extend.test-d.ts
Original file line number Diff line number Diff line change
@@ -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({});
12 changes: 12 additions & 0 deletions src/__types__/jest-implicit-extend.test-d.ts
Original file line number Diff line number Diff line change
@@ -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({});

0 comments on commit c3dc5ce

Please sign in to comment.