Skip to content

Commit bbdd96c

Browse files
authored
feat(typescript): Provide typescript definitions (#11)
* Provide typescript definitions * Move typescript definitions to root dir See #11 (comment) * Add note about typescript caveat
1 parent 7cdb616 commit bbdd96c

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

README.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ to maintain.
4646
* [`toHaveTextContent`](#tohavetextcontent)
4747
* [`toHaveAttribute`](#tohaveattribute)
4848
* [`toHaveClass`](#tohaveclass)
49-
* [Using with Typescript](#using-with-typescript)
5049
* [Inspiration](#inspiration)
5150
* [Other Solutions](#other-solutions)
5251
* [Guiding Principles](#guiding-principles)
@@ -84,6 +83,9 @@ import {toBeInTheDOM, toHaveClass} from 'jest-dom'
8483
expect.extend({toBeInTheDOM, toHaveClass})
8584
```
8685

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

8991
### `toBeInTheDOM`
@@ -161,23 +163,6 @@ expect(getByTestId(container, 'delete-button')).not.toHaveClass('btn-link')
161163
// ...
162164
```
163165

164-
### Using with Typescript
165-
166-
When you use custom Jest Matchers with Typescript, you can extend `jest.Matchers`
167-
interface provided by `@types/jest` package by creating `.d.ts` file somewhere
168-
in your project with following content:
169-
170-
```typescript
171-
declare namespace jest {
172-
interface Matchers<R> {
173-
toHaveAttribute: (attr: string, value?: string) => R
174-
toHaveTextContent: (htmlElement: string) => R
175-
toHaveClass: (className: string) => R
176-
toBeInTheDOM: () => R
177-
}
178-
}
179-
```
180-
181166
## Inspiration
182167

183168
This whole library was extracted out of Kent C. Dodds' [dom-testing-library][],

extend-expect.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare namespace jest {
2+
interface Matchers<R> {
3+
toHaveAttribute: (attr: string, value?: string) => R
4+
toHaveTextContent: (text: string) => R
5+
toHaveClass: (className: string) => R
6+
toBeInTheDOM: () => R
7+
}
8+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
},
2121
"files": [
2222
"dist",
23-
"extend-expect.js"
23+
"extend-expect.js",
24+
"extend-expect.d.ts"
2425
],
2526
"keywords": [
2627
"testing",

0 commit comments

Comments
 (0)