fix(testing): handle cypress-project generation for standalone projects (#13602)

This commit is contained in:
Caleb Ukle 2022-12-05 09:12:32 -06:00 committed by GitHub
parent 856045343f
commit 6f8a849d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 14 deletions

View File

@ -111,12 +111,6 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
}
},
"required": ["name"],

View File

@ -245,6 +245,32 @@ describe('Cypress Project', () => {
const tsConfig = readJson(tree, 'apps/my-dir/my-app-e2e/tsconfig.json');
expect(tsConfig.extends).toBe('../../../tsconfig.json');
});
describe('root project', () => {
it('should generate in option.name when root project detected', async () => {
addProjectConfiguration(tree, 'root', {
root: '.',
});
await cypressProjectGenerator(tree, {
...defaultOptions,
name: 'e2e-tests',
baseUrl: 'http://localhost:1234',
project: 'root',
rootProject: true,
});
expect(tree.listChanges().map((c) => c.path)).toEqual(
expect.arrayContaining([
'e2e-tests/cypress.config.ts',
'e2e-tests/src/e2e/app.cy.ts',
'e2e-tests/src/fixtures/example.json',
'e2e-tests/src/support/app.po.ts',
'e2e-tests/src/support/commands.ts',
'e2e-tests/src/support/e2e.ts',
'e2e-tests/tsconfig.json',
])
);
});
});
});
describe('--project', () => {

View File

@ -16,6 +16,7 @@ import {
toJS,
Tree,
updateJson,
getProjects,
} from '@nrwl/devkit';
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
@ -285,11 +286,26 @@ function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
options.directory
);
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
let projectName, projectRoot;
let projectName: string;
let projectRoot: string;
let maybeRootProject: ProjectConfiguration;
let isRootProject = false;
if (options.rootProject) {
const projects = getProjects(host);
// nx will set the project option for generators when ran within a project.
// since the root project will always be set for standlone projects we can just check it here.
if (options.project) {
maybeRootProject = projects.get(options.project);
}
if (
maybeRootProject?.root === '.' ||
// should still check to see if we are in a standalone based workspace
Array.from(projects.values()).some((config) => config.root === '.')
) {
projectName = options.name;
projectRoot = options.name;
isRootProject = true;
} else {
projectName = filePathPrefix(
projectDirectory ? `${projectDirectory}-${options.name}` : options.name
@ -306,6 +322,8 @@ function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
options.linter = options.linter || Linter.EsLint;
return {
...options,
// other generators depend on the rootProject flag down stream
rootProject: isRootProject,
projectName,
projectRoot,
};

View File

@ -59,12 +59,6 @@
"type": "boolean",
"default": false,
"description": "Do not add dependencies to `package.json`."
},
"rootProject": {
"description": "Create a application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
}
},
"required": ["name"],