fix(testing): provide descriptive message when the project does not have targets defined. (#9795)

when trying to add cypress to a project that does not contain targets 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 message is logged 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
This commit is contained in:
Caleb Ukle 2022-04-12 14:38:59 -05:00 committed by GitHub
parent 46e7b4d49c
commit 02cf4be49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -413,6 +413,23 @@ describe('schematic:cypress-project', () => {
expect(projectConfig.tags).toEqual([]);
});
});
it('should not throw an error when --project does not have targets', async () => {
const projectConf = readProjectConfiguration(tree, 'my-app');
delete projectConf.targets;
updateProjectConfiguration(tree, 'my-app', projectConf);
await cypressProjectGenerator(tree, {
name: 'my-app-e2e',
project: 'my-app',
linter: Linter.EsLint,
});
const projectConfig = readProjectConfiguration(tree, 'my-app-e2e');
expect(projectConfig.targets['e2e'].options.devServerTarget).toEqual(
'my-app:serve'
);
});
});
describe('--linter', () => {

View File

@ -13,6 +13,8 @@ import {
Tree,
updateJson,
ProjectConfiguration,
stripIndents,
logger,
} from '@nrwl/devkit';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
@ -82,8 +84,15 @@ function addProject(tree: Tree, options: CypressProjectSchema) {
};
} else if (options.project) {
const project = readProjectConfiguration(tree, options.project);
if (!project.targets) {
logger.warn(stripIndents`
NOTE: Project, "${options.project}", does not have any targets defined and a baseUrl was not provided. Nx will use
"${options.project}:serve" as the devServerTarget. But you may need to define this target within the project, "${options.project}".
`);
}
const devServerTarget =
project.targets.serve && project.targets.serve.defaultConfiguration
project.targets?.serve && project.targets?.serve?.defaultConfiguration
? `${options.project}:serve:${project.targets.serve.defaultConfiguration}`
: `${options.project}:serve`;
e2eProjectConfig = {