Skip to content

Commit

Permalink
fix(testing): provide more descriptive message when the target projec…
Browse files Browse the repository at this point in the history
…t does not have targets defined.

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 9396db0 commit 8a9de60
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`schematic:cypress-project Cypress Project --project should not throw an error when --project does not have targets 1`] = `
Object {
"configurations": Object {
"production": Object {
"devServerTarget": "my-app:serve:production",
},
},
"executor": "@nrwl/cypress:cypress",
"options": Object {
"cypressConfig": "apps/my-app-e2e/cypress.json",
"devServerTarget": "my-app:serve",
},
}
`;

exports[`schematic:cypress-project Cypress Project nested should set right path names in \`tsconfig.e2e.json\` 1`] = `
Object {
"compilerOptions": Object {
Expand Down
Expand Up @@ -413,6 +413,21 @@ 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']).toMatchSnapshot();
});
});

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 8a9de60

Please sign in to comment.