Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
Issue with setting up jest mock

I have the following function that is used within cypress tests for which I want to do unit testing (filterTests.js):

const filterTests = (definedTags, runTest) => {
  console.log(`Cypress tags: ${definedTags}`);
  let isFound = true;
  
  const includeTag = Cypress.env('INCLUDETAG');
  const excludeTag = Cypress.env('EXCLUDETAG');
  
  if (includeTag) {
    isFound = definedTags.includes(includeTag);
  }

  if (excludeTag) {
    isFound = ! definedTags.includes(excludeTag);
  }

  if (isFound) {
    runTest();
  }
};

export default filterTests;

A test double for Cypress.env needs to be created. I'm not sure if this would technically be considered a stub, mock, fake, dummy, etc..., but the philosophical discussion isn't the focus right now. It looks like in the Cypress world, everything is lumped together under 'mock'.

I started down the path of something like this in the Jest test file:

import filterTests from '../../cypress/support/filterTests';

describe('Something', () => {
  jest.mock('Cypress', () => ({
      env: {
        INCLUDETAG: 'jenkins1'
      }
  }));


  it('Something else ', (done) => {
    const tempFunc = () => {
      console.log('here a');
      done();
    };

    filterTests(tag, tempFunc);
  });
});

But for that I get the error message:

    Cannot find module 'Cypress' from 'spec/cypress/filterTestsSO2.test.js'

      2 |
      3 | describe('Something', () => {
    > 4 |   jest.mock('Cypress', () => ({
        |        ^
      5 |       env: {
      6 |         INCLUDETAG: 'jenkins1'
      7 |       }

I believe what is complicating this situation is that Cypress is not explicitly imported in filterTests.js

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you might just want to set the env value at the top of the test

describe('Something', () => {

  Cypress.env(INCLUDETAG, 'jenkins1')

  it('Something else ', (done) => {
    const tempFunc = () => {
      console.log('here a');
      done();
    };

    filterTests(tag, tempFunc);  // this function will read the env set above
  })
})

Further info - Cypress has a cy.spy() which wraps a method and records it's calls but otherwise leaves it's result the same.

Also cy.stub() which records calls but also provides a fake result.


Jest globals

If you are running the test in Jest, then the Cypress global should be able to be mocked simply by setting it up

global.Cypress = {
  env: () => 'jenkins1'  // or more complicated fn as test requires
}

Note I expect this will only work with simple cases. Cypress wraps jQuery, Chai and Mocha so they behave slightly differently when a Cypress test runs. If the function you test uses any of those features, even implicitly (like command retry), then Jest will not reproduce the right environment.

My recommendation, test Cypress with Cypress.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda