Skip to content

Commit ec3b712

Browse files
npeterkampsKent C. Dodds
authored and
Kent C. Dodds
committed
feat(timeout): allow to configure the timeout for queries (#7)
* feat(timeout): allow to configure the timeout for queries - make sure that Cypress' timeout is larger, so our error message is used - make sure Cypress' Command Log doesn't get cluttered when options isn't specified - show example in the docs - updated outdated snapshot - ignore .idea folder (JetBrains IDE's) - added myself to contributers at the request of the contribution guide * Pass all args to `queryImpl`, pass all args with some defaults to `waitForElement`.
1 parent 02d1ec4 commit ec3b712

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@
3838
"code",
3939
"test"
4040
]
41+
},
42+
{
43+
"login": "npeterkamps",
44+
"name": "Peter Kamps",
45+
"avatar_url": "https://avatars1.githubusercontent.com/u/25429764?v=4",
46+
"profile": "https://github.com/npeterkamps",
47+
"contributions": [
48+
"code",
49+
"doc",
50+
"ideas"
51+
]
4152
}
4253
],
4354
"repoType": "github"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist
55
.opt-out
66
.DS_Store
77
.eslintcache
8+
.idea/
89

910
# these cause more harm than good
1011
# when working with contributors

README.md

Lines changed: 4 additions & 5 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-3-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -73,6 +73,7 @@ To show some simple examples (from [cypress/integration/commands.spec.js](cypres
7373
cy.getAllByText('Jackie Chan').click()
7474
cy.queryByText('Button Text').should('exist')
7575
cy.queryByText('Non-existing Button Text').should('not.exist')
76+
cy.queryByLabelText('Label text', { timeout: 7000 }).should('exist')
7677
```
7778

7879
## Other Solutions
@@ -85,11 +86,9 @@ here!
8586
Thanks goes to these people ([emoji key][emojis]):
8687

8788
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
88-
8989
<!-- prettier-ignore -->
90-
| [<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") |
91-
| :---: | :---: | :---: |
92-
90+
| [<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") |
91+
| :---: | :---: | :---: | :---: |
9392
<!-- ALL-CONTRIBUTORS-LIST:END -->
9493

9594
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/__snapshots__/commands.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ Array [
2626
"queryAllByTitle",
2727
"getByTitle",
2828
"getAllByTitle",
29+
"queryByValue",
30+
"queryAllByValue",
31+
"getByValue",
32+
"getAllByValue",
2933
]
3034
`;

src/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import {queries, waitForElement} from 'dom-testing-library'
22

3+
const defaults = {
4+
timeout: 3000,
5+
}
6+
37
const commands = Object.keys(queries).map(queryName => {
48
return {
59
name: queryName,
610
command: (cy, ...args) => {
11+
const lastArg = args[args.length - 1]
12+
const waitOptions = (typeof lastArg === 'object')
13+
? Object.assign({}, defaults, lastArg)
14+
: defaults
15+
716
const queryImpl = queries[queryName]
817
const baseCommandImpl = doc =>
9-
waitForElement(() => queryImpl(doc, ...args), {
18+
waitForElement(() => queryImpl(doc, ...args), Object.assign({}, waitOptions, {
1019
container: doc,
11-
timeout: 3000,
12-
})
20+
}))
1321
let commandImpl
1422
if (
1523
queryName.startsWith('queryBy') ||
@@ -32,12 +40,20 @@ const commands = Object.keys(queries).map(queryName => {
3240
)(commandImpl)
3341
return cy
3442
.window({log: false})
35-
.then(thenHandler)
43+
.then({ timeout: waitOptions.timeout + 100 }, thenHandler)
3644
.then(subject => {
3745
Cypress.log({
3846
$el: subject,
3947
name: queryName,
40-
message: args,
48+
message: args.filter((value) => {
49+
if (Array.isArray(value) && value.length === 0) {
50+
return false;
51+
}
52+
if (typeof value === 'object' && Object.keys(value).length === 0) {
53+
return false;
54+
}
55+
return Boolean(value);
56+
}),
4157
})
4258
return subject
4359
})

0 commit comments

Comments
 (0)