diff --git a/packages/driver/cypress/integration/commands/querying/querying_spec.js b/packages/driver/cypress/integration/commands/querying/querying_spec.js index 39b7050eb60c..05a858a1e01c 100644 --- a/packages/driver/cypress/integration/commands/querying/querying_spec.js +++ b/packages/driver/cypress/integration/commands/querying/querying_spec.js @@ -1230,6 +1230,16 @@ describe('src/cy/commands/querying', () => { cy.get('#backslashes').contains('" { + cy.on('fail', (err) => { + expect(err.message).to.include('Expected to find content') + }) + + cy.visit('fixtures/dom.html') + cy.contains(/=[0-6]/, { timeout: 100 }).should('have.text', 'a=2') + }) + describe('should(\'not.exist\')', () => { it('returns null when no content exists', () => { cy.contains('alksjdflkasjdflkajsdf').should('not.exist').then(($el) => { diff --git a/packages/driver/src/config/jquery.ts b/packages/driver/src/config/jquery.ts index d998e6af7099..11a35be65660 100644 --- a/packages/driver/src/config/jquery.ts +++ b/packages/driver/src/config/jquery.ts @@ -51,6 +51,14 @@ $.find.matchesSelector = function (elem, expr) { return matchesSelector.apply(_this, args) }, catchFn (e) { + // https://github.com/cypress-io/cypress/issues/21108 + // When regex starts with =, it is a syntax error when nothing found. + // Because Sizzle internally escapes = to handle attribute selectors. + // @see https://github.com/jquery/sizzle/blob/20390f05731af380833b5aa805db97de0b91268a/external/jquery/jquery.js#L4363-L4370 + if (e.message.includes(`Syntax error, unrecognized expression: :cy-contains('`)) { + return false + } + throw e }, finallyFn () {