How can I get Cypress to run a specific folder of tests by specifying it as a project?

Jas picture Jas · Jan 16, 2019 · Viewed 7k times · Source

I have the following directory structure for working with Cypress:

cypress-automation
    cypress
        fixtures
        integration
           apps
              pulse
              smhw-prod
              smhw-qa
                  folder-a
                       sample-spec.js
        examples
        plugins
        screenshots
        support
        videos
        node_modules
        cypress.json // this is where the file currently is
        package-lock.json
        package.json

Expectation

What I want to do is run all the tests inside the smhw-qa folder (there are a number of spec files in there) .. and be able to pass this using the --project command using CLI.

Currently if I just run `--run`` without any other arguments all the spec files, from all folders will start to run which is not desirable, since there are multiple applications "projects" (smhw-qa, smhw-prod etc) within this structure. This will allow me to only run the spec files from a single folder as desired.

I am also aware of using the --run command to "run" a specific folder, but I want to use "projects" instead to organise these tests so it's easier to identify them later.

I have had a look at the docs which show the use of the --project command however I need to help to understand what else I need to setup in order to make this work. By this, I am referring to the package.json file.

What I tried so far
I tried to follow the example provided for testing "nested folders" from here: https://github.com/cypress-io/cypress-test-nested-projects

package.json: added the following script:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "smhw-qa": "cypress run --project ./cypress/integration/apps/smhw-qa"
  },

When I run this via the terminal like so:

➜  cypress-automation npx cypress run --project ./cypress/integration/apps/smhw-qa

I get an error:

Can't run because no spec files were found.

We searched for any files inside of this folder:

/Users/jaswindersingh/Documents/cypress-automation/cypress/integration/apps/smhw-qa/cypress/integration

Am I missing something? Should my cypress.json file be located elsewhere?

I'd like to be able to set each of the folders inside "apps" to be projects, and then be able to run these projects using the cypress run --project command.

This will also make it easier to run specific folders of tests when I hookup our CI so that only specific projects (and their spec files) run when I choose.

Answer

jnotelddim picture jnotelddim · Aug 16, 2019

I think you're looking for --spec path/to/folder/*.js

You can run all the tests in a folder, or even in all subfolders of a folder,

ex/ npx cypress run --spec cypresss/integration/subsetA/**/*-spec.js

would run all .js specs in all folders under the "subsetA" folder in cypress/integration.

So in your case: npx cypress run --spec cypress/integration/apps/smhw-qa/**/*-spec.js should do the trick.

https://docs.cypress.io/guides/guides/command-line.html#How-to-run-commands