diff --git a/cypress/integration/query.spec.js b/cypress/integration/query.spec.js index 3ef0905..24671d3 100644 --- a/cypress/integration/query.spec.js +++ b/cypress/integration/query.spec.js @@ -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') diff --git a/src/index.js b/src/index.js index 26740ed..a3eb575 100644 --- a/src/index.js +++ b/src/index.js @@ -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()