Skip to content
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

fix: show correct error message when a regex starting with = is passed to cy.contains. #21388

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,16 @@ describe('src/cy/commands/querying', () => {
cy.get('#backslashes').contains('"<OE_D]dQ\\')
})

// https://github.com/cypress-io/cypress/issues/21108
it('shows correct error message when regex starts with =(equals sign)', () => {
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) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/driver/src/config/jquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down