Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use testPathPattern as a filter on top of findRelatedTests #8311

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
92837e0
use testPathPattern as a filter on top of findRelatedTests
Apr 11, 2019
808564a
null check testPathPattern and eventually get around to fixing failin…
Apr 11, 2019
f7eafee
use _filterTestPathsWithStats to to filter by with testPathPattern
Apr 12, 2019
5eddcbd
remove unnecessary code
Apr 12, 2019
d83b4c6
update _filterTestPathsWithStats condition
Apr 12, 2019
7ee3e54
convert findRelatedTests parameter from a boolean to a string array
Apr 17, 2019
9d7aed1
fix linting issues
Apr 17, 2019
813fab3
fix instances of findRelatedTests
mackness Apr 24, 2019
b9ad8bc
explicitly check for more than 0 related tests
mackness May 4, 2019
2613dd0
explicit find related tests length check
mackness May 4, 2019
17b4310
fix failing normalize spec
mackness May 4, 2019
f95a0d4
normalize and null check find related tests
mackness Jun 2, 2019
bfa2569
Merge branch 'master' into find-related-tests-bug
mackness Jun 2, 2019
a85dd4b
lockfile
mackness Jun 2, 2019
4e99139
udpate show config and log debug messages snapshots
mackness Jun 2, 2019
4169622
update find related files snapshots
mackness Jun 2, 2019
9c001f7
udpate summary message in find related tests spec
mackness Jun 2, 2019
144e6f0
add a simple --testPathPattern assertion
mackness Jun 3, 2019
e73ecd7
disable prefer-const rule
mackness Jun 3, 2019
c33284d
disable no-unresolved rule for fsevents
mackness Jun 3, 2019
a938c25
Merge branch 'master' into find-related-tests-bug
mackness Jun 8, 2019
7dc3269
remove unused @ts-ignore comments
mackness Jun 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
expand: false,
extraGlobals: [],
filter: null,
findRelatedTests: false,
findRelatedTests: [],
forceExit: false,
globalSetup: null,
globalTeardown: null,
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/findRelatedFiles.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites related to files matching /a.js|b.js/i.
Ran all test suites.
`;

exports[`--findRelatedTests flag coverage configuration is applied correctly 2`] = `
Expand Down Expand Up @@ -50,7 +50,7 @@ Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites related to files matching /a.js/i.
Ran all test suites.
`;

exports[`--findRelatedTests flag generates coverage report for filename 5`] = `
Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"errorOnDeprecated": false,
"expand": false,
"filter": null,
"findRelatedTests": [],
"globalSetup": null,
"globalTeardown": null,
"json": false,
Expand Down
41 changes: 39 additions & 2 deletions e2e/__tests__/findRelatedFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('--findRelatedTests flag', () => {
const {stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']);
expect(stderr).toMatch('PASS __tests__/test.test.js');

const summaryMsg = 'Ran all test suites related to files matching /a.js/i.';
const summaryMsg = 'Ran all test suites.';
expect(stderr).toMatch(summaryMsg);
});

Expand Down Expand Up @@ -87,7 +87,7 @@ describe('--findRelatedTests flag', () => {
expect(stderr).toMatch('PASS __tests__/test.test.js');
expect(stderr).not.toMatch('PASS __tests__/test-skip-deps.test.js');

const summaryMsg = 'Ran all test suites related to files matching /a.js/i.';
const summaryMsg = 'Ran all test suites.';
expect(stderr).toMatch(summaryMsg);
});

Expand Down Expand Up @@ -191,4 +191,41 @@ describe('--findRelatedTests flag', () => {
expect(stdout).toMatch('No tests found');
expect(stderr).toBe('');
});

test('exclude tests that do not match --testPathPattern flag', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/a.test.js': `
require('../a');
test('a', () => expect(1).toBe(1));
`,
'__tests__/b.test.js': `
require('../b');
test('b', () => expect(1).toBe(1));
`,
'a.js': 'module.exports = {}',
'b.js': 'module.exports = {}',
'package.json': JSON.stringify({
jest: {
collectCoverage: true,
collectCoverageFrom: ['b.js', 'a.js'],
testEnvironment: 'node',
},
}),
});

/* eslint-disable prefer-const */
let stdout;
let stderr;
({stdout, stderr} = runJest(
DIR,
['--findRelatedTests', 'a.js', 'b.js', '--testPathPattern', 'xyz'],
{
stripAnsi: true,
},
));

expect(stdout).toMatch('No tests found');
expect(stderr).toBe('');
});
});
5 changes: 1 addition & 4 deletions packages/jest-cli/src/__tests__/cli/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe('check', () => {

it('raises an exception if findRelatedTests is specified with no file paths', () => {
const argv = {
_: [] as Array<string>,
findRelatedTests: true,
findRelatedTests: [],
} as Config.Argv;
expect(() => check(argv)).toThrow(
'The --findRelatedTests option requires file paths to be specified',
Expand All @@ -64,11 +63,9 @@ describe('check', () => {

describe('buildArgv', () => {
it('should return only camelcased args ', () => {
// @ts-ignore
const mockProcessArgv = jest
.spyOn(process.argv, 'slice')
.mockImplementation(() => ['--clear-mocks']);
// @ts-ignore
const actual = buildArgv(null);
expect(actual).not.toHaveProperty('clear-mocks');
expect(actual).toHaveProperty('clearMocks', true);
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const check = (argv: Config.Argv) => {
}
}

if (argv.findRelatedTests && argv._.length === 0) {
if (argv.findRelatedTests && argv.findRelatedTests.length === 0) {
throw new Error(
'The --findRelatedTests option requires file paths to be specified.\n' +
'Example usage: jest --findRelatedTests ./src/source.js ' +
Expand Down Expand Up @@ -265,7 +265,7 @@ export const options = {
'Find related tests for a list of source files that were ' +
'passed in as arguments. Useful for pre-commit hook integration to run ' +
'the minimal amount of tests necessary.',
type: 'boolean' as 'boolean',
type: 'array' as 'array',
},
forceExit: {
default: undefined,
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,11 @@ describe('findRelatedTests', () => {
rootDir: '/root/path/foo/',
},
{
_: [
findRelatedTests: [
`/root/path/${sourceFile}`,
sourceFile,
`<rootDir>/bar/${sourceFile}`,
],
findRelatedTests: true,
},
);

Expand Down
16 changes: 14 additions & 2 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,14 +950,26 @@ export default function normalize(
);
}

if (newOptions.findRelatedTests && newOptions.findRelatedTests.length > 0) {
newOptions.findRelatedTests = newOptions.findRelatedTests.filter(Boolean);
}

if (argv.findRelatedTests && argv.findRelatedTests.length > 0) {
argv.findRelatedTests = argv.findRelatedTests.filter(Boolean);
}

// If collectCoverage is enabled while using --findRelatedTests we need to
// avoid having false negatives in the generated coverage report.
// The following: `--findRelatedTests '/rootDir/file1.js' --coverage`
// Is transformed to: `--findRelatedTests '/rootDir/file1.js' --coverage --collectCoverageFrom 'file1.js'`
// where arguments to `--collectCoverageFrom` should be globs (or relative
// paths to the rootDir)
if (newOptions.collectCoverage && argv.findRelatedTests) {
let collectCoverageFrom = argv._.map(filename => {
if (
newOptions.collectCoverage &&
argv.findRelatedTests &&
argv.findRelatedTests.length > 0
) {
let collectCoverageFrom = argv.findRelatedTests.map(filename => {
filename = replaceRootDirInPath(options.rootDir, filename);
return path.isAbsolute(filename)
? path.relative(options.rootDir, filename)
Expand Down
16 changes: 12 additions & 4 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type SearchResult = {

export type TestSelectionConfig = {
input?: string;
findRelatedTests?: boolean;
findRelatedTests?: Array<string>;
onlyChanged?: boolean;
paths?: Array<Config.Path>;
shouldTreatInputAsPattern?: boolean;
Expand Down Expand Up @@ -265,11 +265,19 @@ export default class SearchSource {
);
} else if (globalConfig.runTestsByPath && paths && paths.length) {
return this.findTestsByPaths(paths);
} else if (globalConfig.findRelatedTests && paths && paths.length) {
return this.findRelatedTestsFromPattern(
paths,
} else if (globalConfig.findRelatedTests.length > 0) {
const relatedTests = this.findRelatedTestsFromPattern(
globalConfig.findRelatedTests,
globalConfig.collectCoverage,
);
if (globalConfig.testPathPattern != null) {
return this._filterTestPathsWithStats(
relatedTests.tests,
globalConfig.testPathPattern,
);
} else {
return relatedTests;
}
} else if (globalConfig.testPathPattern != null) {
return this.findMatchingTests(globalConfig.testPathPattern);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ exports[`prints the config object 1`] = `
"expand": false,
"extraGlobals": [],
"filter": null,
"findRelatedTests": false,
"findRelatedTests": [],
"forceExit": false,
"globalSetup": null,
"globalTeardown": null,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export default (async function runJest({

if (
globalConfig.passWithNoTests ||
globalConfig.findRelatedTests ||
(globalConfig.findRelatedTests &&
globalConfig.findRelatedTests.length > 0) ||
globalConfig.lastCommit ||
globalConfig.onlyChanged
) {
Expand Down
1 change: 0 additions & 1 deletion packages/jest-haste-map/src/lib/FSEventsWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import path from 'path';
import {EventEmitter} from 'events';
import anymatch from 'anymatch';
import micromatch from 'micromatch';
// eslint-disable-next-line
import {Watcher} from 'fsevents';
// @ts-ignore no types
import walker from 'walker';
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-reporters/src/summary_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ export default class SummaryReporter extends BaseReporter {
globalConfig: Config.GlobalConfig,
) {
const getMatchingTestsInfo = () => {
const prefix = globalConfig.findRelatedTests
? ' related to files matching '
: ' matching ';
const prefix =
globalConfig.findRelatedTests.length > 0
? ' related to files matching '
: ' matching ';

return (
chalk.dim(prefix) +
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type InitialOptions = {
expand?: boolean;
extraGlobals?: Array<string>;
filter?: Path;
findRelatedTests?: boolean;
findRelatedTests?: Array<string>;
forceCoverageMatch?: Array<Glob>;
forceExit?: boolean;
json?: boolean;
Expand Down Expand Up @@ -313,7 +313,7 @@ export type GlobalConfig = {
expand: boolean;
extraGlobals: Array<string>;
filter: Path | null | undefined;
findRelatedTests: boolean;
findRelatedTests: Array<string>;
forceExit: boolean;
json: boolean;
globalSetup: string | null | undefined;
Expand Down Expand Up @@ -444,7 +444,7 @@ export type Argv = Arguments<
debug: boolean;
env: string;
expand: boolean;
findRelatedTests: boolean;
findRelatedTests: Array<string>;
forceExit: boolean;
globals: string;
globalSetup: string | null | undefined;
Expand Down