How to make Cypress use test files located outside of default integration folder?

2.9k views Asked by At

I am trying to keep my **.spec.js files for testing next to the actual files that need to be tested like such:

.
├── product
|   ├── product.js
|   ├── product.spec.js
├── user
|   ├── user.js
|   ├── user.spec.js

The above *.spec.js files don't appear in the cypress test runner window and I can't find out how to add them.

It only the displays the contents of the ./cypress/integration folder which contains the example tests included by cypress.

I know you can change the configuration of Cypress to look at a different default folder (other than cypress/integrations) for tests but my *.spec.js files are spread across several different folders.

Is is possible to have cypress test files in different folders and appear in the GUI test runner?

3

There are 3 answers

3
Fody On BEST ANSWER

In the configuration file cypress.json, add (for example)

{
  ...
  "integrationFolder": ".",
  "testFiles": ["cypress/integration/**/*.spec.js", "src/**/*.test.js"]
}

integrationFolder: "." designates the project root to start scan for test files.

But be careful, can pick up tests in node_modules, so use an array of locations in the testFiles option to indicate folders that have valid tests for your project.

1
aldrine00 On

Why not delete the example tests/folder inside the integration folder and put yours instead?

integration
 ├── product
 |   ├── product.js
 |   ├── product.spec.js
 ├── user
 |   ├── user.js
 |   ├── user.spec.js
0
secretAgent On

in v13.3.0 (and probably from version 10+), config is in the cypress.config.js, in the repo's root.

this, with added specPattern line, should work for you:

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  component: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
    specPattern: '**/*.spec.js',
  },
});

any file(s) in entire repo (including sub-direrctories), with extensions .spec.js, should match now.

official documentation for component tests:

https://docs.cypress.io/guides/references/configuration#component

( i had similar problem, but for end-2-end tests and solution is on the QA/Test portal: https://sqa.stackexchange.com/questions/52091/cypress-js-how-to-change-default-path-for-your-e2e-tests )