Skip to content

Commit

Permalink
fix: add deprecation warning to query* (#125)
Browse files Browse the repository at this point in the history
* fix: Revert to previous query* API

Closes #117
  • Loading branch information
NicholasBoll authored Mar 12, 2020
1 parent d871be2 commit ab34b09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit ab34b09

Please sign in to comment.