/
SearchSource.test.ts
594 lines (547 loc) · 17.2 KB
/
SearchSource.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import * as path from 'path';
import Runtime = require('jest-runtime');
import {normalize} from 'jest-config';
import {Test} from 'jest-runner';
import type {Config} from '@jest/types';
import SearchSource, {SearchResult} from '../SearchSource';
jest.setTimeout(15000);
jest.mock('fs', () => {
const realFs = jest.requireActual('fs');
return {
...realFs,
statSync: path => {
if (path === '/foo/bar/prefix') {
return {isDirectory: () => true};
}
return realFs.statSync(path);
},
};
});
const rootDir = path.resolve(__dirname, 'test_root');
const testRegex = path.sep + '__testtests__' + path.sep;
const testMatch = ['**/__testtests__/**/*'];
const maxWorkers = 1;
const toPaths = (tests: Array<Test>) => tests.map(({path}) => path);
let findMatchingTests: (config: Config.ProjectConfig) => Promise<SearchResult>;
describe('SearchSource', () => {
const name = 'SearchSource';
let searchSource: SearchSource;
describe('isTestFilePath', () => {
let config;
beforeEach(() => {
config = normalize(
{
name,
rootDir: '.',
roots: [],
},
{} as Config.Argv,
).options;
return Runtime.createContext(config, {maxWorkers, watchman: false}).then(
context => {
searchSource = new SearchSource(context);
},
);
});
// micromatch doesn't support '..' through the globstar ('**') to avoid
// infinite recursion.
it('supports ../ paths and unix separators via testRegex', () => {
if (process.platform !== 'win32') {
config = normalize(
{
name,
rootDir: '.',
roots: [],
testMatch: undefined,
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$',
},
{} as Config.Argv,
).options;
return Runtime.createContext(config, {
maxWorkers,
watchman: false,
}).then(context => {
searchSource = new SearchSource(context);
const path = '/path/to/__tests__/foo/bar/baz/../../../test.js';
expect(searchSource.isTestFilePath(path)).toEqual(true);
});
} else {
return undefined;
}
});
it('supports unix separators', () => {
if (process.platform !== 'win32') {
const path = '/path/to/__tests__/test.js';
expect(searchSource.isTestFilePath(path)).toEqual(true);
}
});
it('supports win32 separators', () => {
if (process.platform === 'win32') {
const path = '\\path\\to\\__tests__\\test.js';
expect(searchSource.isTestFilePath(path)).toEqual(true);
}
});
});
describe('testPathsMatching', () => {
beforeEach(() => {
findMatchingTests = (config: Config.ProjectConfig) =>
Runtime.createContext(config, {
maxWorkers,
watchman: false,
}).then(context => new SearchSource(context).findMatchingTests());
});
it('finds tests matching a pattern via testRegex', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx', 'txt'],
name,
rootDir,
testMatch: undefined,
testRegex: 'not-really-a-test',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests)
.map(absPath => path.relative(rootDir, absPath))
.sort();
expect(relPaths).toEqual(
[
path.normalize('.hiddenFolder/not-really-a-test.txt'),
path.normalize('__testtests__/not-really-a-test.txt'),
].sort(),
);
});
});
it('finds tests matching a pattern via testMatch', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx', 'txt'],
name,
rootDir,
testMatch: ['**/not-really-a-test.txt', '!**/do-not-match-me.txt'],
testRegex: '',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests)
.map(absPath => path.relative(rootDir, absPath))
.sort();
expect(relPaths).toEqual(
[
path.normalize('.hiddenFolder/not-really-a-test.txt'),
path.normalize('__testtests__/not-really-a-test.txt'),
].sort(),
);
});
});
it('finds tests matching a JS regex pattern', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx'],
name,
rootDir,
testMatch: undefined,
testRegex: 'test.jsx?',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests matching a JS glob pattern', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx'],
name,
rootDir,
testMatch: ['**/test.js?(x)'],
testRegex: '',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests with default file extensions using testRegex', () => {
const {options: config} = normalize(
{
name,
rootDir,
testMatch: undefined,
testRegex,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests with default file extensions using testMatch', () => {
const {options: config} = normalize(
{
name,
rootDir,
testMatch,
testRegex: '',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests with parentheses in their rootDir when using testMatch', () => {
const {options: config} = normalize(
{
name,
rootDir: path.resolve(__dirname, 'test_root_with_(parentheses)'),
testMatch: ['<rootDir>**/__testtests__/**/*'],
testRegex: undefined,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
expect.stringContaining(path.normalize('__testtests__/test.js')),
]);
});
});
it('finds tests with similar but custom file extensions', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx'],
name,
rootDir,
testMatch,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests with totally custom foobar file extensions', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'foobar'],
name,
rootDir,
testMatch,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.foobar'),
path.normalize('__testtests__/test.js'),
]);
});
});
it('finds tests with many kinds of file extensions', () => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx'],
name,
rootDir,
testMatch,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests using a regex only', () => {
const {options: config} = normalize(
{
name,
rootDir,
testMatch: undefined,
testRegex,
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
it('finds tests using a glob only', () => {
const {options: config} = normalize(
{
name,
rootDir,
testMatch,
testRegex: '',
},
{} as Config.Argv,
);
return findMatchingTests(config).then(data => {
const relPaths = toPaths(data.tests).map(absPath =>
path.relative(rootDir, absPath),
);
expect(relPaths.sort()).toEqual([
path.normalize('__testtests__/test.js'),
path.normalize('__testtests__/test.jsx'),
]);
});
});
});
describe('findRelatedTests', () => {
const rootDir = path.join(
__dirname,
'..',
'..',
'..',
'jest-runtime',
'src',
'__tests__',
'test_root',
);
const rootPath = path.join(rootDir, 'root.js');
beforeEach(done => {
const {options: config} = normalize(
{
haste: {
hasteImplModulePath: path.join(
__dirname,
'..',
'..',
'..',
'jest-haste-map',
'src',
'__tests__',
'haste_impl.js',
),
providesModuleNodeModules: [],
},
name: 'SearchSource-findRelatedTests-tests',
rootDir,
},
{} as Config.Argv,
);
Runtime.createContext(config, {maxWorkers, watchman: false}).then(
context => {
searchSource = new SearchSource(context);
done();
},
);
});
it('makes sure a file is related to itself', () => {
const data = searchSource.findRelatedTests(new Set([rootPath]), false);
expect(toPaths(data.tests)).toEqual([rootPath]);
});
it('finds tests that depend directly on the path', () => {
const filePath = path.join(rootDir, 'RegularModule.js');
const file2Path = path.join(rootDir, 'RequireRegularModule.js');
const loggingDep = path.join(rootDir, 'logging.js');
const parentDep = path.join(rootDir, 'ModuleWithSideEffects.js');
const data = searchSource.findRelatedTests(new Set([filePath]), false);
expect(toPaths(data.tests).sort()).toEqual([
parentDep,
filePath,
file2Path,
loggingDep,
rootPath,
]);
});
it('excludes untested files from coverage', () => {
const unrelatedFile = path.join(rootDir, 'JSONFile.json');
const regular = path.join(rootDir, 'RegularModule.js');
const requireRegular = path.join(rootDir, 'RequireRegularMode.js');
const data = searchSource.findRelatedTests(
new Set([regular, requireRegular, unrelatedFile]),
true,
);
expect(Array.from(data.collectCoverageFrom || [])).toEqual([
'RegularModule.js',
]);
});
});
describe('findRelatedTestsFromPattern', () => {
beforeEach(done => {
const {options: config} = normalize(
{
moduleFileExtensions: ['js', 'jsx', 'foobar'],
name,
rootDir,
testMatch,
},
{} as Config.Argv,
);
Runtime.createContext(config, {maxWorkers, watchman: false}).then(
context => {
searchSource = new SearchSource(context);
done();
},
);
});
it('returns empty search result for empty input', () => {
const input: Array<Config.Path> = [];
const data = searchSource.findRelatedTestsFromPattern(input, false);
expect(data.tests).toEqual([]);
});
it('returns empty search result for invalid input', () => {
const input = ['non-existend.js'];
const data = searchSource.findRelatedTestsFromPattern(input, false);
expect(data.tests).toEqual([]);
});
it('returns empty search result if no related tests were found', () => {
const input = ['no_tests.js'];
const data = searchSource.findRelatedTestsFromPattern(input, false);
expect(data.tests).toEqual([]);
});
it('finds tests for a single file', () => {
const input = ['packages/jest-core/src/__tests__/test_root/module.jsx'];
const data = searchSource.findRelatedTestsFromPattern(input, false);
expect(toPaths(data.tests).sort()).toEqual([
path.join(rootDir, '__testtests__', 'test.js'),
path.join(rootDir, '__testtests__', 'test.jsx'),
]);
});
it('finds tests for multiple files', () => {
const input = [
'packages/jest-core/src/__tests__/test_root/module.jsx',
'packages/jest-core/src/__tests__/test_root/module.foobar',
];
const data = searchSource.findRelatedTestsFromPattern(input, false);
expect(toPaths(data.tests).sort()).toEqual([
path.join(rootDir, '__testtests__', 'test.foobar'),
path.join(rootDir, '__testtests__', 'test.js'),
path.join(rootDir, '__testtests__', 'test.jsx'),
]);
});
it('does not mistake roots folders with prefix names', async () => {
if (process.platform !== 'win32') {
const config = normalize(
{
name,
rootDir: '.',
roots: ['/foo/bar/prefix'],
},
{} as Config.Argv,
).options;
searchSource = new SearchSource(
await Runtime.createContext(config, {maxWorkers, watchman: false}),
);
const input = ['/foo/bar/prefix-suffix/__tests__/my-test.test.js'];
const data = searchSource.findTestsByPaths(input);
expect(data.tests).toEqual([]);
}
});
});
describe('findRelatedSourcesFromTestsInChangedFiles', () => {
const rootDir = path.resolve(
__dirname,
'../../../jest-runtime/src/__tests__/test_root',
);
beforeEach(async () => {
const {options: config} = normalize(
{
haste: {
hasteImplModulePath: path.resolve(
__dirname,
'../../../jest-haste-map/src/__tests__/haste_impl.js',
),
providesModuleNodeModules: [],
},
name: 'SearchSource-findRelatedSourcesFromTestsInChangedFiles-tests',
rootDir,
},
{} as Config.Argv,
);
const context = await Runtime.createContext(config, {
maxWorkers,
watchman: false,
});
searchSource = new SearchSource(context);
});
it('return empty set if no SCM', () => {
const requireRegularModule = path.join(
rootDir,
'RequireRegularModule.js',
);
const sources = searchSource.findRelatedSourcesFromTestsInChangedFiles({
changedFiles: new Set([requireRegularModule]),
repos: {
git: new Set(),
hg: new Set(),
},
});
expect(sources).toEqual([]);
});
it('return sources required by tests', () => {
const regularModule = path.join(rootDir, 'RegularModule.js');
const requireRegularModule = path.join(
rootDir,
'RequireRegularModule.js',
);
const sources = searchSource.findRelatedSourcesFromTestsInChangedFiles({
changedFiles: new Set([requireRegularModule]),
repos: {
git: new Set('/path/to/git'),
hg: new Set(),
},
});
expect(sources).toEqual([regularModule]);
});
});
});