Skip to content

Commit 217a289

Browse files
committed
chore: implement feedback
1 parent 1f14ce1 commit 217a289

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

cypress/.eslintrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
"max-lines-per-function": "off",
4+
"jest/valid-expect-in-promise": "off"
5+
}
6+
}

cypress/integration/find.spec.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable max-lines-per-function */
2-
31
describe('find* dom-testing-library commands', () => {
42
beforeEach(() => {
53
cy.visit('cypress/fixtures/test-app/')
@@ -164,7 +162,7 @@ describe('find* dom-testing-library commands', () => {
164162
expect(err.message).to.contain(errorMessage)
165163
})
166164

167-
cy.findByText(regex, {timeout: 100}) // Every find query is implicitly a `.should('exist')
165+
cy.findByText(regex, {timeout: 100})
168166
})
169167

170168
it('findByText should default to Cypress non-existence error message', () => {
@@ -183,7 +181,7 @@ describe('find* dom-testing-library commands', () => {
183181
expect(err.message).to.contain(errorMessage)
184182
})
185183

186-
cy.findByLabelText('Label 3', {timeout: 100}).should('exist')
184+
cy.findByLabelText('Label 3', {timeout: 100})
187185
})
188186

189187
it('findByText finding multiple items should error', () => {

cypress/integration/query.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable max-lines-per-function */
2-
31
describe('query* dom-testing-library commands', () => {
42
beforeEach(() => {
53
cy.visit('cypress/fixtures/test-app/')

src/index.js

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

4-
const getDefaultCommandOptions = () => {
5-
return {
6-
timeout: Cypress.config().defaultCommandTimeout,
7-
fallbackToPreviousFunctionality: true,
8-
log: true,
9-
}
10-
}
11-
124
const queryNames = Object.keys(queries)
135

146
const getRegex = /^get/
@@ -47,7 +39,13 @@ function createCommand(queryName, implementationName) {
4739
options: {prevSubject: ['optional', 'document', 'element', 'window']},
4840
command: (prevSubject, ...args) => {
4941
const lastArg = args[args.length - 1]
50-
const defaults = getDefaultCommandOptions()
42+
const defaults = {
43+
// make the timeout extremely short to ensure `query*` commands pass or fail instantly
44+
timeout: queryRegex.test(queryName) ? 0 : Cypress.config().defaultCommandTimeout,
45+
//
46+
fallbackRetryWithoutPreviousSubject: true,
47+
log: true,
48+
}
5149
const options =
5250
typeof lastArg === 'object' ? {...defaults, ...lastArg} : defaults
5351

@@ -100,22 +98,20 @@ function createCommand(queryName, implementationName) {
10098
consoleProps.yielded = result.toArray();
10199
}
102100

103-
if (result.length > 1 && !/all/i.test(queryName)) {
101+
if (result.length > 1 && !/All/.test(queryName)) {
104102
// Is this useful?
105103
throw Error(`Found multiple elements with the text: ${queryArgument(args)}`)
106104
}
107105

108106
return result
109107
}
110108

111-
if (queryRegex.test(queryName)) {
112-
// make the timeout extremely short to ensure `query*` commands pass or fail instantly
113-
options.timeout = 0
114-
}
115-
116-
let error
109+
// This state tracking is not ideal, but it allows detection of compatibility mode for a warning message
117110
let failedNewFunctionality = false
118111
let failedOldFunctionality = false
112+
113+
let error
114+
119115
// Errors will be thrown by @testing-library/dom, but a query might be followed by `.should('not.exist')`
120116
// We just need to capture the error thrown by @testing-library/dom and return an empty jQuery NodeList
121117
// to allow Cypress assertions errors to happen naturally. If an assertion fails, we'll have a helpful
@@ -135,7 +131,7 @@ function createCommand(queryName, implementationName) {
135131
const catchAndTryOldFunctionality = err => {
136132
error = err
137133
failedNewFunctionality = true
138-
const container = options.fallbackToPreviousFunctionality ? options.container || win.document : undefined
134+
const container = options.fallbackRetryWithoutPreviousSubject ? options.container || win.document : undefined
139135
return getValue(container)
140136
}
141137

@@ -170,7 +166,7 @@ function createCommand(queryName, implementationName) {
170166
}).finally(() => {
171167
if (options._log) {
172168
if (failedNewFunctionality && !failedOldFunctionality) {
173-
options._log.error(Error(`@testing-library/cypress will soon only use previous subjects when queries are added to a chain of commands. We've detected an instance where the this functionality failed, but the old functionality passed. Please use cy.${queryName}(${queryArgument(args)}) instead of continuing from a previous chain.`))
169+
options._log.error(Error(`@testing-library/cypress will eventually only use previous subjects when queries are added to a chain of commands. We've detected an instance where the this functionality failed, but the old functionality passed (so your test may break in a future version). Please use cy.${queryName}(${queryArgument(args)}) instead of continuing from a previous chain.`))
174170
} else {
175171
options._log.end()
176172
}

0 commit comments

Comments
 (0)