Skip to content

Commit f613149

Browse files
aaronmcadamKent C. Dodds
authored and
Kent C. Dodds
committed
feat(typescript): add TypeScript typings (#16)
**What**: Attempts to fix #12. I've only added support for `getByTestId` to ensure that I've done things correctly. I wonder if there's an easy way to keep up to date with `dom-testing-library`? **Why**: For TypeScript support **How**: Added a `typings` directory and added a reference to `package.json`. I've tried to copy how cypress adds its own types for chainable methods and custom commands: https://github.com/cypress-io/add-cypress-custom-command-in-typescript/blob/master/README.md **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> * [ ] Documentation: add instructions on adding the library types to tsconfig.json * [ ] Tests * [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> * [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments -->
1 parent c1bce28 commit f613149

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-semantically-released",
44
"description": "Simple and complete custom Cypress commands and utilities that encourage good testing practices.",
55
"main": "dist/index.js",
6+
"typings": "typings",
67
"engines": {
78
"node": "> 6"
89
},
@@ -20,7 +21,8 @@
2021
"test:cypress:dev": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:open",
2122
"validate": "kcd-scripts validate build,lint,test",
2223
"setup": "npm install && npm run validate -s",
23-
"precommit": "kcd-scripts precommit"
24+
"precommit": "kcd-scripts precommit",
25+
"dtslint": "dtslint typings"
2426
},
2527
"files": [
2628
"dist",
@@ -43,6 +45,7 @@
4345
},
4446
"devDependencies": {
4547
"cypress": "^3.0.1",
48+
"dtslint": "^0.3.0",
4649
"kcd-scripts": "^0.37.0",
4750
"npm-run-all": "^4.1.2",
4851
"serve": "^10.0.0",

typings/index.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TypeScript Version: 2.8
2+
import {
3+
SelectorMatcherOptions,
4+
Matcher,
5+
MatcherOptions,
6+
getByTestId,
7+
} from 'dom-testing-library'
8+
9+
export namespace Cypress {
10+
type GetByAttribute = (id: Matcher, options?: MatcherOptions) => Chainable
11+
12+
type QueryByText = (
13+
id: Matcher,
14+
options?: SelectorMatcherOptions,
15+
) => Chainable
16+
17+
type AllByText = (id: Matcher, options?: SelectorMatcherOptions) => Chainable
18+
19+
type GetByText = (id: Matcher, options?: SelectorMatcherOptions) => Chainable
20+
21+
interface Chainable {
22+
getByPlaceholderText: GetByAttribute
23+
queryByText: QueryByText
24+
queryAllByText: AllByText
25+
getByText: GetByText
26+
getAllByText: AllByText
27+
queryByLabelText: QueryByText
28+
queryAllByLabelText: AllByText
29+
getByLabelText: GetByText
30+
getAllByLabelText: AllByText
31+
getByAltText: GetByAttribute
32+
getByTestId: GetByAttribute
33+
getByTitle: GetByAttribute
34+
getByValue: GetByAttribute
35+
getByTRole: GetByAttribute
36+
}
37+
}

typings/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6", "dom"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictFunctionTypes": true,
8+
"strictNullChecks": true,
9+
"noEmit": true,
10+
11+
"baseUrl": ".",
12+
"paths": {"cypress-testing-library": ["."]}
13+
}
14+
}

typings/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": [true, "never"],
5+
"whitespace": [false]
6+
}
7+
}

0 commit comments

Comments
 (0)