@nrwl/react:cypress-component-configuration

Add a Cypress component testing configuration to an existing project.

Can I use component testing?

React component testing with Nx requires Cypress version 10.7.0 and up.

You can migrate with to v10 via the migrate-to-cypress-10 generator.

This generator is for Cypress based component testing.

If you want to test components via Storybook with Cypress, then check out the storybook-configuration generator docs

This generator is designed to get your React project up and running with Cypress Component Testing.

nx g @nrwl/react:cypress-component-project --project=my-cool-react-project

Running this generator, adds the required files to the specified project with a preconfigured cypress.config.ts designed for Nx workspaces.

cypress.config.ts
import { defineConfig } from 'cypress'; import { nxComponentTestingPreset } from '@nrwl/react/plugins/component-testing'; export default defineConfig({ component: nxComponentTestingPreset(__filename), });

Here is an example on how to add custom options to the configuration

cypress.config.ts
import { defineConfig } from 'cypress'; import { nxComponentTestingPreset } from '@nrwl/react/plugins/component-testing'; export default defineConfig({ component: { ...nxComponentTestingPreset(__filename), // extra options here }, });

Specifying a Build Target

Component testing requires a build target to correctly run the component test dev server. This option can be manually specified with --build-target=some-react-app:build, but Nx will infer this usage from the project graph if one isn't provided.

For React projects, the build target needs to be using the @nrwl/webpack:webpack executor. The generator will throw an error if a build target can't be found and suggest passing one in manually.

Letting Nx infer the build target by default

nx g @nrwl/react:cypress-component-project --project=my-cool-react-project

Manually specifying the build target

nx g @nrwl/react:cypress-component-project --project=my-cool-react-project --build-target:some-react-app:build --generate-tests
Build Target with Configuration

If you're wanting to use a build target with a specific configuration. i.e. my-app:build:production, then manually providing --build-target=my-app:build:production is the best way to do that.

Auto Generating Tests

You can optionally use the --generate-tests flag to generate a test file for each component in your project.

nx g @nrwl/react:cypress-component-project --project=my-cool-react-project --generate-tests

Running Component Tests

A new component-test target will be added to the specified project to run your component tests.

nx g component-test my-cool-react-project

Here is an example of the project configuration that is generated. The --build-target option is added as the devServerTarget which can be changed as needed.

project.json
{ "targets" { "component-test": { "executor": "@nrwl/cypress:cypress", "options": { "cypressConfig": "<path-to-project-root>/cypress.config.ts", "testingType": "component", "devServerTarget": "some-react-app:build", "skipServe": true } } } }

Nx also supports Angular component testing.

Usage

nx generate cypress-component-configuration ...

By default, Nx will search for cypress-component-configuration in the default collection provisioned in workspace.json.

You can specify the collection explicitly as follows:

nx g @nrwl/react:cypress-component-configuration ...

Show what will be generated without writing to disk:

nx g cypress-component-configuration ... --dry-run

Examples

Add component testing to your react project:

nx g @nrwl/react:cypress-component-configuration --project=my-react-project

Add component testing to your react project and generate component tests for your existing components:

nx g @nrwl/react:cypress-component-configuration --project=my-react-project --generate-tests

Options

buildTarget

string
Pattern: ^[^:\\s]+:[^:\\s]+(:\\S+)?$

A build target used to configure Cypress component testing in the format of project:target[:configuration]. The build target should be from a React app. If not provided we will try to infer it from your projects usage.

generateTests

boolean
Default: false

Generate default component tests for existing components in the project

project

Required
string

The name of the project to add cypress component testing configuration to

skipFormat

boolean
Default: false

Skip formatting files