3

I'm using cypress to set E2E tests.

But i'm facing some troubles, because every time I implement a new feature or refactor some code, i need to run all my tests to see if my modifications doesn't broke something in my application.

In Jest we have the flag --findRelatedTests that run only related tests files modified.

I wonder if there's a way to do the same in cypress.

Fody
  • 23,754
  • 3
  • 20
  • 37
Rafo
  • 511
  • 3
  • 17

2 Answers2

2

Are you looking for the plugin cypress-watch-and-reload?

// cypress.json

{
  "cypress-watch-and-reload": {
    "watch": ["page/*", "src/*.js"]  // watch source and page-object (helper) files
  }
}

YouTube - Re-run Cypress Tests When Application Files Change

Fody
  • 23,754
  • 3
  • 20
  • 37
1

if you try in locally, then 1, If you want to skip a specific test group or test case, one approach is to add .skip at the end of context or it blocks. For example, context.skip() or it.skip().

context.skip('Test group', () => {
  // This whole test group will be skipped
  it('Test case 1', () => {
    // This test case will not run because test group is skipped
  });
});
context('Test group', () => {
  it.skip('test case1', () => {
    // Test case one will be skipped
  });

  it('test case2', () => {
    // Detail for test case two
// This will execute
  });
});
  1. Run Specific/modified Tests Only you can add .only at the end of context or it block, such as context.only() or it.only().
// Only 1st test group will run
  context.only('test group1', () => {
    it('test case', () => {
      // Test case one will run
    });

    it('test case2', () => {
      // Test case two will run
    });
  });

  // The 2nd test group will not run
  context('test group2', () => {
    it('test case3', () => {
      // Test case three will be skipped
    });

    it('test cas4', () => {
      // Test case three will be skipped
    });
  });
context('test group', () => {
    it.only('test case1', () => {
      // Test case one will run
    });

    it('test case2', () => {
      // Test case two will be skipped
    });
  });
  1. Run Tests Conditionally by Using cypress.json run tests conditionally is to use cypress.json file, if you want to run a specific test file.

If you only want to run tests in test.spec.js file, you can just add file path to test files in the cypress.json.

{
  "testFiles": "**/test.spec.js"
}

run multiple test files

{
  "testFiles": ["**/test-1.spec.js", "**/test-2.spec.js"]
}

ignore specific tests from your test runs

{
  "ignoreTestFiles": "**/*.ignore.js"
}

Run Tests Conditionally by Using command line

npx cypress run --spec "cypress/integration/test.spec.js"

To run all the tests in a specific folder with file name end with .spec.js

npx cypress run --spec "cypress/integration/testgroup-directory-name/*.spec.js"

If you are using CI/CD then this will help you and you can get an idea. Faster test execution with cypress-grep

Pali
  • 126
  • 3
  • This would be a manual approach, which would be a bit tedious to do on local and much more difficult to handle via ci. That wouldn't help with only running modified tests. – jjhelguero Apr 01 '22 at 15:04