Skip to content
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

TypeScript fails to find extended expect methods #7

Closed
lewie9021 opened this issue Jun 11, 2019 · 9 comments · Fixed by #9
Closed

TypeScript fails to find extended expect methods #7

lewie9021 opened this issue Jun 11, 2019 · 9 comments · Fixed by #9
Labels

Comments

@lewie9021
Copy link

lewie9021 commented Jun 11, 2019

  • react-native or expo: 0.59.2
  • @testing-library/react-native version: 4.0.2
  • @testing-library/jest-native version: 3.0.1
  • jest-preset: "@testing-library/react-native"
  • react-native version: 0.59.2
  • typescript version: 3.1.6
  • node version: 10.13.0

Relevant code or config:

import * as React from "react";
import { Text } from "react-native";
import { render } from "@testing-library/react-native";

function Example() {
  return (
    <Text testID="text">2</Text>
  );
}

it("works without TypeScript errors", () => {
  const { getByTestId } = render(<Example />);

  expect(getByTestId("text")).toHaveTextContent("2");
});

What you did:

Installed jest-native within a project using TypeScript.

What happened:

I get a TypeScript error when using any of the custom matchers from the library, e.g:

Screenshot 2019-06-11 at 16 12 35

Error:(14, 31) TS2339: Property 'toHaveTextContent' does not exist on type 'Matchers<Pick<ReactTestInstance, "type" | "props" | "parent" | "children" | "find" | "findAll">>'.

Reproduction:

See code above.

Problem description:

Without the correct typings, it's not possible to use the additional assertions from the library within a TypeScript project.

Suggested solution:

Since Jest exposes expect globally, it appears we need to merge the matchers in the global namespace, meaning extend-expect.d.ts should look like this:

import { ReactTestInstance } from 'react-test-renderer';

declare global {
  namespace jest {
    interface Matchers<R> {
      toBeDisabled(): R;
      toContainElement(element: ReactTestInstance | null): R;
      toBeEmpty(): R;
      toHaveProp(attr: string, value?: any): R;
      toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
      toBeEnabled(): R;
      toHaveStyle(style: object[] | object): R;
    }
  }
}
@bcarroll22
Copy link
Collaborator

Hey thanks for pointing this out. I think that it makes sense to me, but my only question is, this is how the matchers are typed in jest-dom as well, so do you think this a problem there as well? I couldn't find any issues about this on their repo and that one is widely used by more than just React folks, so are we positive this isn't something else at play?

Admittedly, I don't know why they did it that way over there (perhaps @gnapse could help me out with this one) but it seems like since it works there it should work here? 🤔

@lewie9021
Copy link
Author

lewie9021 commented Jun 12, 2019

That's a valid point. I've not used react-testing-library or jest-dom yet, but like you say - if that works so should this. My solution came from an issue in the react-testing-library project here. It's probably worth noting that I'm using v3.1.6 of TypeScript with this library, so that might be part of the issue

@bcarroll22
Copy link
Collaborator

@lewie9021 what's the verdict here? what do you think we should do?

@marnusw
Copy link
Contributor

marnusw commented Jul 14, 2019

I believe the import changes the definition from global augmentation to a module definition. In jest-dom there is no import and as such they too are augmenting the global namespace. I've confirmed the suggested solution works and have created a PR #9 for it.

@bcarroll22
Copy link
Collaborator

🎉 This issue has been resolved in version 3.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sinxwal
Copy link

sinxwal commented Jul 29, 2019

I think that error was not fixed in 3.0.2

@bcarroll22
Copy link
Collaborator

🤔 can anyone else chime in? Or if anyone can think of a better fix for the issue, PRs are welcome 👍

@marnusw
Copy link
Contributor

marnusw commented Jul 31, 2019

For the record I'm using version 3.0.2 without any further issues. It might help to understand your setup better @sinxwal.

@alexandermckay
Copy link

alexandermckay commented Nov 11, 2019

I am still getting the issue in version 3.0.2

EDIT: After changing my setupFiles.js file to setupFiles.ts the warning went away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants