Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!