Skip to content

Commit d782f03

Browse files
Airat AminevKent C. Dodds
authored andcommitted
fix(TS): fix types and add tests for types (#21)
<!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: - fixed TypeScript types - added jsdoc for helpers - added tests for types <!-- Why are these changes necessary? --> **Why**: types were not working reported here #18 (comment) by @aaronmcadam and @freddydumont <!-- How were these changes implemented? --> **How**: - added and fixed types for the helpers into Cypress namespace using global declaration as described in TS docs https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html - for testing - added `tsc --noemit` on sample ts project <!-- Have you done all of these things? --> **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" --> * [x] Documentation * [x] Tests * [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> * [x] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments --> This is how types being picked up by VS-Code: ![image](https://user-images.githubusercontent.com/4506749/47946634-840d6d80-decb-11e8-9be7-62fb2a68526e.png) ![image](https://user-images.githubusercontent.com/4506749/47946573-9e931700-deca-11e8-94e0-4fd6bd70ce4c.png)
1 parent d530690 commit d782f03

File tree

6 files changed

+801
-31
lines changed

6 files changed

+801
-31
lines changed

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050
"ideas",
5151
"test"
5252
]
53+
},
54+
{
55+
"login": "airato",
56+
"name": "Airat Aminev",
57+
"avatar_url": "https://avatars3.githubusercontent.com/u/4506749?v=4",
58+
"profile": "https://github.com/airato",
59+
"contributions": [
60+
"code",
61+
"test",
62+
"tool"
63+
]
5364
}
5465
],
5566
"repoType": "github"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -100,8 +100,8 @@ Thanks goes to these people ([emoji key][emojis]):
100100

101101
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
102102
<!-- prettier-ignore -->
103-
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Documentation") [🤔](#ideas-npeterkamps "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Tests") |
104-
| :---: | :---: | :---: | :---: |
103+
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Documentation") [🤔](#ideas-npeterkamps "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Tests") | [<img src="https://avatars3.githubusercontent.com/u/4506749?v=4" width="100px;"/><br /><sub><b>Airat Aminev</b></sub>](https://github.com/airato)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=airato "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=airato "Tests") [🔧](#tool-airato "Tools") |
104+
| :---: | :---: | :---: | :---: | :---: |
105105

106106
<!-- ALL-CONTRIBUTORS-LIST:END -->
107107

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
"add-contributor": "kcd-scripts contributors add",
1212
"build": "kcd-scripts build",
1313
"lint": "kcd-scripts lint",
14-
"test": "npm-run-all --parallel test:unit test:cypress",
14+
"test": "npm-run-all --parallel test:unit test:cypress test:typings",
1515
"test:unit": "kcd-scripts test --no-watch",
1616
"test:unit:watch": "kcd-scripts test",
1717
"test:cypress:serve": "serve --listen 13370 ./cypress/fixtures/test-app",
1818
"test:cypress:run": "wait-port --timeout 10000 localhost:13370 && cypress run",
1919
"test:cypress:open": "cypress open",
2020
"test:cypress": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:run",
2121
"test:cypress:dev": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:open",
22+
"test:typings": "tsc -p tests/typescript-types/ --noemit",
23+
"test:typings:watch": "tsc -p tests/typescript-types/ --noemit --watch",
2224
"validate": "kcd-scripts validate build,lint,test",
2325
"setup": "npm install && npm run validate -s",
2426
"precommit": "kcd-scripts precommit",
@@ -50,6 +52,7 @@
5052
"kcd-scripts": "^0.37.0",
5153
"npm-run-all": "^4.1.2",
5254
"serve": "^10.0.0",
55+
"typescript": "^3.1.6",
5356
"wait-port": "^0.2.2"
5457
},
5558
"peerDependencies": {

tests/typescript-types/test.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
describe('Foo', () => {
2+
it('has proper types', () => {
3+
cy.visit('#/foo')
4+
5+
cy.getAllByPlaceholderText('foo').should(elements => {
6+
// argument should be an array of HTML elements
7+
expect(elements.length).to.eq(0)
8+
expect(elements[0].tagName).to.eq(0)
9+
})
10+
11+
// with regex
12+
cy.queryByPlaceholderText<'a'>(/foo/).should(element => {
13+
// node can be null
14+
if (element) {
15+
// argument should be an anchor
16+
expect(element.href).to.eq('')
17+
}
18+
})
19+
20+
// with matcher function
21+
const matcherFn = (content: string, element: HTMLElement) => true
22+
23+
cy.queryByPlaceholderText<HTMLAudioElement>(matcherFn).should(element => {
24+
// node can be null
25+
if (element) {
26+
// argument should be an Audio element
27+
expect(element.play).to.exist
28+
}
29+
})
30+
31+
// with matcher options
32+
cy.queryAllByPlaceholderText<HTMLAudioElement>('foo', {
33+
collapseWhitespace: true,
34+
exact: true,
35+
trim: true,
36+
}).should(elements => {
37+
const el = elements[0]
38+
// node can be null
39+
if (el) {
40+
// argument should be an array of Audio elements
41+
expect(el.play).to.exist
42+
}
43+
})
44+
})
45+
})

tests/typescript-types/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["dom", "es2015", "es2016", "es2017"],
4+
"target": "es2017",
5+
"module": "esnext",
6+
"moduleResolution": "node",
7+
"experimentalDecorators": true,
8+
"outDir": "dist",
9+
"noUnusedLocals": true,
10+
"noImplicitAny": true,
11+
"strictNullChecks": true,
12+
"types": ["cypress", "../../typings"]
13+
}
14+
}

0 commit comments

Comments
 (0)