Skip to content

Commit

Permalink
fix(TS): fix types and add tests for types (#21)
Browse files Browse the repository at this point in the history
<!--
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)
  • Loading branch information
Airat Aminev authored and Kent C. Dodds committed Nov 6, 2018
1 parent d530690 commit d782f03
Show file tree
Hide file tree
Showing 6 changed files with 801 additions and 31 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
"ideas",
"test"
]
},
{
"login": "airato",
"name": "Airat Aminev",
"avatar_url": "https://avatars3.githubusercontent.com/u/4506749?v=4",
"profile": "https://github.com/airato",
"contributions": [
"code",
"test",
"tool"
]
}
],
"repoType": "github"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

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

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

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<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://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") |
| :---: | :---: | :---: | :---: | :---: |

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

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
"add-contributor": "kcd-scripts contributors add",
"build": "kcd-scripts build",
"lint": "kcd-scripts lint",
"test": "npm-run-all --parallel test:unit test:cypress",
"test": "npm-run-all --parallel test:unit test:cypress test:typings",
"test:unit": "kcd-scripts test --no-watch",
"test:unit:watch": "kcd-scripts test",
"test:cypress:serve": "serve --listen 13370 ./cypress/fixtures/test-app",
"test:cypress:run": "wait-port --timeout 10000 localhost:13370 && cypress run",
"test:cypress:open": "cypress open",
"test:cypress": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:run",
"test:cypress:dev": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:open",
"test:typings": "tsc -p tests/typescript-types/ --noemit",
"test:typings:watch": "tsc -p tests/typescript-types/ --noemit --watch",
"validate": "kcd-scripts validate build,lint,test",
"setup": "npm install && npm run validate -s",
"precommit": "kcd-scripts precommit",
Expand Down Expand Up @@ -50,6 +52,7 @@
"kcd-scripts": "^0.37.0",
"npm-run-all": "^4.1.2",
"serve": "^10.0.0",
"typescript": "^3.1.6",
"wait-port": "^0.2.2"
},
"peerDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions tests/typescript-types/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
describe('Foo', () => {
it('has proper types', () => {
cy.visit('#/foo')

cy.getAllByPlaceholderText('foo').should(elements => {
// argument should be an array of HTML elements
expect(elements.length).to.eq(0)
expect(elements[0].tagName).to.eq(0)
})

// with regex
cy.queryByPlaceholderText<'a'>(/foo/).should(element => {
// node can be null
if (element) {
// argument should be an anchor
expect(element.href).to.eq('')
}
})

// with matcher function
const matcherFn = (content: string, element: HTMLElement) => true

cy.queryByPlaceholderText<HTMLAudioElement>(matcherFn).should(element => {
// node can be null
if (element) {
// argument should be an Audio element
expect(element.play).to.exist
}
})

// with matcher options
cy.queryAllByPlaceholderText<HTMLAudioElement>('foo', {
collapseWhitespace: true,
exact: true,
trim: true,
}).should(elements => {
const el = elements[0]
// node can be null
if (el) {
// argument should be an array of Audio elements
expect(el.play).to.exist
}
})
})
})
14 changes: 14 additions & 0 deletions tests/typescript-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["dom", "es2015", "es2016", "es2017"],
"target": "es2017",
"module": "esnext",
"moduleResolution": "node",
"experimentalDecorators": true,
"outDir": "dist",
"noUnusedLocals": true,
"noImplicitAny": true,
"strictNullChecks": true,
"types": ["cypress", "../../typings"]
}
}
Loading

0 comments on commit d782f03

Please sign in to comment.