Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Using Cypress and/or mocha (or mocha-chai) to check for not containing a word but NOT substring

I am trying to using Cypress (which contains mocha, plus I am using mocha-chai) to check for a series of words NOT included in a phrase. That is because for example, I have three <div>s and I want to check

  • that words in div1 are present in div1 but not in div2 and div3;
  • that words in div2 are present in div2 but not in div1 and div3;
  • that words in div3 are present in div3 but not in div1 and div2.

To do that I'm using .contain and .not.contain but unluckily they find some substrings:

if I have

// div1
<div> 
    ...
    The perfect professionist for you
</div>

// div2
<div>
    ...
    Good professionists are ready here
</div>

I do

expect(div1).to.contain('professionist')        // true
expext(div1).to.not.contain('professionists')   // true

this is fine, but

expect(div2).to.contain('professionists')       // true
expect(div2).to.not.contain('professionist')    // false!

will fail.

Is there a way using cypress, mocha or anything similar to do a check on entire word but not substring?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use a regex to do that. For assertion, you have to use match().

var regexValue = "(?<!\S)word(?!\S)"
var actualRegexValue = new RegExp(regexValue.replace("word", "professionist"))
cy.get('div2').invoke('text').then((text) => {
    expect(text).to.match(actualRegexValue)
})

Your regex /(?<!\S)professionist(?!\S)/ will only search for the string professionist in the sentence and not anything else not even professionists.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Word Boundary: \b

More general is regex word boundary

cy.get('div').eq(1).invoke('text')
  .then(div2Text => expect(div2Text).to.not.match(/\bprofessionist\b/))  // passes

or passing the word into the regex (note requires \\b for propper translation)

const word = 'professionist'
const regex = new RegExp(`\\b${word}\\b`)

cy.get('div').eq(1).invoke('text')
  .then(div2Text => expect(div2Text).to.not.match(regex))  // passes

cy.get('div').eq(1).invoke('text')
  .should('not.match', regex)      // passes

Your pastebin

/// <reference types="cypress" />
 
before(() => {})
 
beforeEach(() => {
  cy.viewport(2048, 1080)
})
describe('Test', () => {
  it('simple test', () => {
    let div = document.createElement('div')
    let content = document.createTextNode('Good professionists are ready here')
    div.appendChild(content)

    try {

      let word = 'professionist'
      let regex = new RegExp(`\\b${word}\\b`)
      expect(div.innerText).not.to.match(regex) // passes 

      word = 'professionists'
      regex = new RegExp(`\\b${word}\\b`)
      expect(div.innerText).to.match(regex)  // passes

    } catch (error) {
      Cypress.log(error)
    }
  })
})

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda