Writing and Organizing Tests
What you'll learnβ
- How to organize your tests in Cypress and the types of supported files
- How to write tests in Cypress including hooks, exclusions, and configurations
- Test statuses and how to interpret them
- What files are automatically watched while writing Cypress tests
Folder structureβ
After adding a new project, Cypress will automatically scaffold out a suggested folder structure. By default it will create:
- JavaScript
- TypeScript
E2E:
/cypress.config.js
/cypress/fixtures/example.json
/cypress/support/commands.js
/cypress/support/e2e.js
Component:
/cypress.config.js
/cypress/fixtures/example.json
/cypress/support/commands.js
/cypress/support/component.js
/cypress/support/component-index.html
Both:
/cypress.config.js
/cypress/fixtures/example.json
/cypress/support/commands.js
/cypress/support/e2e.js
/cypress/support/component.js
/cypress/support/component-index.html
E2E:
/cypress.config.ts
/cypress/fixtures/example.json
/cypress/support/commands.ts
/cypress/support/e2e.ts
Component:
/cypress.config.ts
/cypress/fixtures/example.json
/cypress/support/commands.ts
/cypress/support/component.ts
/cypress/support/component-index.html
Both:
/cypress.config.ts
/cypress/fixtures/example.json
/cypress/support/commands.ts
/cypress/support/e2e.ts
/cypress/support/component.ts
/cypress/support/component-index.html
Configuring Folder Structureβ
While Cypress allows you to configure where your tests, fixtures, and support files are located, if you're starting your first project, we recommend you use the above structure.
You can modify the folder configuration in your configuration file. See the Cypress configuration for more detail.
Cypress will create a
screenshotsFolder
and a
videosFolder
to store the
screenshots and videos taken during the testing of your application. Many users
will opt to add these folders to their .gitignore
file. Additionally, if you
are storing sensitive environment variables in your
Cypress configuration or
cypress.env.json
,
these should also be ignored when you check into source control.
Spec filesβ
Test files are located in cypress/e2e
by default, but can be
configured to another
directory. Test files may be written as:
.js
.jsx
.ts
.tsx
.coffee
.cjsx
Cypress also supports ES2015
out of the box. You can use either
ES2015 modules
or CommonJS modules
. This means you can import
or require
both npm packages and local relative modules.
Check out our recipe using ES2015 and CommonJS modules.
To see an example of every command used in Cypress, open the
2-advanced-examples
folder
within your cypress/e2e
folder.
Fixture Filesβ
Fixtures are used as external pieces of static data that can be used by your
tests. Fixture files are located in cypress/fixtures
by default, but can be
configured to another
directory.
You would typically use them with the cy.fixture()
command and most often when you're stubbing
Network Requests.
Asset Filesβ
There are some folders that may be generated after a test run, containing assets that were generated during the test run.
You may consider adding these folders to your .gitignore
file to ignore
checking these files into source control.