Is there a way to prevent Cypress to fail if zero test files are found?

Viewed 474

Question, I've a situation where I filter with a RegExp the Cypress tests, in the Cypress plugins folder. This check is done on a javascript level with a custom lib that check with git diff the updated files of the branch I am in, during the execution of Cypress tests. This functionality works properly both locally and within a Jenkins pipeline.

There are some cases where this RegExp filters all tests out, meaning: there is no need to run any Cypress tests (what it means is that it was touched some code that is not affecting anything regarding components of a UI library, so any Cypress tests is necessary).

Is there a way to prevent in this case, the failure of Cypress suite? I would love that my package.json script that run Cypress tests is executed but without failure of the command if zero tests are found.

$ cypress run --headless
[ '**/time.**' ]
Can't run because no spec files were found.

We searched for any files inside of this folder:

cypress/integration/functional
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! .......
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ....... test:cy:run:functional script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! .npm/_logs/2021-08-20T06_41_30_883Z-debug.log
Error: Command failed with exit code 1: npm run test:cy:run:functional

{
  command: 'npm run test:cy:run:functional',
  exitCode: 1,
  signal: undefined,
  signalDescription: undefined,
  stdout: undefined,
  stderr: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}

Thanks in advance

1 Answers

The simplest answer is add a dummy/token/smoke test that always succeeds and never filters out, e.g expect(true).to.eq(true).

Share any details of the test selection? If it's on test timestamp, you will need to touch the dummy test prior to each run.


Cypress Module API allows more control over the test.

Useful blog Wrap Cypress Using NPM Module API

Possible suppression of error - a bit of a punt, depends if the error originates from Cypress or your own script.

cypress.run(options)
  .then((runResults) => {
    if (runResults.status === 'failed') {
      // Cypress could not run, something is terrible wrong
      console.error(runResults.message)

      // Suppress fail if error is "no spec files found"
      const exitCode = runResults.message.contians('no spec files were found') ? 0 : 1;
      return process.exit(exitCode)
    }
  })
Related