-
Notifications
You must be signed in to change notification settings - Fork 44
Property 'toHaveTextContent' does not exist on type 'Matchers<void> & ...' (and any other matcher from library) #136
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
Comments
Yeah, I am also experiencing this issue. Would be willing to help solve it. |
@alexamy Did you try installing It seems there are some conflicts with the Screen.Recording.2022-12-16.at.00.33.07.mov |
I installed the @types/jest at the same time as I used @jest/globals but no
dice. Are you suggesting that @types/jest would work where the recommended
globals would not? I did have this working prior to switching but it feels
like a step backwards to go from explicitly calling jest methods to going
back to globals.
I should mention that even when I try getting the matchers directly
(instead of importing them all at once during setup) it doesn’t see the
typescript there either, so you might be right (though i really don’t want
to have to undo all the work of explicitly using the globals).
…On Thu, Dec 15, 2022 at 7:34 PM Roni Castro ***@***.***> wrote:
@alexamy <https://github.com/alexamy> Did you try installing @types/jest
instead of using ***@***.***/globals"?
https://user-images.githubusercontent.com/24610813/208015970-3059ca21-444e-4109-8b96-bcf044af0bed.mov
—
Reply to this email directly, view it on GitHub
<#136 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHTRV3CPWTICBEAYTJROIDWNPPLLANCNFSM6AAAAAATAHBGOU>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
@worldlee78 It seems the declarations of this file are ignored when you import using Screen.Recording.2022-12-16.at.01.23.38.mov |
@roni-castro Thanks, I will try to use But I think that using implicit globals is a bad pratice because, according to Jest docs: Getting started with Typescript:
API:
|
UPD: After reloading editor, the error goes away. @roni-castro With
I've made a new branch with UPD: After reloading editor, the error goes away. |
Maybe we could have a look at some other 3rd party Jest marchers package written in TS and see how they are solving the issue. |
It looks like // Type definitions for @testing-library/jest-dom 5.14
// Project: https://github.com/testing-library/jest-dom
// Definitions by: Ernesto García <https://github.com/gnapse>
// John Gozde <https://github.com/jgoz>
// Seth Macpherson <https://github.com/smacpherson64>
// Andrew Leedham <https://github.com/AndrewLeedham>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 4.3
/// <reference types="jest" />
import { TestingLibraryMatchers } from './matchers';
declare global {
namespace jest {
interface Matchers<R = void, T = {}> extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
}
} |
So, the solution to the missing type definitions for matchers is to use Similar issue in Anyone feel free to investigate further, but for now I close this issue. |
Following this comment (thanks @yordis), this may lead to the solution: declare module '@jest/expect' {
interface Matchers<R extends void | Promise<void>> {
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;
}
} But it seems that this solution redefines already existing matchers from 'jest': Made a new branch in repro repo with additional definition file: |
@alexamy shoulden't TS theoretically merge the interfaces? https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-namespaces |
@mdjastrzebski Yes, but I think the reason for overwritting here is |
@mdjastrzebski Thanks for a docs! Module augmentaion is the answer. import { Matchers } from '@jest/expect';
declare module '@jest/expect' {
interface Matchers<R extends void | Promise<void>> {
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;
}
} |
And, matchers interface extension for import { Matchers } from '@jest/expect';
interface JestNativeMatchers<R extends void | Promise<void>> {
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;
}
// implicit jest globals, types coming from `@types/jest`
declare global {
namespace jest {
interface Matchers<R, T> extends JestNativeMatchers<R> {}
}
}
// explicit jest globals, types coming from `@jest/globals`
declare module '@jest/expect' {
interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
} Code example here. |
Shamelessly copied this into my
This stopped my ts from complaining, although I admit I don't understand why this works. |
Uh oh!
There was an error while loading. Please reload this page.
react-native
orexpo
:expo
@testing-library/react-native
version:11.5.0
jest-preset
:jest-expo
react-native
version:0.70.5
node
version:v16.17.1
@testing-library/jest-native
version:5.3.3
jest
version:29.3.0
Relevant code or config:
What you did:
Just using any matcher from
jest-native
.What happened:
Typescript complains for missed types. For example, for
toHaveTextContent
matcher:Reproduction:
https://github.com/alexamy/react-native-testing-library-missing-matchers
or the same code in zip archive:
react-native-testing-library-missing-matchers-45392ed44fe4fa3de64723609f6a0f74227188a4.zip
Problem description:
There is no way to use custom matchers in typescript tests for obvious reason. This problem can be 'avoided' by skipping matcher typecheck with
@ts-ignore
or optionally skipping test typecheck altogether, but it is not really a solution.Suggested solution:
Modify types in
@testing-library/jest-native/extend-expect.d.ts
to proper extend the matchers interface.Btw, there is also import problem on line 3.
Matchers interface from Jest:

I've tried to make
declare global
in project with extended jest'Matchers
interface, but it seems that something is missed, I didn't get any working result from my tries, this interface seems to be unchanged.Can you help us fix this issue by submitting a pull request?
I can make a Pull Request with solution for this problem if someone can point me how to properly modify definition file.
The text was updated successfully, but these errors were encountered: