Cypress: Running selected tests only


Overview

When you start a Cypress run, normally you have to wait quite a time until all your tests run through. You might have multiple Cypress scripts and each of them might have multiple tests.

So you might want to have more control over which tests are supposed to be included in a Cypress run. There are several mechanisms that give you more control in selecting the tests you want to run which we discuss here.

Selecting tests on the script level

Normally if you call the npx cypress run command, all Cypress scripts in the cypress/integration/ folder are executed.

You can have more control by using the --spec option:

npx cypress run --spec cypress/integration/businessApps/*.js

Selecting by folder name

The --spec option allows also filtering depending on folder name pattern:

cypress run --spec 'cypress/integration/examples/**/*.js'

So this command executes any file with the pattern *.js in any of the folders under examples-Folder.

Selecting by multiple filters

The -–spec option also allows the combination of multiple filters. You just have to comma separate the different filters:

cypress run --spec 'cypress/integration/examples/*,cypress/integration/somefolder/**/*,cypress/integration/somefile.js'

Exclusion from Selection

You can also use the --spec option to exclude files as follows:

cypress run --spec 'cypress/integration/somefolder/*[!_draft].js'

In the example above all Files with “_draft” in the file, name are excluded from the run

Selecting tests inside a script

We can also define the tests we want to execute inside a Cypress tests script file. If we want to execute only one specific test among many tests we can use the it.only(…) option:

In the following example only the test #2 will be executed and all the others are left out

it('test #1', () => {
  // ...
})
it.only('test #2', () => {
  // ...
})
it ('test #2', () => {
  // ...
})

We can also run several tests and exclude some others from execution using the it.skip(…) option:

In the following example only the test #1 will be excluded from the run and all the other tests are included in the run:

It.skip('test #1', () => {
  // ...
})
it ('test #2', () => {
  // ...
})
it ('test #2', () => {
  // ...
})

But the solution above relies on hard coding our selection which might be ok if we are developing a test locally and can easily change the code to run this or that test but if our code is in the pipeline where we can’t change the code this approach is not suited to select or deselect tests from the run. For this, there is a better approach explained here https://glebbahmutov.com/blog/cypress-grep-filters/

Troubleshooting

Error: Can’t run because no spec files were found

Problem

We use Cypress version 12.2.0 and try to run the following cypress command in WSL using the pwsh prompt:

yarn cypress run --spec '../some-folder-outside-cypress-root/cypress/e2e/*.cy.ts' --browser chrome

And we receive the following error:

...
Can't run because no spec files were found.

We searched for specs matching this glob pattern:

  > /home/someuser/some-folder-outside-cypress-root/cypress/e2e/*.cy.ts

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Resolution

Use the following command and run it directly in bash and not in pwsh !

yarn cypress run –config ‘{“specPattern”:[“../some-folder-outside-cypress-root/cypress/e2e/*.cy.ts”]}’ –browser chrome

Related topics: