From e83c2d9eb1e0d829bca2ccdc54a7fbb542446eb6 Mon Sep 17 00:00:00 2001 From: Mark Phippard Date: Wed, 11 Apr 2018 14:38:57 -0400 Subject: [PATCH] feat(schematics): run tests for affected apps Add support for affected:test that works similar to the e2e target except it runs test instead. Theoretically useful for a CI process to run the unit tests based on the apps affected by the change. This is WIP towards Issue #416 --- e2e/schematics/command-line.test.ts | 5 +++++ .../application/files/__directory__/package.json | 1 + .../migrations/20180410-adding-affected-test.ts | 13 +++++++++++++ .../application/files/__directory__/package.json | 1 + .../schematics/src/collection/workspace/index.ts | 2 ++ packages/schematics/src/command-line/affected.ts | 9 ++++++--- 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 packages/schematics/migrations/20180410-adding-affected-test.ts diff --git a/e2e/schematics/command-line.test.ts b/e2e/schematics/command-line.test.ts index 414389c27b8de..4ec0883052088 100644 --- a/e2e/schematics/command-line.test.ts +++ b/e2e/schematics/command-line.test.ts @@ -160,6 +160,11 @@ describe('Command line', () => { 'npm run affected:e2e -- --files="libs/mylib/index.ts"' ); expect(e2e).toContain('should display welcome message'); + + const test = runCommand( + 'npm run affected:test -- --files="libs/mylib/index.ts"' + ); + expect(test).toContain('should display welcome message'); }, 1000000 ); diff --git a/packages/bazel/src/collection/application/files/__directory__/package.json b/packages/bazel/src/collection/application/files/__directory__/package.json index 3608f1d1ab4c4..9a6fabd600902 100644 --- a/packages/bazel/src/collection/application/files/__directory__/package.json +++ b/packages/bazel/src/collection/application/files/__directory__/package.json @@ -12,6 +12,7 @@ "affected:apps": "./node_modules/.bin/nx affected apps", "affected:build": "./node_modules/.bin/nx affected build", "affected:e2e": "./node_modules/.bin/nx affected e2e", + "affected:test": "./node_modules/.bin/nx affected test", "affected:dep-graph": "./node_modules/.bin/nx affected dep-graph", "format": "./node_modules/.bin/nx format write", "format:write": "./node_modules/.bin/nx format write", diff --git a/packages/schematics/migrations/20180410-adding-affected-test.ts b/packages/schematics/migrations/20180410-adding-affected-test.ts new file mode 100644 index 0000000000000..5c2dc1e914ca0 --- /dev/null +++ b/packages/schematics/migrations/20180410-adding-affected-test.ts @@ -0,0 +1,13 @@ +import { updateJsonFile } from '../src/utils/fileutils'; + +export default { + description: 'Update npm scripts to use the nx command', + run: () => { + updateJsonFile('package.json', json => { + json.scripts = { + ...json.scripts, + 'affected:test': './node_modules/.bin/nx affected test' + }; + }); + } +}; diff --git a/packages/schematics/src/collection/application/files/__directory__/package.json b/packages/schematics/src/collection/application/files/__directory__/package.json index 39b3541b589ad..c82c1fa9a5cc7 100755 --- a/packages/schematics/src/collection/application/files/__directory__/package.json +++ b/packages/schematics/src/collection/application/files/__directory__/package.json @@ -12,6 +12,7 @@ "affected:apps": "./node_modules/.bin/nx affected apps", "affected:build": "./node_modules/.bin/nx affected build", "affected:e2e": "./node_modules/.bin/nx affected e2e", + "affected:test": "./node_modules/.bin/nx affected test", "affected:dep-graph": "./node_modules/.bin/nx affected dep-graph", "format": "./node_modules/.bin/nx format write", "format:write": "./node_modules/.bin/nx format write", diff --git a/packages/schematics/src/collection/workspace/index.ts b/packages/schematics/src/collection/workspace/index.ts index 1568ed27e38a0..ed8901d19dfb4 100755 --- a/packages/schematics/src/collection/workspace/index.ts +++ b/packages/schematics/src/collection/workspace/index.ts @@ -84,6 +84,8 @@ function updatePackageJson() { packageJson.scripts['affected:build'] = './node_modules/.bin/nx affected build'; packageJson.scripts['affected:e2e'] = './node_modules/.bin/nx affected e2e'; + packageJson.scripts['affected:test'] = + './node_modules/.bin/nx affected test'; packageJson.scripts['affected:dep-graph'] = './node_modules/.bin/nx affected dep-graph'; diff --git a/packages/schematics/src/command-line/affected.ts b/packages/schematics/src/command-line/affected.ts index 7d219431f9312..fe57d34165ee0 100644 --- a/packages/schematics/src/command-line/affected.ts +++ b/packages/schematics/src/command-line/affected.ts @@ -29,7 +29,10 @@ export function affected(args: string[]): void { build(apps, rest); break; case 'e2e': - e2e(apps, rest); + runTests('e2e', apps, rest); + break; + case 'test': + runTests('test', apps, rest); break; case 'dep-graph': generateGraph(yargsParser(rest), projects); @@ -77,11 +80,11 @@ function build(apps: string[], rest: string[]) { } } -function e2e(apps: string[], rest: string[]) { +function runTests(type: string, apps: string[], rest: string[]) { if (apps.length > 0) { console.log(`Testing ${apps.join(', ')}`); apps.forEach(app => { - execSync(`node ${ngPath()} e2e ${rest.join(' ')} -a=${app}`, { + execSync(`node ${ngPath()} ${type} ${rest.join(' ')} -a=${app}`, { stdio: [0, 1, 2] }); });