Skip to content

Commit 9b558c8

Browse files
committed
SearchSource now returns tests: Array<Test>.
1 parent c03821a commit 9b558c8

8 files changed

+92
-68
lines changed

packages/jest-cli/src/SearchSource.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type {Context} from 'types/Context';
1414
import type {Glob, Path} from 'types/Config';
1515
import type {ResolveModuleConfig} from 'types/Resolve';
16+
import type {Test} from 'types/TestRunner';
1617

1718
const micromatch = require('micromatch');
1819

@@ -27,8 +28,8 @@ const {
2728

2829
type SearchResult = {|
2930
noSCM?: boolean,
30-
paths: Array<Path>,
3131
stats?: {[key: string]: number},
32+
tests: Array<Test>,
3233
total?: number,
3334
|};
3435

@@ -62,7 +63,7 @@ const globsToMatcher = (globs: ?Array<Glob>) => {
6263
}
6364

6465
const matchers = globs.map(each => micromatch.matcher(each, {dot: true}));
65-
return (path: Path) => matchers.some(each => each(path));
66+
return path => matchers.some(each => each(path));
6667
};
6768

6869
const regexToMatcher = (testRegex: string) => {
@@ -71,9 +72,16 @@ const regexToMatcher = (testRegex: string) => {
7172
}
7273

7374
const regex = new RegExp(pathToRegex(testRegex));
74-
return (path: Path) => regex.test(path);
75+
return path => regex.test(path);
7576
};
7677

