Skip to content

Cypress is a test runner built for the modern web. It has a lot of great features:

  • Time travel
  • Real-time reloads
  • Automatic waiting
  • Spies, stubs, and clocks
  • Network traffic control
  • Screenshots and videos

Info about Cypress Component Testing can be found here

In any Nx workspace, you can install @nx/cypress by running the following command:

nx add @nx/cypress

This will install the correct version of @nx/cypress.

The @nx/cypress plugin will create a task for any project that has a Cypress configuration file present. Any of the following files will be recognized as a Cypress configuration file:

  • cypress.config.js
  • cypress.config.ts
  • cypress.config.mjs
  • cypress.config.cjs

To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project --web in the command line.

The @nx/cypress/plugin is configured in the plugins array in nx.json.

nx.json
{
"plugins": [
{
"plugin": "@nx/cypress/plugin",
"options": {
"targetName": "e2e",
"ciTargetName": "e2e-ci",
"componentTestingTargetName": "component-test",
"openTargetName": "open-cypress"
}
}
]
}

The targetName, ciTargetName, componentTestingTargetName, and open-cypress options control the names of the inferred Cypress tasks. The default names are e2e, e2e-ci, component-test, and open-cypress.

The @nx/cypress/plugin will automatically split your e2e tasks by file. You can read more about the Atomizer feature here.

To enable e2e task splitting, make sure there is a ciWebServerCommand property set in your cypress.config.ts file. It will look something like this:

apps/my-project-e2e/cypress.config.ts
import { defineConfig } from 'cypress';
import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset';
export default defineConfig({
e2e: {
...nxE2EPreset(__filename, {
cypressDir: 'src',
bundler: 'vite',
webServerCommands: {
default: 'nx run my-project:serve',
production: 'nx run my-project:preview',
},
ciWebServerCommand: 'nx run my-project:serve-static',
}),
baseUrl: 'http://localhost:4200',
},
});

By default, when creating a new frontend application, Nx will use Cypress to create the e2e tests project.

nx g @nx/web:app apps/frontend

To configure Cypress for an existing project, run the following generator:

nx g @nx/cypress:configuration --project=your-app-name

Optionally, you can use the --baseUrl option if you don't want the Cypress plugin to serve your-app-name.

nx g @nx/cypress:configuration --project=your-app-name --baseUrl=http://localhost:4200

Replace your-app-name with the app's name as defined in your project.json file or the name property of your package.json.

Run nx e2e frontend-e2e to execute e2e tests with Cypress.

You can run your e2e test against a production build by using the production configuration

nx e2e frontend-e2e --configuration=production

You can use the --spec flag to glob for test files.

# run the tests in the smoke/ directory
nx e2e frontend-e2e --spec="**smoke/**"
# run the tests in smoke/ directory and with dashboard in the file name
nx e2e frontend-e2e --spec="**smoke/**,**dashboard.cy**"

By default, Cypress will run in headless mode. You will have the result of all the tests and errors (if any) in your terminal. Screenshots and videos will be accessible in dist/cypress/apps/frontend/screenshots and dist/cypress/apps/frontend/videos.

You can also run Cypress in headed mode and watching for changes. This is a great way to enhance the dev workflow. You can build up test files with the application running and Cypress will re-run those tests as you enhance and add to the suite.

nx open-cypress frontend-e2e

The baseUrl property provides you the ability to test an application hosted on a specific domain.

nx e2e frontend-e2e --config="baseUrl=https://frontend.com"

If you need to fine tune your Cypress setup, you can do so by modifying cypress.config.ts in the project root. For instance, you can easily add your projectId to save all the screenshots and videos into your Cypress dashboard. The complete configuration is documented on the official website.

For adding more dynamic configurations to your Cypress configuration, you can look into using setupNodeEvents configuration option.

If you need to pass a variable to Cypress that you don't want to commit to your repository (i.e. API keys, dynamic values based on configurations, API URLs), you can use Cypress environment variables.

There are a handful of ways to pass environment variables to Cypress, but the most common is going to be via the cypress.env.json file, the -e Cypress arg or the env option from the @nx/cypress:cypress executor in the project configuration or the command line.

Create a cypress.env.json file in the projects root (i.e. apps/my-cool-app-e2e/cypress.env.json). Cypress will automatically pick up this file. This method is helpful for configurations that you don't want to commit. Just don't forget to add the file to the .gitignore and add documentation so people in your repo know what values to populate in their local copy of the cypress.env.json file.

Setting the -e Cypress arg or the env option from the @nx/cypress:cypress executor in the project configuration is a good way to add values you want to define that you don't mind committing to the repository, such as a base API URL.

project.json
{
...
"targets": {
"e2e": {
"options": {
"args": "--env=API_URL=https://api.my-nx-website.com"
}
}
}
}

Finally, you can also pass environment variables via the command line with the -e Cypress arg or the --env option for the @nx/cypress:cypress executor.

nx e2e frontend-e2e -e=API_URL=https://api.my-nx-website.com,API_KEY=abc-123