Skip to content

chore: add deprecation warning to query* #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cypress/integration/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ describe('query* dom-testing-library commands', () => {

/* Test the behaviour around these queries */

it('queryByText should show a deprecation warning', () => {
let addedLog
function addLog (_, log) {
addedLog = log
cy.off('log:added', addLog)
}
cy.on('log:added', addLog)

cy.queryByText('Button Text 1')
// query* doesn't retry more than once, but our log could be updated later depending on timing.
// the `cy.wrap` adds a retryable step in to deal with possible timing issues of the assertions.
cy.wrap(null).should(() => {
const attrs = addedLog.toJSON()
expect(attrs).to.have.property('state', 'failed')
expect(attrs).to.have.nested.property('err.message')
expect(attrs.err.message).to.contain(`@testing-library/cypress is deprecating all 'query*' commands.`)
})
})

it('queryByText with .should(\'not.exist\')', () => {
cy.queryAllByText(/^Button Text \d$/).should('exist')
cy.queryByText('Non-existing Button Text', {timeout: 100}).should('not.exist')
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ function createCommand(queryName, implementationName) {
return subject
}).finally(() => {
if (options._log) {
if (failedNewFunctionality && !failedOldFunctionality) {
if (queryRegex.test(queryName)) {
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.`))
} else if (failedNewFunctionality && !failedOldFunctionality) {
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.`))
} else {
options._log.end()
Expand Down