Skip to content

Commit

Permalink
fix(testing): run init generator in cypress-project when cypress is n…
Browse files Browse the repository at this point in the history
…ot installed
  • Loading branch information
barbados-clemens committed Oct 12, 2022
1 parent 7d19df0 commit 4b0bff7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit 4b0bff7

Please sign in to comment.