Skip to content

Commit

Permalink
--changedFilesWithAncestor (#4070)
Browse files Browse the repository at this point in the history
* --changedFilesWithAncestor

* Update git.js
  • Loading branch information
aaronabramov authored and cpojer committed Jul 19, 2017
1 parent 74eb6d9 commit 3c37b21
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"framework\\": \\"jasmine2\\",
\\"globalConfig\\": {
\\"bail\\": false,
\\"changedFilesWithAncestor\\": false,
\\"coverageDirectory\\": \\"<<REPLACED_ROOT_DIR>>/coverage\\",
\\"coverageReporters\\": [
\\"json\\",
Expand Down
51 changes: 51 additions & 0 deletions integration_tests/__tests__/only_changed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import path from 'path';
const skipOnWindows = require('skipOnWindows');
const DIR = path.resolve(os.tmpdir(), 'jest_only_changed');
const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com';
const HG = 'hg --config ui.username=jest_test';

skipOnWindows.suite();

Expand Down Expand Up @@ -138,3 +139,53 @@ test('onlyChanged in config is overwritten by --all or testPathPattern', () => {
expect(stderr).toMatch('PASS __tests__/file2.test.js');
expect(stderr).toMatch('PASS __tests__/file3.test.js');
});

test('gets changed files for hg', async () => {
if (process.env.CI) {
// Circle and Travis have very old version of hg (v2, and current
// version is v4.2) and its API changed since then and not compatible
// any more. Changing the SCM version on CIs is not trivial, so we'll just
// skip this test and run it only locally.
return;
}
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/file1.test.js': `require('../file1'); test('file1', () => {});`,
'file1.js': 'module.exports = {}',
'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
});

run(`${HG} init`, DIR);
run(`${HG} add .`, DIR);
run(`${HG} commit -m "test"`, DIR);

let stdout;
let stderr;

({stdout, stderr} = runJest(DIR, ['-o']));
expect(stdout).toMatch('No tests found related to files changed');

writeFiles(DIR, {
'__tests__/file2.test.js': `require('../file2'); test('file2', () => {});`,
'file2.js': 'module.exports = {}',
'file3.js': `require('./file2')`,
});

({stdout, stderr} = runJest(DIR, ['-o']));
expect(stderr).toMatch('PASS __tests__/file2.test.js');

run(`${HG} add .`, DIR);
run(`${HG} commit -m "test2"`, DIR);

writeFiles(DIR, {
'__tests__/file3.test.js': `require('../file3'); test('file3', () => {});`,
});

({stdout, stderr} = runJest(DIR, ['-o']));
expect(stderr).toMatch('PASS __tests__/file3.test.js');
expect(stderr).not.toMatch('PASS __tests__/file2.test.js');

({stdout, stderr} = runJest(DIR, ['-o', '--changedFilesWithAncestor']));
expect(stderr).toMatch('PASS __tests__/file2.test.js');
expect(stderr).toMatch('PASS __tests__/file3.test.js');
});
5 changes: 5 additions & 0 deletions packages/jest-changed-files/src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const adapter: SCMAdapter = {
cwd: string,
options?: Options,
): Promise<Array<Path>> => {
if (options && options.withAncestor) {
throw new Error(
'`changedFilesWithAncestor` is not supported in git repos.',
);
}
return new Promise((resolve, reject) => {
const args =
options && options.lastCommit
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-changed-files/src/hg.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const adapter: SCMAdapter = {
let stderr = '';
child.stdout.on('data', data => (stdout += data));
child.stderr.on('data', data => (stderr += data));
child.on('error', e => reject(e));
child.on('error', (error: Error) => reject(error));
child.on('close', code => {
if (code === 0) {
stdout = stdout.trim();
Expand All @@ -49,7 +49,7 @@ const adapter: SCMAdapter = {
);
}
} else {
reject(code + ': ' + stderr);
reject(new Error(code + ': ' + stderr));
}
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ const options = {
' dependency information.',
type: 'string',
},
changedFilesWithAncestor: {
description:
'When used together with `--onlyChanged`, it runs tests ' +
'related to the current changes and the changes made in the last commit. ' +
'(NOTE: this only works for hg repos)',
type: 'boolean',
},
ci: {
default: isCI,
description:
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/src/get_changed_files_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = (
);
return getChangedFilesForRoots(allRootsForAllProjects, {
lastCommit: globalConfig.lastCommit,
withAncestor: globalConfig.changedFilesWithAncestor,
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = ({
browser: false,
cache: true,
cacheDirectory,
changedFilesWithAncestor: false,
clearMocks: false,
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageReporters: ['json', 'text', 'lcov', 'clover'],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const getConfigs = (
return {
globalConfig: Object.freeze({
bail: options.bail,
changedFilesWithAncestor: options.changedFilesWithAncestor,
collectCoverage: options.collectCoverage,
collectCoverageFrom: options.collectCoverageFrom,
collectCoverageOnlyFrom: options.collectCoverageOnlyFrom,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ function normalize(options: InitialOptions, argv: Argv) {
case 'bail':
case 'browser':
case 'cache':
case 'changedFilesWithAncestor':
case 'clearMocks':
case 'collectCoverage':
case 'coverageReporters':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/valid_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = ({
browser: false,
cache: true,
cacheDirectory: '/tmp/user/jest',
changedFilesWithAncestor: false,
clearMocks: false,
collectCoverage: true,
collectCoverageFrom: ['src', '!public'],
Expand Down
1 change: 1 addition & 0 deletions types/Argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Argv = {|
browser: boolean,
cache: boolean,
cacheDirectory: string,
changedFilesWithAncestor: boolean,
clearMocks: boolean,
ci: boolean,
collectCoverage: boolean,
Expand Down
3 changes: 3 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type DefaultOptions = {|
browser: boolean,
cache: boolean,
cacheDirectory: Path,
changedFilesWithAncestor: boolean,
clearMocks: boolean,
coveragePathIgnorePatterns: Array<string>,
coverageReporters: Array<string>,
Expand Down Expand Up @@ -67,6 +68,7 @@ export type InitialOptions = {|
cache?: boolean,
cacheDirectory?: Path,
clearMocks?: boolean,
changedFilesWithAncestor?: boolean,
collectCoverage?: boolean,
collectCoverageFrom?: Array<Glob>,
collectCoverageOnlyFrom?: {[key: string]: boolean},
Expand Down Expand Up @@ -134,6 +136,7 @@ export type SnapshotUpdateState = 'all' | 'new' | 'none';

export type GlobalConfig = {|
bail: boolean,
changedFilesWithAncestor: boolean,
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
collectCoverageOnlyFrom: ?{[key: string]: boolean},
Expand Down

0 comments on commit 3c37b21

Please sign in to comment.