Skip to content

Commit 9cd00e6

Browse files
valerybugakovcpojer
authored andcommitted
Allow unmatched test paths from CLI (#3882)
* Allow to pass the exact test filenames * Replaced custom check with fs.existsSync(), added tests * Changed licence to MIT * Fixed tests
1 parent ffec104 commit 9cd00e6

4 files changed

Lines changed: 89 additions & 6 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CLI accepts exact filenames 1`] = `
4+
"PASS ./bar.js
5+
● Console
6+
7+
console.log bar.js:2
8+
Hey
9+
10+
PASS foo/baz.js
11+
● Console
12+
13+
console.log foo/baz.js:2
14+
Hey
15+
16+
17+
"
18+
`;
19+
20+
exports[`CLI accepts exact filenames 2`] = `
21+
"Test Suites: 2 passed, 2 total
22+
Tests: 2 passed, 2 total
23+
Snapshots: 0 total
24+
Time: <<REPLACED>>
25+
Ran all test suites matching /.\\\\/bar.js|.\\\\/foo\\\\/baz.js/i.
26+
"
27+
`;
28+
29+
exports[`CLI accepts exact filenames 3`] = `""`;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
const path = require('path');
13+
const skipOnWindows = require('../../scripts/skip_on_windows');
14+
const {extractSummary, cleanup, writeFiles} = require('../utils');
15+
const runJest = require('../runJest');
16+
17+
const DIR = path.resolve(__dirname, '../cli_accepts_exact_filenames');
18+
19+
skipOnWindows.suite();
20+
21+
beforeEach(() => cleanup(DIR));
22+
afterAll(() => cleanup(DIR));
23+
24+
test('CLI accepts exact filenames', () => {
25+
writeFiles(DIR, {
26+
'bar.js': `
27+
test('bar', () => console.log('Hey'));
28+
`,
29+
'foo/baz.js': `
30+
test('baz', () => console.log('Hey'));
31+
`,
32+
'package.json': '{}',
33+
});
34+
35+
const {stderr, stdout, status} = runJest(DIR, [
36+
'-i',
37+
'--ci=false',
38+
'--forceExit',
39+
'./bar.js',
40+
'./foo/baz.js',
41+
]);
42+
const {rest, summary} = extractSummary(stderr);
43+
expect(status).toBe(0);
44+
expect(rest).toMatchSnapshot();
45+
expect(summary).toMatchSnapshot();
46+
expect(stdout).toMatchSnapshot();
47+
});

integration_tests/__tests__/find_related_files.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test('runs tests related to filename', () => {
3535
});
3636

3737
const {stdout} = runJest(DIR, ['a.js']);
38-
expect(stdout).toMatch(/no tests found/i);
38+
expect(stdout).toMatch('');
3939

4040
const {stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']);
4141
expect(stderr).toMatch('PASS __tests__/test.test.js');

packages/jest-cli/src/search_source.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {Glob, GlobalConfig, Path} from 'types/Config';
1212
import type {Test} from 'types/TestRunner';
1313
import type {ChangedFilesPromise} from 'types/ChangedFiles';
1414

15+
import fs from 'fs';
1516
import path from 'path';
1617
import micromatch from 'micromatch';
1718
import DependencyResolver from 'jest-resolve-dependencies';
@@ -207,12 +208,18 @@ export default class SearchSource {
207208
return Promise.resolve(this.findTestsByPaths(paths));
208209
} else if (globalConfig.findRelatedTests && paths && paths.length) {
209210
return Promise.resolve(this.findRelatedTestsFromPattern(paths));
210-
} else if (globalConfig.testPathPattern != null) {
211-
return Promise.resolve(
212-
this.findMatchingTests(globalConfig.testPathPattern),
213-
);
214211
} else {
215-
return Promise.resolve({tests: []});
212+
const validTestPaths = paths && paths.filter(fs.existsSync);
213+
214+
if (validTestPaths && validTestPaths.length) {
215+
return Promise.resolve({tests: toTests(this._context, validTestPaths)});
216+
} else if (globalConfig.testPathPattern != null) {
217+
return Promise.resolve(
218+
this.findMatchingTests(globalConfig.testPathPattern),
219+
);
220+
} else {
221+
return Promise.resolve({tests: []});
222+
}
216223
}
217224
}
218225
}

0 commit comments

Comments
 (0)