Skip to content

Commit ab34b09

Browse files
authored
fix: add deprecation warning to query* (#125)
* fix: Revert to previous query* API Closes #117
1 parent d871be2 commit ab34b09

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

cypress/integration/query.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ describe('query* dom-testing-library commands', () => {
8585

8686
/* Test the behaviour around these queries */
8787

88+
it('queryByText should show a deprecation warning', () => {
89+
let addedLog
90+
function addLog (_, log) {
91+
addedLog = log
92+
cy.off('log:added', addLog)
93+
}
94+
cy.on('log:added', addLog)
95+
96+
cy.queryByText('Button Text 1')
97+
// query* doesn't retry more than once, but our log could be updated later depending on timing.
98+
// the `cy.wrap` adds a retryable step in to deal with possible timing issues of the assertions.
99+
cy.wrap(null).should(() => {
100+
const attrs = addedLog.toJSON()
101+
expect(attrs).to.have.property('state', 'failed')
102+
expect(attrs).to.have.nested.property('err.message')
103+
expect(attrs.err.message).to.contain(`@testing-library/cypress is deprecating all 'query*' commands.`)
104+
})
105+
})
106+
88107
it('queryByText with .should(\'not.exist\')', () => {
89108
cy.queryAllByText(/^Button Text \d$/).should('exist')
90109
cy.queryByText('Non-existing Button Text', {timeout: 100}).should('not.exist')

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ function createCommand(queryName, implementationName) {
174174
return subject
175175
}).finally(() => {
176176
if (options._log) {
177-
if (failedNewFunctionality && !failedOldFunctionality) {
177+
if (queryRegex.test(queryName)) {
178+
options._log.error(Error(`@testing-library/cypress is deprecating all 'query*' commands. 'find*' queries support non-existence starting with version 5 (E.g. cy.findByText('Does Not Exist').should('not.exist')). Please use cy.${queryName.replace(queryRegex, 'find')}(${queryArgument(args)}) instead.`))
179+
} else if (failedNewFunctionality && !failedOldFunctionality) {
178180
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.`))
179181
} else {
180182
options._log.end()

0 commit comments

Comments
 (0)