@nx/angular:cypress-component-configuration
Add a Cypress component testing configuration to an existing project. Cypress v10.7.0 or higher is required.
Monorepo World: October 7, 2024Monorepo World: October 7, 2024Join us!
Add a Cypress component testing configuration to an existing project. Cypress v10.7.0 or higher is required.
Angular component testing with Nx requires Cypress version 10.7.0 and up.
You can migrate with to v11 via the migrate-to-cypress-11 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. However, this functionality is deprecated, and will be removed on Nx version 18.
This generator is designed to get your Angular project up and running with Cypress Component Testing.
❯
nx g @nx/angular:cypress-component-configuration --project=my-cool-angular-project
Running this generator, adds the required files to the specified project with a preconfigured cypress.config.ts
designed for Nx workspaces.
1import { defineConfig } from 'cypress';
2import { nxComponentTestingPreset } from '@nx/angular/plugins/component-testing';
3
4export default defineConfig({
5 component: nxComponentTestingPreset(__filename),
6});
7
Here is an example on how to add custom options to the configuration
1import { defineConfig } from 'cypress';
2import { nxComponentTestingPreset } from '@nx/angular/plugins/component-testing';
3
4export default defineConfig({
5 component: {
6 ...nxComponentTestingPreset(__filename),
7 // extra options here
8 },
9});
10
Component testing requires a build target to correctly run the component test dev server. This option can be manually specified with --build-target=some-angular-app:build
, but Nx will infer this usage from the project graph if one isn't provided.
For Angular projects, the build target needs to be using the @nx/angular:webpack-browser
or @angular-devkit/build-angular:browser
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 @nx/angular:cypress-component-configuration --project=my-cool-angular-project
Manually specifying the build target
❯
nx g @nx/angular:cypress-component-configuration --project=my-cool-angular-project --build-target:some-angular-app:build --generate-tests
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.
You can optionally use the --generate-tests
flag to generate a test file for each component in your project.
❯
nx g @nx/angular:cypress-component-configuration --project=my-cool-angular-project --generate-tests
A new component-test
target will be added to the specified project to run your component tests.
❯
nx g component-test my-cool-angular-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.
1{
2 "targets" {
3 "component-test": {
4 "executor": "@nx/cypress:cypress",
5 "options": {
6 "cypressConfig": "<path-to-project-root>/cypress.config.ts",
7 "testingType": "component",
8 "devServerTarget": "some-angular-app:build",
9 "skipServe": true
10 }
11 }
12 }
13}
14
When the project being tested is a dependent of the specified --build-target
, then assets, scripts, and styles are applied to the component being tested. You can determine if the project is dependent by using the project graph. If there is no link between the two projects, then the assets, scripts, and styles won't be included in the build; therefore, they will not be applied to the component. To have a link between projects, you can import from the project being tested into the specified --build-target
project, or set the --build-target
project to implicitly depend on the project being tested.
Nx also supports React component testing.
1nx generate cypress-component-configuration ...
2
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:
1nx g @nx/angular:cypress-component-configuration ...
2
Show what will be generated without writing to disk:
1nx g cypress-component-configuration ... --dry-run
2
The name of the project to add cypress component testing configuration to
^[^:\\s]+:[^:\\s]+(:\\S+)?$
A build target used to configure Cypress component testing in the format of project:target[:configuration]
. The build target should be an angular app. If not provided we will try to infer it from your projects usage.
false
Generate default component tests for existing components in the project
false
Skip formatting files