fix(testing): run init generator in cypress-project when cypress is n… (#12552)

This commit is contained in:
Caleb Ukle 2022-10-18 10:09:45 -07:00 committed by GitHub
parent 1f1559378f
commit ed05ca3d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -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<Schema, 'name' | 'project'> = {
@ -23,6 +24,8 @@ describe('Cypress Project', () => {
let mockedInstalledCypressVersion: jest.Mock<
ReturnType<typeof installedCypressVersion>
> = installedCypressVersion as never;
let mockInitCypress: jest.Mock<ReturnType<typeof cypressInitGenerator>> =
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);

View File

@ -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 {