diff --git a/TestUtils.ts b/TestUtils.ts index 7150270a7b2e..65777a08b43d 100644 --- a/TestUtils.ts +++ b/TestUtils.ts @@ -25,7 +25,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { expand: false, extraGlobals: [], filter: null, - findRelatedTests: false, + findRelatedTests: [], forceExit: false, globalSetup: null, globalTeardown: null, diff --git a/e2e/__tests__/__snapshots__/findRelatedFiles.test.ts.snap b/e2e/__tests__/__snapshots__/findRelatedFiles.test.ts.snap index d0183e881440..f8238286ae0b 100644 --- a/e2e/__tests__/__snapshots__/findRelatedFiles.test.ts.snap +++ b/e2e/__tests__/__snapshots__/findRelatedFiles.test.ts.snap @@ -5,7 +5,7 @@ Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> -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`] = ` @@ -50,7 +50,7 @@ Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: <> -Ran all test suites related to files matching /a.js/i. +Ran all test suites. `; exports[`--findRelatedTests flag generates coverage report for filename 5`] = ` diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index dbafaaad3ca1..0e9396207875 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -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, diff --git a/e2e/__tests__/findRelatedFiles.test.ts b/e2e/__tests__/findRelatedFiles.test.ts index a5a8e2e686fa..ee6b5f57ba21 100644 --- a/e2e/__tests__/findRelatedFiles.test.ts +++ b/e2e/__tests__/findRelatedFiles.test.ts @@ -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); }); @@ -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); }); @@ -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(''); + }); }); diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index 8b1c3a291637..958b4a6bd980 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -39,8 +39,7 @@ describe('check', () => { it('raises an exception if findRelatedTests is specified with no file paths', () => { const argv = { - _: [] as Array, - findRelatedTests: true, + findRelatedTests: [], } as Config.Argv; expect(() => check(argv)).toThrow( 'The --findRelatedTests option requires file paths to be specified', @@ -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); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 7bba9a038621..21865229bee7 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -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 ' + @@ -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, diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 3930cd473e09..e42cc906ab9b 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -248,12 +248,11 @@ describe('findRelatedTests', () => { rootDir: '/root/path/foo/', }, { - _: [ + findRelatedTests: [ `/root/path/${sourceFile}`, sourceFile, `/bar/${sourceFile}`, ], - findRelatedTests: true, }, ); diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index d31047ac538d..d2170f0e6955 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -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) diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index 5e9a8d330a80..7b955d69cdca 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -28,7 +28,7 @@ export type SearchResult = { export type TestSelectionConfig = { input?: string; - findRelatedTests?: boolean; + findRelatedTests?: Array; onlyChanged?: boolean; paths?: Array; shouldTreatInputAsPattern?: boolean; @@ -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 { diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap index 357612825f0a..ceee7270e43c 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap @@ -82,7 +82,7 @@ exports[`prints the config object 1`] = ` "expand": false, "extraGlobals": [], "filter": null, - "findRelatedTests": false, + "findRelatedTests": [], "forceExit": false, "globalSetup": null, "globalTeardown": null, diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index b0906aeb616e..905b13c09d9b 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -212,7 +212,8 @@ export default (async function runJest({ if ( globalConfig.passWithNoTests || - globalConfig.findRelatedTests || + (globalConfig.findRelatedTests && + globalConfig.findRelatedTests.length > 0) || globalConfig.lastCommit || globalConfig.onlyChanged ) { diff --git a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts index b0ebad2867dd..95b515b083dc 100644 --- a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts +++ b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts @@ -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'; diff --git a/packages/jest-reporters/src/summary_reporter.ts b/packages/jest-reporters/src/summary_reporter.ts index e94cffb2d0a5..f1ae33c2bf00 100644 --- a/packages/jest-reporters/src/summary_reporter.ts +++ b/packages/jest-reporters/src/summary_reporter.ts @@ -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) + diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 00a78f0bd1ca..fc9d82e9bce3 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -142,7 +142,7 @@ export type InitialOptions = { expand?: boolean; extraGlobals?: Array; filter?: Path; - findRelatedTests?: boolean; + findRelatedTests?: Array; forceCoverageMatch?: Array; forceExit?: boolean; json?: boolean; @@ -313,7 +313,7 @@ export type GlobalConfig = { expand: boolean; extraGlobals: Array; filter: Path | null | undefined; - findRelatedTests: boolean; + findRelatedTests: Array; forceExit: boolean; json: boolean; globalSetup: string | null | undefined; @@ -444,7 +444,7 @@ export type Argv = Arguments< debug: boolean; env: string; expand: boolean; - findRelatedTests: boolean; + findRelatedTests: Array; forceExit: boolean; globals: string; globalSetup: string | null | undefined;