Skip to content

Commit

Permalink
feat(typescript): Provide typescript definitions (#11)
Browse files Browse the repository at this point in the history
* Provide typescript definitions

* Move typescript definitions to root dir

See #11 (comment)

* Add note about typescript caveat
  • Loading branch information
gnapse committed May 15, 2018
1 parent 7cdb616 commit bbdd96c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ to maintain.
* [`toHaveTextContent`](#tohavetextcontent)
* [`toHaveAttribute`](#tohaveattribute)
* [`toHaveClass`](#tohaveclass)
* [Using with Typescript](#using-with-typescript)
* [Inspiration](#inspiration)
* [Other Solutions](#other-solutions)
* [Guiding Principles](#guiding-principles)
Expand Down Expand Up @@ -84,6 +83,9 @@ import {toBeInTheDOM, toHaveClass} from 'jest-dom'
expect.extend({toBeInTheDOM, toHaveClass})
```

> Note: when using TypeScript, this way of importing matchers won't provide the
> necessary type definitions. More on this [here](https://github.com/gnapse/jest-dom/pull/11#issuecomment-387817459).
## Custom matchers

### `toBeInTheDOM`
Expand Down Expand Up @@ -161,23 +163,6 @@ expect(getByTestId(container, 'delete-button')).not.toHaveClass('btn-link')
// ...
```

### Using with Typescript

When you use custom Jest Matchers with Typescript, you can extend `jest.Matchers`
interface provided by `@types/jest` package by creating `.d.ts` file somewhere
in your project with following content:

```typescript
declare namespace jest {
interface Matchers<R> {
toHaveAttribute: (attr: string, value?: string) => R
toHaveTextContent: (htmlElement: string) => R
toHaveClass: (className: string) => R
toBeInTheDOM: () => R
}
}
```

## Inspiration

This whole library was extracted out of Kent C. Dodds' [dom-testing-library][],
Expand Down
8 changes: 8 additions & 0 deletions extend-expect.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare namespace jest {
interface Matchers<R> {
toHaveAttribute: (attr: string, value?: string) => R
toHaveTextContent: (text: string) => R
toHaveClass: (className: string) => R
toBeInTheDOM: () => R
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"files": [
"dist",
"extend-expect.js"
"extend-expect.js",
"extend-expect.d.ts"
],
"keywords": [
"testing",
Expand Down

0 comments on commit bbdd96c

Please sign in to comment.