Skip to content

Commit

Permalink
fix(testing): provide more descriptive error when the target project …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
barbados-clemens committed Apr 11, 2022
1 parent e4e5c3b commit 4e37666
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Expand Up @@ -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';
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit 4e37666

Please sign in to comment.