78+
const toTests = (context, tests) =>
79+
tests.map(path => ({
80+
context,
81+
duration: undefined,
82+
path,
83+
}));
84+
7785
class SearchSource {
7886
_context: Context;
7987
_options: ResolveModuleConfig;
@@ -112,12 +120,12 @@ class SearchSource {
112120
}
113121

114122
_filterTestPathsWithStats(
115-
allPaths: Array<Path>,
123+
allPaths: Array<Test>,
116124
testPathPattern?: StrOrRegExpPattern,
117125
): SearchResult {
118126
const data = {
119-
paths: [],
120127
stats: {},
128+
tests: [],
121129
total: allPaths.length,
122130
};
123131

@@ -128,11 +136,10 @@ class SearchSource {
128136
}
129137

130138
const testCasesKeys = Object.keys(testCases);
131-
132-
data.paths = allPaths.filter(path => {
139+
data.tests = allPaths.filter(test => {
133140
return testCasesKeys.reduce(
134141
(flag, key) => {
135-
if (testCases[key](path)) {
142+
if (testCases[key](test.path)) {
136143
data.stats[key] = ++data.stats[key] || 1;
137144
return flag && true;
138145
}
@@ -148,7 +155,7 @@ class SearchSource {
148155

149156
_getAllTestPaths(testPathPattern: StrOrRegExpPattern): SearchResult {
150157
return this._filterTestPathsWithStats(
151-
this._context.hasteFS.getAllFiles(),
158+
toTests(this._context, this._context.hasteFS.getAllFiles()),
152159
testPathPattern,
153160
);
154161
}
@@ -168,12 +175,15 @@ class SearchSource {
168175
this._context.hasteFS,
169176
);
170177
return {
171-
paths: dependencyResolver.resolveInverse(
172-
allPaths,
173-
this.isTestFilePath.bind(this),
174-
{
175-
skipNodeResolution: this._options.skipNodeResolution,
176-
},
178+
tests: toTests(
179+
this._context,
180+
dependencyResolver.resolveInverse(
181+
allPaths,
182+
this.isTestFilePath.bind(this),
183+
{
184+
skipNodeResolution: this._options.skipNodeResolution,
185+
},
186+
),
177187
),
178188
};
179189
}
@@ -183,7 +193,7 @@ class SearchSource {
183193
const resolvedPaths = paths.map(p => path.resolve(process.cwd(), p));
184194
return this.findRelatedTests(new Set(resolvedPaths));
185195
}
186-
return {paths: []};
196+
return {tests: []};
187197
}
188198

189199
findChangedTests(options: Options): Promise<SearchResult> {
@@ -193,7 +203,7 @@ class SearchSource {
193203
if (!repos.every(([gitRepo, hgRepo]) => gitRepo || hgRepo)) {
194204
return {
195205
noSCM: true,
196-
paths: [],
206+
tests: [],
197207
};
198208
}
199209
return Promise.all(
@@ -217,7 +227,7 @@ class SearchSource {
217227
} else if (pattern.testPathPattern != null) {
218228
return Promise.resolve(this.findMatchingTests(pattern.testPathPattern));
219229
} else {
220-
return Promise.resolve({paths: []});
230+
return Promise.resolve({tests: []});
221231
}
222232
}
223233
}

packages/jest-cli/src/TestPathPatternPrompt.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,16 @@ module.exports = class TestPathPatternPrompt {
6464
regex = new RegExp(pattern, 'i');
6565
} catch (e) {}
6666

67-
let paths = [];
67+
let tests = [];
6868
if (regex) {
6969
this._searchSources.forEach(({searchSource, context}) => {
70-
paths = paths.concat(
71-
searchSource.findMatchingTests(pattern).paths.map(path => ({
72-
context,
73-
path,
74-
})),
75-
);
70+
tests = tests.concat(searchSource.findMatchingTests(pattern).tests);
7671
});
7772
}
7873

7974
this._pipe.write(ansiEscapes.eraseLine);
8075
this._pipe.write(ansiEscapes.cursorLeft);
81-
this._printTypeahead(pattern, paths, 10);
76+
this._printTypeahead(pattern, tests, 10);
8277
}
8378

8479
_printTypeahead(pattern: string, allResults: Array<Test>, max: number) {

packages/jest-cli/src/TestSequencer.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class TestSequencer {
4545
// After a test run we store the time it took to run a test and on
4646
// subsequent runs we use that to run the slowest tests first, yielding the
4747
// fastest results.
48-
sort(testPaths: Array<string>): Tests {
49-
const context = this._context;
48+
sort(tests: Tests): Tests {
5049
const stats = {};
5150
const fileSize = filePath =>
5251
stats[filePath] || (stats[filePath] = fs.statSync(filePath).size);
@@ -56,14 +55,14 @@ class TestSequencer {
5655

5756
this._cache = {};
5857
try {
59-
if (context.config.cache) {
58+
if (this._context.config.cache) {
6059
this._cache = JSON.parse(
6160
fs.readFileSync(this._getTestPerformanceCachePath(), 'utf8'),
6261
);
6362
}
6463
} catch (e) {}
6564

66-
testPaths = testPaths.sort((pathA, pathB) => {
65+
tests = tests.sort(({path: pathA}, {path: pathB}) => {
6766
const failedA = failed(pathA);
6867
const failedB = failed(pathB);
6968
if (failedA !== failedB) {
@@ -83,11 +82,8 @@ class TestSequencer {
8382
return fileSize(pathA) < fileSize(pathB) ? 1 : -1;
8483
});
8584

86-
return testPaths.map(path => ({
87-
context,
88-
duration: this._cache[path] && this._cache[path][1],
89-
path,
90-
}));
85+
tests.forEach(test => test.duration = time(test.path));
86+
return tests;
9187
}
9288

9389
cacheResults(tests: Tests, results: AggregatedResult) {

packages/jest-cli/src/__tests__/SearchSource-test.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const testRegex = path.sep + '__testtests__' + path.sep;
1919
const testMatch = ['**/__testtests__/**/*'];
2020
const maxWorkers = 1;
2121

22+
const toPaths = tests => tests.map(({path}) => path);
23+
2224
let findMatchingTests;
2325
let normalizeConfig;
2426

@@ -106,7 +108,7 @@ describe('SearchSource', () => {
106108
testRegex: 'not-really-a-test',
107109
});
108110
return findMatchingTests(config).then(data => {
109-
const relPaths = data.paths
111+
const relPaths = toPaths(data.tests)
110112
.map(absPath => path.relative(rootDir, absPath))
111113
.sort();
112114
expect(relPaths).toEqual(
@@ -127,7 +129,7 @@ describe('SearchSource', () => {
127129
testRegex: '',
128130
});
129131
return findMatchingTests(config).then(data => {
130-
const relPaths = data.paths
132+
const relPaths = toPaths(data.tests)
131133
.map(absPath => path.relative(rootDir, absPath))
132134
.sort();
133135
expect(relPaths).toEqual(
@@ -148,7 +150,7 @@ describe('SearchSource', () => {
148150
testRegex: 'test\.jsx?',
149151
});
150152
return findMatchingTests(config).then(data => {
151-
const relPaths = data.paths.map(absPath =>
153+
const relPaths = toPaths(data.tests).map(absPath =>
152154
path.relative(rootDir, absPath));
153155
expect(relPaths.sort()).toEqual([
154156
path.normalize('__testtests__/test.js'),
@@ -166,7 +168,7 @@ describe('SearchSource', () => {
166168
testRegex: '',
167169
});
168170
return findMatchingTests(config).then(data => {
169-
const relPaths = data.paths.map(absPath =>
171+
const relPaths = toPaths(data.tests).map(absPath =>
170172
path.relative(rootDir, absPath));
171173
expect(relPaths.sort()).toEqual([
172174
path.normalize('__testtests__/test.js'),
@@ -183,7 +185,7 @@ describe('SearchSource', () => {
183185
testRegex,
184186
});
185187
return findMatchingTests(config).then(data => {
186-
const relPaths = data.paths.map(absPath =>
188+
const relPaths = toPaths(data.tests).map(absPath =>
187189
path.relative(rootDir, absPath));
188190
expect(relPaths.sort()).toEqual([
189191
path.normalize('__testtests__/test.js'),
@@ -200,7 +202,7 @@ describe('SearchSource', () => {
200202
testRegex: '',
201203
});
202204
return findMatchingTests(config).then(data => {
203-
const relPaths = data.paths.map(absPath =>
205+
const relPaths = toPaths(data.tests).map(absPath =>
204206
path.relative(rootDir, absPath));
205207
expect(relPaths.sort()).toEqual([
206208
path.normalize('__testtests__/test.js'),
@@ -217,7 +219,7 @@ describe('SearchSource', () => {
217219
testMatch,
218220
});
219221
return findMatchingTests(config).then(data => {
220-
const relPaths = data.paths.map(absPath =>
222+
const relPaths = toPaths(data.tests).map(absPath =>
221223
path.relative(rootDir, absPath));
222224
expect(relPaths).toEqual([path.normalize('__testtests__/test.jsx')]);
223225
});
@@ -231,7 +233,7 @@ describe('SearchSource', () => {
231233
testMatch,
232234
});
233235
return findMatchingTests(config).then(data => {
234-
const relPaths = data.paths.map(absPath =>
236+
const relPaths = toPaths(data.tests).map(absPath =>
235237
path.relative(rootDir, absPath));
236238
expect(relPaths).toEqual([path.normalize('__testtests__/test.foobar')]);
237239
});
@@ -245,7 +247,7 @@ describe('SearchSource', () => {
245247
testMatch,
246248
});
247249
return findMatchingTests(config).then(data => {
248-
const relPaths = data.paths.map(absPath =>
250+
const relPaths = toPaths(data.tests).map(absPath =>
249251
path.relative(rootDir, absPath));
250252
expect(relPaths.sort()).toEqual([
251253
path.normalize('__testtests__/test.js'),
@@ -262,7 +264,7 @@ describe('SearchSource', () => {
262264
testRegex,
263265
});
264266
return findMatchingTests(config).then(data => {
265-
const relPaths = data.paths.map(absPath =>
267+
const relPaths = toPaths(data.tests).map(absPath =>
266268
path.relative(rootDir, absPath));
267269
expect(relPaths.sort()).toEqual([
268270
path.normalize('__testtests__/test.js'),
@@ -279,7 +281,7 @@ describe('SearchSource', () => {
279281
testRegex: '',
280282
});
281283
return findMatchingTests(config).then(data => {
282-
const relPaths = data.paths.map(absPath =>
284+
const relPaths = toPaths(data.tests).map(absPath =>
283285
path.relative(rootDir, absPath));
284286
expect(relPaths.sort()).toEqual([
285287
path.normalize('__testtests__/test.js'),
@@ -315,15 +317,15 @@ describe('SearchSource', () => {
315317

316318
it('makes sure a file is related to itself', () => {
317319
const data = searchSource.findRelatedTests(new Set([rootPath]));
318-
expect(data.paths).toEqual([rootPath]);
320+
expect(toPaths(data.tests)).toEqual([rootPath]);
319321
});
320322

321323
it('finds tests that depend directly on the path', () => {
322324
const filePath = path.join(rootDir, 'RegularModule.js');
323325
const loggingDep = path.join(rootDir, 'logging.js');
324326
const parentDep = path.join(rootDir, 'ModuleWithSideEffects.js');
325327
const data = searchSource.findRelatedTests(new Set([filePath]));
326-
expect(data.paths.sort()).toEqual([
328+
expect(toPaths(data.tests).sort()).toEqual([
327329
parentDep,
328330
filePath,
329331
loggingDep,
@@ -349,25 +351,25 @@ describe('SearchSource', () => {
349351
it('returns empty search result for empty input', () => {
350352
const input = [];
351353
const data = searchSource.findRelatedTestsFromPattern(input);
352-
expect(data.paths).toEqual([]);
354+
expect(data.tests).toEqual([]);
353355
});
354356

355357
it('returns empty search result for invalid input', () => {
356358
const input = ['non-existend.js'];
357359
const data = searchSource.findRelatedTestsFromPattern(input);
358-
expect(data.paths).toEqual([]);
360+
expect(data.tests).toEqual([]);
359361
});
360362

361363
it('returns empty search result if no related tests were found', () => {
362364
const input = ['no tests.js'];
363365
const data = searchSource.findRelatedTestsFromPattern(input);
364-
expect(data.paths).toEqual([]);
366+
expect(data.tests).toEqual([]);
365367
});
366368

367369
it('finds tests for a single file', () => {
368370
const input = ['packages/jest-cli/src/__tests__/test_root/module.jsx'];
369371
const data = searchSource.findRelatedTestsFromPattern(input);
370-
expect(data.paths.sort()).toEqual([
372+
expect(toPaths(data.tests).sort()).toEqual([
371373
path.join(rootDir, '__testtests__', 'test.js'),
372374
path.join(rootDir, '__testtests__', 'test.jsx'),
373375
]);
@@ -379,7 +381,7 @@ describe('SearchSource', () => {
379381
'packages/jest-cli/src/__tests__/test_root/module.foobar',
380382
];
381383
const data = searchSource.findRelatedTestsFromPattern(input);
382-
expect(data.paths.sort()).toEqual([
384+
expect(toPaths(data.tests).sort()).toEqual([
383385
path.join(rootDir, '__testtests__', 'test.foobar'),
384386
path.join(rootDir, '__testtests__', 'test.js'),
385387
path.join(rootDir, '__testtests__', 'test.jsx'),

0 commit comments

Comments
 (0)