Skip to content

Commit

Permalink
fix(testing): provide descriptive message when the project does not h…
Browse files Browse the repository at this point in the history
…ave 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
  • Loading branch information
barbados-clemens committed Apr 12, 2022
1 parent 46e7b4d commit 02cf4be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Expand Up @@ -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', () => {
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 02cf4be

Please sign in to comment.