Skip to content

Commit ee75c14

Browse files
NicholasBollkentcdodds
authored andcommitted
fix: remove deprecated query* queries (#130)
BREAKING CHANGE: Remove `query*` queries. Throw an error instead. Fixing requires updating all `query*` to `find*` queries. In practice this means replacing `cy.query` with `cy.find` BREAKING CHANGE: Remove fallback that retries chaiined query that assumes no previous subject. In practice this means starting new chains if no previous subject is required. ```js // Before cy.findByText('Foo') .click() .findByText('Bar') // Element with 'Bar' text is not a child of an element with 'Foo' text .click() // After cy.findByText('Foo') .click() cy.findByText('Bar') .click() ```
1 parent 503cbc0 commit ee75c14

File tree

5 files changed

+100
-329
lines changed

5 files changed

+100
-329
lines changed

README.md

+34-35
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ This allows you to use all the useful
5757
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
5858
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5959

60-
6160
- [Installation](#installation)
6261
- [With TypeScript](#with-typescript)
6362
- [Usage](#usage)
@@ -79,7 +78,8 @@ npm install --save-dev @testing-library/cypress
7978

8079
### With TypeScript
8180

82-
Typings are defined in `@types/testing-library__cypress` at [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress),
81+
Typings are defined in `@types/testing-library__cypress` at
82+
[DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress),
8383
and should be added as follows in `tsconfig.json`:
8484

8585
```json
@@ -100,11 +100,12 @@ Add this line to your project's `cypress/support/commands.js`:
100100
import '@testing-library/cypress/add-commands'
101101
```
102102

103-
You can now use all of `DOM Testing Library`'s `findBy`, `findAllBy`, `queryBy`
104-
and `queryAllBy` commands.
103+
You can now use all of `DOM Testing Library`'s `findBy` and `findAllBy`
104+
commands.
105105
[See the `DOM Testing Library` docs for reference](https://testing-library.com)
106106

107-
You can find [all Library definitions here](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress/index.d.ts).
107+
You can find
108+
[all Library definitions here](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress/index.d.ts).
108109

109110
To configure DOM Testing Library, use the following custom command:
110111

@@ -113,46 +114,43 @@ cy.configureCypressTestingLibrary(config)
113114
```
114115

115116
To show some simple examples (from
116-
[cypress/integration/query.spec.js](cypress/integration/query.spec.js) or [cypress/integration/find.spec.js](cypress/integration/find.spec.js)):
117+
[cypress/integration/find.spec.js](cypress/integration/find.spec.js)):
117118

118119
```javascript
119-
cy.queryAllByText('Button Text').should('exist')
120-
cy.queryAllByText('Non-existing Button Text').should('not.exist')
121-
cy.queryAllByLabelText('Label text', {timeout: 7000}).should('exist')
122-
cy.findAllByText('Jackie Chan').click();
120+
cy.findAllByText('Button Text').should('exist')
121+
cy.findAllByText('Non-existing Button Text').should('not.exist')
122+
cy.findAllByLabelText('Label text', {timeout: 7000}).should('exist')
123+
cy.findAllByText('Jackie Chan').click()
123124

124125
// findAllByText _inside_ a form element
125-
cy.get('form').within(() => {
126-
cy.findAllByText('Button Text').should('exist')
127-
})
128-
cy.get('form').then(subject => {
129-
cy.findAllByText('Button Text', {container: subject}).should('exist')
130-
})
131-
cy.get('form').findAllByText('Button Text').should('exist')
126+
cy.get('form')
127+
.findAllByText('Button Text')
128+
.should('exist')
132129
```
133130

134131
### Differences from DOM Testing Library
135132

136133
`Cypress Testing Library` supports both jQuery elements and DOM nodes. This is
137134
necessary because Cypress uses jQuery elements, while `DOM Testing Library`
138-
expects DOM nodes. When you pass a jQuery element as `container`, it will get
139-
the first DOM node from the collection and use that as the `container` parameter
140-
for the `DOM Testing Library` functions.
141-
142-
`get*` queries are disabled. `find*` queries do not use the Promise API of
143-
`DOM Testing Library`, but instead forward to the `get*` queries and use Cypress'
144-
built-in retryability using error messages from `get*` APIs to forward as error
145-
messages if a query fails. `query*` also uses `get*` APIs, but disables retryability.
146-
147-
`findAll*` can select more than one element and is closer in functionality to how
148-
Cypress built-in commands work. `findAll*` is preferred to `find*` queries.
149-
`find*` commands will fail if more than one element is found that matches the criteria
150-
which is not how built-in Cypress commands work, but is provided for closer compatibility
151-
to other Testing Libraries.
152-
153-
Cypress handles actions when there is only one element found. For example, the following
154-
will work without having to limit to only 1 returned element. The `cy.click` will
155-
automatically fail if more than 1 element is returned by the `findAllByText`:
135+
expects DOM nodes. When you chain a query, it will get the first DOM node from
136+
`subject` of the collection and use that as the `container` parameter for the
137+
`DOM Testing Library` functions.
138+
139+
`get*` and `query*` queries are disabled. `find*` queries do not use the Promise
140+
API of `DOM Testing Library`, but instead forward to the `get*` queries and use
141+
Cypress' built-in retryability using error messages from `get*` APIs to forward
142+
as error messages if a query fails.
143+
144+
`findAll*` can select more than one element and is closer in functionality to
145+
how Cypress built-in commands work. `find*` commands will fail if more than one
146+
element is found that matches the criteria which is not how built-in Cypress
147+
commands work, but is provided for closer compatibility to other Testing
148+
Libraries.
149+
150+
Cypress handles actions when there is only one element found. For example, the
151+
following will work without having to limit to only 1 returned element. The
152+
`cy.click` will automatically fail if more than 1 element is returned by the
153+
`findAllByText`:
156154

157155
```javascript
158156
cy.findAllByText('Some Text').click()
@@ -237,6 +235,7 @@ Thanks goes to these people ([emoji key][emojis]):
237235

238236
<!-- markdownlint-enable -->
239237
<!-- prettier-ignore-end -->
238+
240239
<!-- ALL-CONTRIBUTORS-LIST:END -->
241240

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

cypress/integration/configure.spec.js

-18
This file was deleted.

cypress/integration/find.spec.js

+1-21
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('find* dom-testing-library commands', () => {
108108

109109
it('findByText with a previous subject', () => {
110110
cy.get('#nested')
111-
.findByText('Button Text 1', {fallbackRetryWithoutPreviousSubject: false})
111+
.findByText('Button Text 1')
112112
.should('not.exist')
113113
cy.get('#nested')
114114
.findByText('Button Text 2')
@@ -188,15 +188,6 @@ describe('find* dom-testing-library commands', () => {
188188
cy.findByText(/^Button Text/i, {timeout: 100})
189189
})
190190

191-
it('findByText should not break existing code', () => {
192-
cy.window()
193-
.findByText('Button Text 1')
194-
.should('exist')
195-
cy.location()
196-
.findByText('Button Text 1')
197-
.should('exist')
198-
})
199-
200191
it('findByText should show as a parent command if it starts a chain', () => {
201192
const assertLog = (attrs, log) => {
202193
if (log.get('name') === 'findByText') {
@@ -218,17 +209,6 @@ describe('find* dom-testing-library commands', () => {
218209
cy.on('log:added', assertLog)
219210
cy.get('body').findByText('Button Text 1')
220211
})
221-
222-
it('should chain findBy* with subject different of document, element or window', () => {
223-
cy.wrap(true)
224-
.should('be.true')
225-
.findByText('Error message')
226-
.findByLabelText(/Required/i)
227-
.type('something')
228-
.findByText('Submit')
229-
.queryByText('Error message')
230-
.should('not.be.visible')
231-
})
232212
})
233213

234214
/* global cy */

0 commit comments

Comments
 (0)