Skip to content

Commit baf066d

Browse files
committed
feat: add ability to configure
2 parents 217a289 + e5cb2a5 commit baf066d

File tree

8 files changed

+64
-15
lines changed

8 files changed

+64
-15
lines changed

.all-contributorsrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"imageSize": 100,
88
"commit": false,
9-
"skipCi": true,
9+
"skipCi": false,
1010
"contributors": [
1111
{
1212
"login": "kentcdodds",
@@ -271,6 +271,15 @@
271271
"code",
272272
"test"
273273
]
274+
},
275+
{
276+
"login": "FlopieUtd",
277+
"name": "FlopieUtd",
278+
"avatar_url": "https://avatars3.githubusercontent.com/u/23555863?v=4",
279+
"profile": "https://github.com/FlopieUtd",
280+
"contributions": [
281+
"doc"
282+
]
274283
}
275284
],
276285
"repoType": "github",

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ This allows you to use all the useful
6161
- [Installation](#installation)
6262
- [With TypeScript](#with-typescript)
6363
- [Usage](#usage)
64+
- [Differences from DOM Testing Library](#differences-from-dom-testing-library)
6465
- [Other Solutions](#other-solutions)
6566
- [Contributors](#contributors)
6667
- [LICENSE](#license)
@@ -95,7 +96,7 @@ and should be added as follows in `tsconfig.json`:
9596

9697
Add this line to your project's `cypress/support/commands.js`:
9798

98-
```
99+
```javascript
99100
import '@testing-library/cypress/add-commands'
100101
```
101102

@@ -105,14 +106,19 @@ and `queryAllBy` commands.
105106

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

109+
To configure DOM Testing Library, use the following:
110+
```javascript
111+
cy.configureCypressTestingLibrary(config)
112+
```
113+
108114
To show some simple examples (from
109115
[cypress/integration/query.spec.js](cypress/integration/query.spec.js) or [cypress/integration/find.spec.js](cypress/integration/find.spec.js)):
110116

111117
```javascript
112118
cy.queryAllByText('Button Text').should('exist')
113119
cy.queryAllByText('Non-existing Button Text').should('not.exist')
114120
cy.queryAllByLabelText('Label text', {timeout: 7000}).should('exist')
115-
cy.findAllByText('Jackie Chan').click({multiple: true})
121+
cy.findAllByText('Jackie Chan').click();
116122

117123
// findAllByText _inside_ a form element
118124
cy.get('form').within(() => {
@@ -124,7 +130,7 @@ cy.get('form').then(subject => {
124130
cy.get('form').findAllByText('Button Text').should('exist')
125131
```
126132

127-
### Differences DOM Testing Library
133+
### Differences from DOM Testing Library
128134

129135
`Cypress Testing Library` supports both jQuery elements and DOM nodes. This is
130136
necessary because Cypress uses jQuery elements, while `DOM Testing Library`
@@ -137,7 +143,8 @@ for the `DOM Testing Library` functions.
137143
built-in retryability using error messages from `get*` APIs to forward as error
138144
messages if a query fails. `query*` also uses `get*` APIs, but disables retryability.
139145

140-
`findBy*` is less useful in Cypress compared to `findAllBy*`. If you intend to limit
146+
`findAll*` can select more than one element and is closer in functionality to how
147+
Cypress built-in commands work. is less useful in Cypress compared to `findAllBy*`. If you intend to limit
141148
to only 1 element, the following will work:
142149

143150
```javascript
@@ -199,6 +206,7 @@ Thanks goes to these people ([emoji key][emojis]):
199206
<td align="center"><a href="https://www.franck-abgrall.me/"><img src="https://avatars3.githubusercontent.com/u/9840435?v=4" width="100px;" alt=""/><br /><sub><b>Franck Abgrall</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/pulls?q=is%3Apr+reviewed-by%3Akefranabg" title="Reviewed Pull Requests">👀</a></td>
200207
<td align="center"><a href="http://twitter.com/tlrobinson"><img src="https://avatars0.githubusercontent.com/u/18193?v=4" width="100px;" alt=""/><br /><sub><b>Tom Robinson</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=tlrobinson" title="Code">💻</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=tlrobinson" title="Tests">⚠️</a></td>
201208
<td align="center"><a href="https://github.com/NicholasBoll"><img src="https://avatars2.githubusercontent.com/u/338257?v=4" width="100px;" alt=""/><br /><sub><b>Nicholas Boll</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=NicholasBoll" title="Code">💻</a> <a href="https://github.com/testing-library/cypress-testing-library/commits?author=NicholasBoll" title="Tests">⚠️</a></td>
209+
<td align="center"><a href="https://github.com/FlopieUtd"><img src="https://avatars3.githubusercontent.com/u/23555863?v=4" width="100px;" alt=""/><br /><sub><b>FlopieUtd</b></sub></a><br /><a href="https://github.com/testing-library/cypress-testing-library/commits?author=FlopieUtd" title="Documentation">📖</a></td>
202210
</tr>
203211
</table>
204212

cypress/integration/configure.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference types="cypress" />
2+
describe('configuring fallback globally', () => {
3+
beforeEach(() => {
4+
cy.visit('cypress/fixtures/test-app/')
5+
cy.configureCypressTestingLibrary({ fallbackRetryWithoutPreviousSubject: false })
6+
})
7+
8+
it('findByText with a previous subject', () => {
9+
cy.get('#nested')
10+
.findByText('Button Text 1')
11+
.should('not.exist')
12+
cy.get('#nested')
13+
.findByText('Button Text 2')
14+
.should('exist')
15+
})
16+
})
17+
18+
/* global cy */

cypress/integration/find.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="cypress" />
12
describe('find* dom-testing-library commands', () => {
23
beforeEach(() => {
34
cy.visit('cypress/fixtures/test-app/')
@@ -110,7 +111,7 @@ describe('find* dom-testing-library commands', () => {
110111

111112
it('findByText with a previous subject', () => {
112113
cy.get('#nested')
113-
.findByText('Button Text 1', { fallbackToPreviousFunctionality: false })
114+
.findByText('Button Text 1', { fallbackRetryWithoutPreviousSubject: false })
114115
.should('not.exist')
115116
cy.get('#nested')
116117
.findByText('Button Text 2')
@@ -125,8 +126,6 @@ describe('find* dom-testing-library commands', () => {
125126
})
126127

127128
it('findByText in container', () => {
128-
// NOTE: Cypress' `then` doesn't actually return a promise
129-
// eslint-disable-next-line jest/valid-expect-in-promise
130129
cy.get('#nested').then(subject => {
131130
cy.findByText('Button Text 1', {container: subject}).should('not.exist')
132131
cy.findByText('Button Text 2', {container: subject}).should('exist')

cypress/integration/query.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="cypress" />
12
describe('query* dom-testing-library commands', () => {
23
beforeEach(() => {
34
cy.visit('cypress/fixtures/test-app/')

src/__tests__/add-commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('adds commands to Cypress', () => {
77

88
require('../add-commands')
99

10-
expect(addMock).toHaveBeenCalledTimes(commands.length)
10+
expect(addMock).toHaveBeenCalledTimes(commands.length + 1) // we're also adding a configuration command
1111
commands.forEach(({name}, index) => {
1212
expect(addMock.mock.calls[index]).toMatchObject([
1313
name,

src/add-commands.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import {commands} from './'
1+
import {configure, commands} from './'
22

33
commands.forEach(({name, command, options = {}}) => {
44
Cypress.Commands.add(name, options, command)
55
})
66

7+
Cypress.Commands.add('configureCypressTestingLibrary', config => {
8+
configure(config)
9+
})
10+
711
/* global Cypress */

src/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import {configure, queries} from '@testing-library/dom'
22
import {getContainer} from './utils'
33

4+
let globalFallbackRetryWithoutPreviousSubject = true
5+
const configureFn = configDelta => {
6+
if (configDelta.fallbackRetryWithoutPreviousSubject != null) {
7+
globalFallbackRetryWithoutPreviousSubject = configDelta.fallbackRetryWithoutPreviousSubject
8+
delete configDelta.fallbackRetryWithoutPreviousSubject
9+
}
10+
configure(configDelta)
11+
}
12+
413
const queryNames = Object.keys(queries)
514

615
const getRegex = /^get/
@@ -42,8 +51,9 @@ function createCommand(queryName, implementationName) {
4251
const defaults = {
4352
// make the timeout extremely short to ensure `query*` commands pass or fail instantly
4453
timeout: queryRegex.test(queryName) ? 0 : Cypress.config().defaultCommandTimeout,
45-
//
46-
fallbackRetryWithoutPreviousSubject: true,
54+
// setting this to false will disable the fallback to querying without a previous subject
55+
// This is to prevent breaking changes, but also allow for prevSubject scoping
56+
fallbackRetryWithoutPreviousSubject: globalFallbackRetryWithoutPreviousSubject,
4757
log: true,
4858
}
4959
const options =
@@ -93,9 +103,9 @@ function createCommand(queryName, implementationName) {
93103

94104
consoleProps.elements = result.length
95105
if (result.length === 1) {
96-
consoleProps.yielded = result.toArray()[0];
106+
consoleProps.yielded = result.toArray()[0]
97107
} else if (result.length > 0) {
98-
consoleProps.yielded = result.toArray();
108+
consoleProps.yielded = result.toArray()
99109
}
100110

101111
if (result.length > 1 && !/All/.test(queryName)) {
@@ -203,7 +213,7 @@ function queryArgument(args) {
203213

204214
const commands = [...getCommands, ...findCommands, ...queryCommands]
205215

206-
export {commands, configure}
216+
export {commands, configureFn as configure}
207217

208218
/* eslint no-new-func:0, complexity:0 */
209219
/* globals Cypress, cy */

0 commit comments

Comments
 (0)