From 4e376668748509f5f5a0f3f204fb569ee5ebd7a3 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Mon, 11 Apr 2022 17:10:00 -0500 Subject: [PATCH] fix(testing): provide more descriptive error when the target project does not have a serve target. when trying to add cypress to a project that does not contain a serve target the project will error out because of a null reference. Now when trying to add to a project without a serve target a more descriptive error is throw stating to add a serve target or use the --baseUrl flag Note: a future update should add the parameter to override what target to use if so desired. ISSUES CLOSED: #9756 --- .../cypress-project/cypress-project.spec.ts | 16 ++++++++++++++++ .../cypress-project/cypress-project.ts | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts b/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts index 783a8cf17cc52..779ff66affa68 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts @@ -149,6 +149,22 @@ describe('schematic:cypress-project', () => { }); }); + it('should throw an error if the --project does not have a serve target', async () => { + const projectConf = readProjectConfiguration(tree, 'my-app'); + delete projectConf.targets; + updateProjectConfiguration(tree, 'my-app', projectConf); + + await expect( + cypressProjectGenerator(tree, { + name: 'my-app-e2e', + project: 'my-app', + linter: Linter.EsLint, + }) + ).rejects.toThrow( + 'Unable to find a serve target for the provided project, "my-app". Please define a serve target or provide a baseUrl via the --baseUrl flag.' + ); + }); + it('should add update `workspace.json` file for a project with a defaultConfiguration', async () => { const originalProject = readProjectConfiguration(tree, 'my-app'); originalProject.targets.serve.defaultConfiguration = 'development'; diff --git a/packages/cypress/src/generators/cypress-project/cypress-project.ts b/packages/cypress/src/generators/cypress-project/cypress-project.ts index 27c9e0fbabf8c..9b3a8554f770e 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.ts @@ -13,6 +13,7 @@ import { Tree, updateJson, ProjectConfiguration, + stripIndents, } from '@nrwl/devkit'; import { Linter, lintProjectGenerator } from '@nrwl/linter'; import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; @@ -82,6 +83,13 @@ function addProject(tree: Tree, options: CypressProjectSchema) { }; } else if (options.project) { const project = readProjectConfiguration(tree, options.project); + + if (!project.targets?.serve) { + throw new Error( + `Unable to find a serve target for the provided project, "${options.project}". Please define a serve target or provide a baseUrl via the --baseUrl flag.` + ); + } + const devServerTarget = project.targets.serve && project.targets.serve.defaultConfiguration ? `${options.project}:serve:${project.targets.serve.defaultConfiguration}`