Skip to content

add documentation for using Matchers w/ Typescript #37

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

Merged
merged 1 commit into from
Apr 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ facilitate testing implementation details). Read more about this in
* [Custom Jest Matchers](#custom-jest-matchers)
* [`toBeInTheDOM`](#tobeinthedom)
* [`toHaveTextContent`](#tohavetextcontent)
* [Custom Jest Matchers - Typescript](#custom-jest-matchers-typescript)
* [`TextMatch`](#textmatch)
* [`query` APIs](#query-apis)
* [Examples](#examples)
Expand Down Expand Up @@ -343,6 +344,34 @@ expect(getByTestId('count-value')).toHaveTextContent('2')
expect(getByTestId('count-value')).not.toHaveTextContent('21')
// ...
```
### Custom Jest Matchers - Typescript

When you use custom Jest Matchers with Typescript, you will need to extend the type signature of `jest.Matchers<void>`, then cast the result of `expect` accordingly. Here's a handy usage example:

```typescript
// this adds custom expect matchers
import 'react-testing-library/extend-expect';
interface ExtendedMatchers extends jest.Matchers<void> {
toHaveTextContent: (htmlElement: string) => object;
toBeInTheDOM: () => void;
}
test('renders the tooltip as expected', async () => {
const {
// getByLabelText,
getByText,
// getByTestId,
container
} = render(<Tooltip label="hello world">Child</Tooltip>);
// tests rendering of the child
getByText('Child');
// tests rendering of tooltip label
(expect(getByText('hello world')) as ExtendedMatchers).toHaveTextContent(
'hello world'
);
// snapshots work great with regular DOM nodes!
expect(container.firstChild).toMatchSnapshot();
});
```

## `TextMatch`

Expand Down