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 1fa119a4be94d..ff77c2858f205 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.spec.ts @@ -11,9 +11,10 @@ import { cypressProjectGenerator } from './cypress-project'; import { Schema } from './schema'; import { Linter } from '@nrwl/linter'; import { installedCypressVersion } from '../../utils/cypress-version'; +import { cypressInitGenerator } from '../init/init'; jest.mock('../../utils/cypress-version'); - +jest.mock('../init/init'); describe('Cypress Project', () => { let tree: Tree; const defaultOptions: Omit = { @@ -23,6 +24,8 @@ describe('Cypress Project', () => { let mockedInstalledCypressVersion: jest.Mock< ReturnType > = installedCypressVersion as never; + let mockInitCypress: jest.Mock> = + cypressInitGenerator as never; beforeEach(() => { tree = createTreeWithEmptyV1Workspace(); @@ -55,6 +58,26 @@ describe('Cypress Project', () => { }); afterEach(() => jest.clearAllMocks()); + it('should call init if cypress is not installed', async () => { + mockedInstalledCypressVersion.mockReturnValue(null); + await cypressProjectGenerator(tree, { + ...defaultOptions, + name: 'my-app-e2e', + project: 'my-app', + }); + expect(mockInitCypress).toHaveBeenCalled(); + }); + + it('should call not init if cypress is installed', async () => { + mockedInstalledCypressVersion.mockReturnValue(10); + await cypressProjectGenerator(tree, { + ...defaultOptions, + name: 'my-app-e2e', + project: 'my-app', + }); + expect(mockInitCypress).not.toHaveBeenCalled(); + }); + describe('> v10', () => { beforeEach(() => { mockedInstalledCypressVersion.mockReturnValue(10); diff --git a/packages/cypress/src/generators/cypress-project/cypress-project.ts b/packages/cypress/src/generators/cypress-project/cypress-project.ts index f03005f285818..86eaa3f8a1b2f 100644 --- a/packages/cypress/src/generators/cypress-project/cypress-project.ts +++ b/packages/cypress/src/generators/cypress-project/cypress-project.ts @@ -27,6 +27,7 @@ import { cypressVersion, eslintPluginCypressVersion, } from '../../utils/versions'; +import { cypressInitGenerator } from '../init/init'; // app import { Schema } from './schema'; @@ -248,13 +249,21 @@ export async function addLinter(host: Tree, options: CypressProjectSchema) { export async function cypressProjectGenerator(host: Tree, schema: Schema) { const options = normalizeOptions(host, schema); + const tasks = []; + const cypressVersion = installedCypressVersion(); + // if there is an installed cypress version, then we don't call + // init since we want to keep the existing version that is installed + if (!cypressVersion) { + tasks.push(cypressInitGenerator(host, options)); + } createFiles(host, options); addProject(host, options); const installTask = await addLinter(host, options); + tasks.push(installTask); if (!options.skipFormat) { await formatFiles(host); } - return installTask; + return runTasksInSerial(...tasks); } function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {