From 64485f7b36a3645aceb81010ac88a16c8bd21fd8 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Esun Date: Tue, 6 Feb 2018 13:07:23 +0000 Subject: [PATCH] Make lastCommit and changedFilesWithAncestor work through config --- CHANGELOG.md | 2 ++ packages/jest-changed-files/src/hg.js | 13 ++++++++++++- packages/jest-cli/src/__tests__/cli/args.test.js | 6 ------ packages/jest-cli/src/cli/args.js | 4 +--- packages/jest-config/src/defaults.js | 1 + packages/jest-config/src/normalize.js | 12 +++++++++++- packages/jest-config/src/valid_config.js | 1 + types/Config.js | 2 ++ 8 files changed, 30 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9531b5f7f67f..63cab92aa903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +* `[jest-config]` Allow lastComit and changedFilesWithAncestor via JSON config + ([#5476](https://github.com/facebook/jest/pull/5476)) * `[jest-util]` Add deletion to `process.env` as well ([#5466](https://github.com/facebook/jest/pull/5466)) * `[jest-util]` Add case-insensitive getters/setters to `process.env` diff --git a/packages/jest-changed-files/src/hg.js b/packages/jest-changed-files/src/hg.js index ccf4b923e818..949d7c8d33b0 100644 --- a/packages/jest-changed-files/src/hg.js +++ b/packages/jest-changed-files/src/hg.js @@ -17,6 +17,17 @@ const env = Object.assign({}, process.env, { HGPLAIN: 1, }); +const ANCESTORS = [ + // Parent commit to this one. + '.^', + + // The first commit of my branch, only if we are not on the default branch. + 'min(branch(.)) and not min(branch(default))', + + // Latest public commit. + 'max(public())', +]; + const adapter: SCMAdapter = { findChangedFiles: async ( cwd: string, @@ -25,7 +36,7 @@ const adapter: SCMAdapter = { return new Promise((resolve, reject) => { let args = ['status', '-amnu']; if (options && options.withAncestor) { - args.push('--rev', 'ancestor(.^)'); + args.push('--rev', `ancestor(${ANCESTORS.join(', ')})`); } else if (options && options.changedSince) { args.push('--rev', `ancestor(., ${options.changedSince})`); } else if (options && options.lastCommit === true) { diff --git a/packages/jest-cli/src/__tests__/cli/args.test.js b/packages/jest-cli/src/__tests__/cli/args.test.js index ad53b7dcc31a..88f3faf1c4c5 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.js +++ b/packages/jest-cli/src/__tests__/cli/args.test.js @@ -38,12 +38,6 @@ describe('check', () => { ); }); - it('sets onlyChanged if lastCommit is specified', () => { - const argv: Argv = {lastCommit: true}; - check(argv); - expect(argv.onlyChanged).toBe(true); - }); - it('raises an exception if findRelatedTests is specified with no file paths', () => { const argv: Argv = {_: [], findRelatedTests: true}; expect(() => check(argv)).toThrow( diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index a0b4c9366515..7f89eca93e26 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -26,9 +26,6 @@ export const check = (argv: Argv) => { 'changedFilesWithAncestor', 'changedSince', ]) { - if (argv[key]) { - argv.onlyChanged = true; - } if (argv[key] && argv.watchAll) { throw new Error( `Both --${key} and --watchAll were specified, but these two ` + @@ -113,6 +110,7 @@ export const options = { type: 'string', }, changedFilesWithAncestor: { + default: undefined, description: 'Runs tests related to the current changes and the changes made in the ' + 'last commit. Behaves similarly to `--onlyChanged`.', diff --git a/packages/jest-config/src/defaults.js b/packages/jest-config/src/defaults.js index 3ac8868ff79e..bdb8362dfe83 100644 --- a/packages/jest-config/src/defaults.js +++ b/packages/jest-config/src/defaults.js @@ -33,6 +33,7 @@ export default ({ browser: false, cache: true, cacheDirectory, + changedFilesWithAncestor: false, clearMocks: false, coveragePathIgnorePatterns: [NODE_MODULES_REGEXP], coverageReporters: ['json', 'text', 'lcov', 'clover'], diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 1bd405676c10..91a88ed1dc3f 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -474,6 +474,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'findRelatedTests': case 'forceCoverageMatch': case 'forceExit': + case 'lastCommit': case 'listTests': case 'logHeapUsage': case 'mapCoverage': @@ -521,7 +522,6 @@ export default function normalize(options: InitialOptions, argv: Argv) { newOptions.nonFlagArgs = argv._; newOptions.testPathPattern = buildTestPathPattern(argv); newOptions.json = argv.json; - newOptions.lastCommit = argv.lastCommit; newOptions.testFailureExitCode = parseInt(newOptions.testFailureExitCode, 10); @@ -571,6 +571,16 @@ export default function normalize(options: InitialOptions, argv: Argv) { ); } + for (const key of [ + 'lastCommit', + 'changedFilesWithAncestor', + 'changedSince', + ]) { + if (newOptions[key]) { + newOptions.onlyChanged = true; + } + } + return { hasDeprecationWarnings, options: newOptions, diff --git a/packages/jest-config/src/valid_config.js b/packages/jest-config/src/valid_config.js index dba9ab45f210..c2d4159c3b06 100644 --- a/packages/jest-config/src/valid_config.js +++ b/packages/jest-config/src/valid_config.js @@ -47,6 +47,7 @@ export default ({ providesModuleNodeModules: ['react', 'react-native'], }, json: false, + lastCommit: false, logHeapUsage: true, mapCoverage: false, moduleDirectories: ['node_modules'], diff --git a/types/Config.js b/types/Config.js index f5ca383ad1b1..48cbb287d172 100644 --- a/types/Config.js +++ b/types/Config.js @@ -27,6 +27,7 @@ export type DefaultOptions = {| browser: boolean, cache: boolean, cacheDirectory: Path, + changedFilesWithAncestor: boolean, clearMocks: boolean, coveragePathIgnorePatterns: Array, coverageReporters: Array, @@ -98,6 +99,7 @@ export type InitialOptions = { haste?: HasteConfig, reporters?: Array, logHeapUsage?: boolean, + lastCommit?: boolean, listTests?: boolean, mapCoverage?: boolean, moduleDirectories?: Array,