You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add more helpful debugging information to queries (#108)
* feat: add more helpful debugging information to queries
* Add element selector information for debugging (outlines element when you click on command) (fixes#103)
* Add @testing-library/dom errors (from `get*` queries) to failure messages - these are more helpful than the generic `find*('input') does not exist` messages (fixes#103)
* Add retryability to `findBy*` when multiple elements are found (fixes#83)
* Add option to disable logging of all commands
* `query*` and `find*` have a consistent code path and error messaging (fixes#103)
* Remove usage of Cypress commands in queries (fixes#103)
* feat: add ability for queries to inherit previous subject
* Fixes#109 without breaking change caused by #100
* feat: add parent/child log type detection
* chore: implement feedback
* docs: update readme to complete my thought process
* slightly simplify config fn
* Update README.md
Co-authored-by: Kent C. Dodds <[email protected]>
Closes#103, Closes#109, Closes#110
Copy file name to clipboardExpand all lines: README.md
+46-7Lines changed: 46 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,7 @@ This allows you to use all the useful
61
61
-[Installation](#installation)
62
62
-[With TypeScript](#with-typescript)
63
63
-[Usage](#usage)
64
+
-[Differences from DOM Testing Library](#differences-from-dom-testing-library)
64
65
-[Other Solutions](#other-solutions)
65
66
-[Contributors](#contributors)
66
67
-[LICENSE](#license)
@@ -95,7 +96,7 @@ and should be added as follows in `tsconfig.json`:
95
96
96
97
Add this line to your project's `cypress/support/commands.js`:
97
98
98
-
```
99
+
```javascript
99
100
import'@testing-library/cypress/add-commands'
100
101
```
101
102
@@ -105,28 +106,66 @@ and `queryAllBy` commands.
105
106
106
107
You can find [all Library definitions here](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__cypress/index.d.ts).
107
108
109
+
To configure DOM Testing Library, use the following custom command:
110
+
111
+
```javascript
112
+
cy.configureCypressTestingLibrary(config)
113
+
```
114
+
108
115
To show some simple examples (from
109
116
[cypress/integration/query.spec.js](cypress/integration/query.spec.js) or [cypress/integration/find.spec.js](cypress/integration/find.spec.js)):
it('findByText should set the Cypress element to the found element',()=>{
141
+
// This test is a little strange since snapshots show what element
142
+
// is selected, but snapshots themselves don't give access to those
143
+
// elements. I had to make the implementation specific so that the `$el`
144
+
// is the `subject` when the log is added and the `$el` is the `value`
145
+
// when the log is changed. It would be better to extract the `$el` from
146
+
// each snapshot
147
+
148
+
cy.on('log:changed',(attrs,log)=>{
149
+
if(log.get('name')==='findByText'){
150
+
expect(log.get('$el')).to.have.text('Button Text 1')
151
+
}
152
+
})
153
+
154
+
cy.findByText('Button Text 1')
155
+
})
156
+
113
157
it('findByText should error if no elements are found',()=>{
114
158
constregex=/Supercalifragilistic/
115
-
consterrorMessage=`Timed out retrying: Expected to find element: 'findByText(${regex})', but never found it.`
159
+
consterrorMessage=`Unable to find an element with the text: /Supercalifragilistic/`
116
160
cy.on('fail',err=>{
117
-
expect(err.message).to.eq(errorMessage)
161
+
expect(err.message).to.contain(errorMessage)
118
162
})
119
163
120
-
cy.findByText(regex,{timeout: 100})// Doesn't explicitly need .should('exist') if it's the last element?
164
+
cy.findByText(regex,{timeout: 100})
165
+
})
166
+
167
+
it('findByText should default to Cypress non-existence error message',()=>{
168
+
consterrorMessage=`Expected <button> not to exist in the DOM, but it was continuously found.`
169
+
cy.on('fail',err=>{
170
+
expect(err.message).to.contain(errorMessage)
171
+
})
172
+
173
+
cy.findByText('Button Text 1',{timeout: 100})
174
+
.should('not.exist')
175
+
})
176
+
177
+
it('findByLabelText should forward useful error messages from @testing-library/dom',()=>{
178
+
consterrorMessage=`Found a label with the text of: Label 3, however no form control was found associated to that label.`
179
+
cy.on('fail',err=>{
180
+
expect(err.message).to.contain(errorMessage)
181
+
})
182
+
183
+
cy.findByLabelText('Label 3',{timeout: 100})
121
184
})
122
185
123
186
it('findByText finding multiple items should error',()=>{
124
187
consterrorMessage=`Found multiple elements with the text: /^Button Text/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`
125
188
cy.on('fail',err=>{
126
-
expect(err.message).to.eq(errorMessage)
189
+
expect(err.message).to.contain(errorMessage)
127
190
})
128
191
129
-
cy.findByText(/^ButtonText/i)
192
+
cy.findByText(/^ButtonText/i,{timeout: 100})
193
+
})
194
+
195
+
it('findByText should not break existing code',()=>{
196
+
cy.window()
197
+
.findByText('Button Text 1')
198
+
.should('exist')
199
+
})
200
+
201
+
it('findByText should show as a parent command if it starts a chain',()=>{
202
+
constassertLog=(attrs,log)=>{
203
+
if(log.get('name')==='findByText'){
204
+
expect(log.get('type')).to.equal('parent')
205
+
cy.off('log:added',assertLog)
206
+
}
207
+
}
208
+
cy.on('log:added',assertLog)
209
+
cy.findByText('Button Text 1')
210
+
})
211
+
212
+
it('findByText should show as a child command if it continues a chain',()=>{
it('queryAllByText should default to Cypress non-existence error message',()=>{
171
+
consterrorMessage=`Expected <button> not to exist in the DOM, but it was continuously found.`
135
172
cy.on('fail',err=>{
136
-
expect(err.message).to.eq(errorMessage)
173
+
expect(err.message).to.contain(errorMessage)
137
174
})
138
175
139
-
cy.queryAllByText(text,{timeout: 100}).should('exist')// NOT POSSIBLE WITH QUERYALL?
176
+
cy.queryAllByText('Button Text 1',{timeout: 100})
177
+
.should('not.exist')
140
178
})
141
179
142
180
it('queryByText finding multiple items should error',()=>{
143
-
consterrorMessage=`Found multiple elements with the text: /^queryByText/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`
181
+
consterrorMessage=`Found multiple elements with the text: /^Button Text/i\n\n(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).`
0 commit comments