From 0d9b8bb0dbd0d6f6f9f8cdfdeafb0c494b95c12b Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Thu, 11 Jun 2020 21:42:28 -0400 Subject: [PATCH] fix(testing): do not add cypress-eslint-plugin if eslint not used (#3162) --- .../add-cypress-eslint-plugin-9-4-0.spec.ts | 43 +++++++++++++------ .../add-cypress-eslint-plugin-9-4-0.ts | 22 +++++++--- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.spec.ts b/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.spec.ts index f1080c3d3da66..0e30d93bfd442 100644 --- a/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.spec.ts +++ b/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.spec.ts @@ -1,18 +1,20 @@ import { Tree } from '@angular-devkit/schematics'; -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import { readJsonInTree, serializeJson } from '@nrwl/workspace'; -import * as path from 'path'; +import { + readJsonInTree, + serializeJson, + updateJsonInTree, +} from '@nrwl/workspace'; +import { callRule, runMigration } from '../../utils/testing'; describe('Add Cypress ESLint Plugin 9.4.0', () => { let tree: Tree; - let schematicRunner: SchematicTestRunner; beforeEach(() => { tree = Tree.empty(); tree.create( 'package.json', serializeJson({ - devDependencies: { '@nrwl/cypress': '9.3.0' }, + devDependencies: { '@nrwl/cypress': '9.3.0', eslint: '6.8.0' }, }) ); @@ -53,17 +55,14 @@ describe('Add Cypress ESLint Plugin 9.4.0', () => { extends: ['../../.eslintrc'], }) ); - - schematicRunner = new SchematicTestRunner( - '@nrwl/cypress', - path.join(__dirname, '../../../migrations.json') - ); }); it('should add eslint-plugin-cypress devDependency', async () => { - const result = await schematicRunner - .runSchematicAsync('add-cypress-eslint-plugin-9.4.0', {}, tree) - .toPromise(); + const result = await runMigration( + 'add-cypress-eslint-plugin-9.4.0', + {}, + tree + ); const { devDependencies } = readJsonInTree(result, '/package.json'); const projectOneEslintrcJson = readJsonInTree( @@ -83,4 +82,22 @@ describe('Add Cypress ESLint Plugin 9.4.0', () => { 'plugin:cypress/recommended' ); }); + + it('should not add eslint-plugin-cypress if eslint is not used', async () => { + tree = await callRule( + updateJsonInTree('package.json', (json) => { + delete json.devDependencies.eslint; + return json; + }), + tree + ); + + const result = await runMigration( + 'add-cypress-eslint-plugin-9.4.0', + {}, + tree + ); + const { devDependencies } = readJsonInTree(result, '/package.json'); + expect(devDependencies['eslint-plugin-cypress']).not.toBeDefined(); + }); }); diff --git a/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.ts b/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.ts index 8db1e4bcfe238..425f923e5a061 100644 --- a/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.ts +++ b/packages/cypress/src/migrations/update-9-4-0/add-cypress-eslint-plugin-9-4-0.ts @@ -1,4 +1,4 @@ -import { chain, Rule, Tree } from '@angular-devkit/schematics'; +import { chain, noop, Rule, Tree } from '@angular-devkit/schematics'; import { addDepsToPackageJson, formatFiles, @@ -47,9 +47,19 @@ async function addCypressPluginToEslintrc(host: Tree) { } export default () => { - return chain([ - addDepsToPackageJson({}, { 'eslint-plugin-cypress': '^2.10.3' }), - addCypressPluginToEslintrc, - formatFiles(), - ]); + return (host: Tree) => { + const packageJson = readJsonInTree(host, 'package.json'); + if ( + packageJson.devDependencies?.eslint || + packageJson.dependencies?.eslint + ) { + return chain([ + addDepsToPackageJson({}, { 'eslint-plugin-cypress': '^2.10.3' }), + addCypressPluginToEslintrc, + formatFiles(), + ]); + } else { + return noop(); + } + }; };