From 3aafa8bb6af4ed89637278f0490d0e4fc7b649fc Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Mon, 3 Jan 2022 16:01:42 -0500 Subject: [PATCH] feat(testing): project generator should accept baseUrl --- docs/angular/api-cypress/executors/cypress.md | 2 +- .../api-cypress/generators/cypress-project.md | 6 ++ docs/node/api-cypress/executors/cypress.md | 2 +- .../api-cypress/generators/cypress-project.md | 6 ++ docs/react/api-cypress/executors/cypress.md | 2 +- .../api-cypress/generators/cypress-project.md | 6 ++ package.json | 2 +- .../cypress/src/executors/cypress/schema.json | 2 +- .../cypress-project/cypress-project.spec.ts | 31 +++++++ .../cypress-project/cypress-project.ts | 83 ++++++++++++------- .../generators/cypress-project/schema.d.ts | 1 + .../generators/cypress-project/schema.json | 4 + 12 files changed, 113 insertions(+), 34 deletions(-) diff --git a/docs/angular/api-cypress/executors/cypress.md b/docs/angular/api-cypress/executors/cypress.md index a872003d40..6bf9fc46b6 100644 --- a/docs/angular/api-cypress/executors/cypress.md +++ b/docs/angular/api-cypress/executors/cypress.md @@ -21,7 +21,7 @@ The path of the Cypress configuration json file. Type: `string` -Use this to pass directly the address of your distant server address with the port running your application +The address (with the port) which your application is running on ### browser diff --git a/docs/angular/api-cypress/generators/cypress-project.md b/docs/angular/api-cypress/generators/cypress-project.md index 7cdcc5dae4..1b61a714ea 100644 --- a/docs/angular/api-cypress/generators/cypress-project.md +++ b/docs/angular/api-cypress/generators/cypress-project.md @@ -35,6 +35,12 @@ Type: `string` Name of the E2E Project +### baseUrl + +Type: `string` + +The address (with the port) which your application is running on + ### directory Type: `string` diff --git a/docs/node/api-cypress/executors/cypress.md b/docs/node/api-cypress/executors/cypress.md index 555addb70a..e0d92c7942 100644 --- a/docs/node/api-cypress/executors/cypress.md +++ b/docs/node/api-cypress/executors/cypress.md @@ -21,7 +21,7 @@ The path of the Cypress configuration json file. Type: `string` -Use this to pass directly the address of your distant server address with the port running your application +The address (with the port) which your application is running on ### browser diff --git a/docs/node/api-cypress/generators/cypress-project.md b/docs/node/api-cypress/generators/cypress-project.md index ce63480296..3c5505aa92 100644 --- a/docs/node/api-cypress/generators/cypress-project.md +++ b/docs/node/api-cypress/generators/cypress-project.md @@ -35,6 +35,12 @@ Type: `string` Name of the E2E Project +### baseUrl + +Type: `string` + +The address (with the port) which your application is running on + ### directory Type: `string` diff --git a/docs/react/api-cypress/executors/cypress.md b/docs/react/api-cypress/executors/cypress.md index 555addb70a..e0d92c7942 100644 --- a/docs/react/api-cypress/executors/cypress.md +++ b/docs/react/api-cypress/executors/cypress.md @@ -21,7 +21,7 @@ The path of the Cypress configuration json file. Type: `string` -Use this to pass directly the address of your distant server address with the port running your application +The address (with the port) which your application is running on ### browser diff --git a/docs/react/api-cypress/generators/cypress-project.md b/docs/react/api-cypress/generators/cypress-project.md index ce63480296..3c5505aa92 100644 --- a/docs/react/api-cypress/generators/cypress-project.md +++ b/docs/react/api-cypress/generators/cypress-project.md @@ -35,6 +35,12 @@ Type: `string` Name of the E2E Project +### baseUrl + +Type: `string` + +The address (with the port) which your application is running on + ### directory Type: `string` diff --git a/package.json b/package.json index 3d9acaa234..c77f216f70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nrwl/nx-source", - "version": "13.4.2", + "version": "13.4.3-beta.1", "description": "Smart, Fast and Extensible Build System", "homepage": "https://nx.dev", "private": true, diff --git a/packages/cypress/src/executors/cypress/schema.json b/packages/cypress/src/executors/cypress/schema.json index ef47ca2d3d..b85e1aa849 100644 --- a/packages/cypress/src/executors/cypress/schema.json +++ b/packages/cypress/src/executors/cypress/schema.json @@ -54,7 +54,7 @@ }, "baseUrl": { "type": "string", - "description": "Use this to pass directly the address of your distant server address with the port running your application" + "description": "The address (with the port) which your application is running on" }, "browser": { "type": "string", diff --git a/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts b/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts index 549d5aad78..1ae61257a8 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts @@ -119,6 +119,36 @@ describe('schematic:cypress-project', () => { }); }); + it('should add update `workspace.json` file (baseUrl)', async () => { + await cypressProjectGenerator(tree, { + name: 'my-app-e2e', + project: 'my-app', + baseUrl: 'http://localhost:3000', + linter: Linter.TsLint, + standaloneConfig: false, + }); + const workspaceJson = readJson(tree, 'workspace.json'); + const project = workspaceJson.projects['my-app-e2e']; + + expect(project.root).toEqual('apps/my-app-e2e'); + + expect(project.architect.lint).toEqual({ + builder: '@angular-devkit/build-angular:tslint', + options: { + tsConfig: ['apps/my-app-e2e/tsconfig.json'], + exclude: ['**/node_modules/**', '!apps/my-app-e2e/**/*'], + }, + }); + expect(project.architect.e2e).toEqual({ + builder: '@nrwl/cypress:cypress', + options: { + cypressConfig: 'apps/my-app-e2e/cypress.json', + baseUrl: 'http://localhost:3000', + tsConfig: 'apps/my-app-e2e/tsconfig.json', + }, + }); + }); + 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'; @@ -321,6 +351,7 @@ describe('schematic:cypress-project', () => { await cypressProjectGenerator(tree, { ...defaultOptions, name: 'my-app-e2e', + baseUrl: 'http://localhost:7788', }); const workspaceJson = readJson( diff --git a/packages/cypress/src/generators/cypress-project/cypress-project.ts b/packages/cypress/src/generators/cypress-project/cypress-project.ts index 5f1ee391ee..091d443650 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.ts @@ -57,39 +57,64 @@ function createFiles(tree: Tree, options: CypressProjectSchema) { } function addProject(tree: Tree, options: CypressProjectSchema) { - let devServerTarget: string = `${options.project}:serve`; - if (options.project) { - const project = readProjectConfiguration(tree, options.project); - devServerTarget = - project.targets.serve && project.targets.serve.defaultConfiguration - ? `${options.project}:serve:${project.targets.serve.defaultConfiguration}` - : devServerTarget; - } - - const project: ProjectConfiguration = { - root: options.projectRoot, - sourceRoot: joinPathFragments(options.projectRoot, 'src'), - projectType: 'application', - targets: { - e2e: { - executor: '@nrwl/cypress:cypress', - options: { - cypressConfig: joinPathFragments(options.projectRoot, 'cypress.json'), - devServerTarget, - }, - configurations: { - production: { - devServerTarget: `${options.project}:serve:production`, + let e2eProjectConfig: ProjectConfiguration; + if (options.baseUrl) { + e2eProjectConfig = { + root: options.projectRoot, + sourceRoot: joinPathFragments(options.projectRoot, 'src'), + projectType: 'application', + targets: { + e2e: { + executor: '@nrwl/cypress:cypress', + options: { + cypressConfig: joinPathFragments( + options.projectRoot, + 'cypress.json' + ), + baseUrl: options.baseUrl, }, }, }, - }, - tags: [], - implicitDependencies: options.project ? [options.project] : undefined, - }; + tags: [], + implicitDependencies: options.project ? [options.project] : undefined, + }; + } else if (options.project) { + const project = readProjectConfiguration(tree, options.project); + const devServerTarget = + project.targets.serve && project.targets.serve.defaultConfiguration + ? `${options.project}:serve:${project.targets.serve.defaultConfiguration}` + : `${options.project}:serve`; + e2eProjectConfig = { + root: options.projectRoot, + sourceRoot: joinPathFragments(options.projectRoot, 'src'), + projectType: 'application', + targets: { + e2e: { + executor: '@nrwl/cypress:cypress', + options: { + cypressConfig: joinPathFragments( + options.projectRoot, + 'cypress.json' + ), + devServerTarget, + }, + configurations: { + production: { + devServerTarget: `${options.project}:serve:production`, + }, + }, + }, + }, + tags: [], + implicitDependencies: options.project ? [options.project] : undefined, + }; + } else { + throw new Error(`Either project or baseUrl should be specified.`); + } + const detectedCypressVersion = installedCypressVersion() ?? cypressVersion; if (detectedCypressVersion < 7) { - project.targets.e2e.options.tsConfig = joinPathFragments( + e2eProjectConfig.targets.e2e.options.tsConfig = joinPathFragments( options.projectRoot, 'tsconfig.json' ); @@ -97,7 +122,7 @@ function addProject(tree: Tree, options: CypressProjectSchema) { addProjectConfiguration( tree, options.projectName, - project, + e2eProjectConfig, options.standaloneConfig ); } diff --git a/packages/cypress/src/generators/cypress-project/schema.d.ts b/packages/cypress/src/generators/cypress-project/schema.d.ts index 266bd98157..165801c31c 100644 --- a/packages/cypress/src/generators/cypress-project/schema.d.ts +++ b/packages/cypress/src/generators/cypress-project/schema.d.ts @@ -2,6 +2,7 @@ import { Linter } from '@nrwl/linter'; export interface Schema { project?: string; + baseUrl?: string; name: string; directory?: string; linter?: Linter; diff --git a/packages/cypress/src/generators/cypress-project/schema.json b/packages/cypress/src/generators/cypress-project/schema.json index 1d5adbe899..4b1b1a6f04 100644 --- a/packages/cypress/src/generators/cypress-project/schema.json +++ b/packages/cypress/src/generators/cypress-project/schema.json @@ -12,6 +12,10 @@ "$source": "projectName" } }, + "baseUrl": { + "type": "string", + "description": "The address (with the port) which your application is running on" + }, "name": { "type": "string", "description": "Name of the E2E Project",