Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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'> = {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -27,6 +27,7 @@ import {
cypressVersion,
eslintPluginCypressVersion,
} from '../../utils/versions';
import { cypressInitGenerator } from '../init/init';
// app
import { Schema } from './schema';

Expand Down Expand Up @@ -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 {
Expand Down