mo3ta-coverage 1.0.18 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENT.md +144 -0
- package/README.md +54 -7
- package/RECOMMENDED_USAGE.md +20 -5
- package/VIDEO_PLAN.md +316 -0
- package/docs/poster.png +0 -0
- package/docs/poster.svg +131 -0
- package/index.js +32 -1
- package/package.json +1 -1
- package/src/cli.js +150 -89
- package/src/cli.test.js +127 -15
- package/src/fileUtils.js +241 -9
- package/src/fileUtils.test.js +98 -1
package/src/cli.test.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import os from 'os';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import { execSync } from 'child_process';
|
|
4
5
|
import {
|
|
5
6
|
classifyChangedLineCoverage,
|
|
6
7
|
formatMissedLinesSummary,
|
|
8
|
+
getChangedFiles,
|
|
9
|
+
getExplicitFiles,
|
|
10
|
+
normalizeExplicitFiles,
|
|
7
11
|
parseLcov,
|
|
8
|
-
|
|
12
|
+
readExplicitFilesFromFile,
|
|
9
13
|
summarizeCoverageResult
|
|
10
14
|
} from './cli.js';
|
|
11
15
|
|
|
@@ -92,20 +96,6 @@ describe('summarizeCoverageResult', () => {
|
|
|
92
96
|
});
|
|
93
97
|
});
|
|
94
98
|
|
|
95
|
-
describe('selectTestFilesForMode', () => {
|
|
96
|
-
const changedTests = ['src/changed.test.js'];
|
|
97
|
-
const mappedTests = ['src/changed.test.js', 'src/unchanged.test.js'];
|
|
98
|
-
|
|
99
|
-
it('limits smart mode to test files changed in the diff', () => {
|
|
100
|
-
expect(selectTestFilesForMode('smart', changedTests, mappedTests)).toEqual(changedTests);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('keeps mapped tests for non-smart modes', () => {
|
|
104
|
-
expect(selectTestFilesForMode('fast', changedTests, mappedTests)).toEqual(mappedTests);
|
|
105
|
-
expect(selectTestFilesForMode('full', changedTests, mappedTests)).toEqual(mappedTests);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
99
|
describe('formatMissedLinesSummary', () => {
|
|
110
100
|
it('formats uncovered files and lines for the final summary', () => {
|
|
111
101
|
const result = formatMissedLinesSummary([
|
|
@@ -126,3 +116,125 @@ describe('formatMissedLinesSummary', () => {
|
|
|
126
116
|
expect(formatMissedLinesSummary([])).toEqual([]);
|
|
127
117
|
});
|
|
128
118
|
});
|
|
119
|
+
|
|
120
|
+
describe('normalizeExplicitFiles', () => {
|
|
121
|
+
it('accepts a comma-separated string', () => {
|
|
122
|
+
expect(normalizeExplicitFiles('src/a.js,src/b.ts')).toEqual([
|
|
123
|
+
'src/a.js',
|
|
124
|
+
'src/b.ts'
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('accepts an array of paths', () => {
|
|
129
|
+
expect(normalizeExplicitFiles(['src/a.js', 'src/b.ts'])).toEqual([
|
|
130
|
+
'src/a.js',
|
|
131
|
+
'src/b.ts'
|
|
132
|
+
]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('trims, drops empty entries, and removes duplicates', () => {
|
|
136
|
+
expect(normalizeExplicitFiles([' src/a.js ', '', 'src/a.js', 'src/b.js'])).toEqual([
|
|
137
|
+
'src/a.js',
|
|
138
|
+
'src/b.js'
|
|
139
|
+
]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('converts absolute paths under the root to relative paths', () => {
|
|
143
|
+
const root = path.resolve('/tmp/project');
|
|
144
|
+
|
|
145
|
+
expect(normalizeExplicitFiles([path.join(root, 'src/a.js')], root)).toEqual([
|
|
146
|
+
'src/a.js'
|
|
147
|
+
]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('drops dependency and build artifacts', () => {
|
|
151
|
+
expect(normalizeExplicitFiles([
|
|
152
|
+
'src/a.js',
|
|
153
|
+
'node_modules/pkg/index.js',
|
|
154
|
+
'coverage/lcov-report/x.js',
|
|
155
|
+
'dist/bundle.js',
|
|
156
|
+
'build/out.js'
|
|
157
|
+
])).toEqual(['src/a.js']);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('readExplicitFilesFromFile', () => {
|
|
162
|
+
it('reads a newline-separated list from disk', () => {
|
|
163
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-files-'));
|
|
164
|
+
const listPath = path.join(tempDir, 'files.txt');
|
|
165
|
+
fs.writeFileSync(listPath, 'src/a.js\nsrc/b.ts\n\n');
|
|
166
|
+
|
|
167
|
+
expect(readExplicitFilesFromFile(listPath, tempDir)).toEqual([
|
|
168
|
+
'src/a.js',
|
|
169
|
+
'src/b.ts'
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('getExplicitFiles', () => {
|
|
177
|
+
it('returns an empty list when no explicit options are provided', () => {
|
|
178
|
+
expect(getExplicitFiles({})).toEqual([]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('combines --files and --filesFrom, removing duplicates', () => {
|
|
182
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-files-'));
|
|
183
|
+
const listPath = path.join(tempDir, 'files.txt');
|
|
184
|
+
fs.writeFileSync(listPath, 'src/b.js\nsrc/a.js\n');
|
|
185
|
+
|
|
186
|
+
expect(getExplicitFiles({
|
|
187
|
+
files: ['src/a.js'],
|
|
188
|
+
filesFrom: listPath
|
|
189
|
+
})).toEqual(['src/a.js', 'src/b.js']);
|
|
190
|
+
|
|
191
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('getChangedFiles', () => {
|
|
196
|
+
let tempDir;
|
|
197
|
+
let originalCwd;
|
|
198
|
+
|
|
199
|
+
beforeEach(() => {
|
|
200
|
+
originalCwd = process.cwd();
|
|
201
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-git-'));
|
|
202
|
+
|
|
203
|
+
execSync('git init -b main -q', { cwd: tempDir });
|
|
204
|
+
execSync('git config user.name "Base Author"', { cwd: tempDir });
|
|
205
|
+
execSync('git config user.email "base@example.com"', { cwd: tempDir });
|
|
206
|
+
|
|
207
|
+
fs.writeFileSync(path.join(tempDir, 'base.js'), 'export const base = 1;\n');
|
|
208
|
+
execSync('git add base.js && git commit -q -m "base"', { cwd: tempDir });
|
|
209
|
+
execSync('git checkout -q -b feature', { cwd: tempDir });
|
|
210
|
+
|
|
211
|
+
process.chdir(tempDir);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
afterEach(() => {
|
|
215
|
+
process.chdir(originalCwd);
|
|
216
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('includes changed files regardless of the commit author', () => {
|
|
220
|
+
fs.writeFileSync(path.join(tempDir, 'by-other.js'), 'export const other = 1;\n');
|
|
221
|
+
execSync('git add by-other.js && git -c user.name="Other Dev" -c user.email="other@example.com" commit -q -m "other author"', { cwd: tempDir });
|
|
222
|
+
|
|
223
|
+
fs.writeFileSync(path.join(tempDir, 'by-me.js'), 'export const me = 1;\n');
|
|
224
|
+
execSync('git add by-me.js && git commit -q -m "current author"', { cwd: tempDir });
|
|
225
|
+
|
|
226
|
+
const result = getChangedFiles('main');
|
|
227
|
+
|
|
228
|
+
expect(result).toEqual(['by-me.js', 'by-other.js']);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('includes uncommitted and staged changes', () => {
|
|
232
|
+
fs.writeFileSync(path.join(tempDir, 'base.js'), 'export const base = 2;\n');
|
|
233
|
+
fs.writeFileSync(path.join(tempDir, 'staged.js'), 'export const b = 1;\n');
|
|
234
|
+
execSync('git add staged.js', { cwd: tempDir });
|
|
235
|
+
|
|
236
|
+
const result = getChangedFiles('main');
|
|
237
|
+
|
|
238
|
+
expect(result).toEqual(['base.js', 'staged.js']);
|
|
239
|
+
});
|
|
240
|
+
});
|
package/src/fileUtils.js
CHANGED
|
@@ -57,16 +57,16 @@ export function separateSourceAndTestFiles(files) {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @param {string} sourceFile - Path to source file
|
|
63
|
-
* @returns {string[]} -
|
|
60
|
+
* Builds candidate test paths for a source file using common naming conventions.
|
|
61
|
+
* Paths are relative and have not been checked against the filesystem.
|
|
62
|
+
* @param {string} sourceFile - Path to source file (relative)
|
|
63
|
+
* @returns {string[]} - Candidate test file paths
|
|
64
64
|
*/
|
|
65
|
-
|
|
65
|
+
function getTestFileCandidates(sourceFile) {
|
|
66
66
|
const testFiles = [];
|
|
67
67
|
const parsed = path.parse(sourceFile);
|
|
68
68
|
const extensions = ['.js', '.ts', '.jsx', '.tsx'];
|
|
69
|
-
|
|
69
|
+
|
|
70
70
|
// Pattern 1: Same directory with .test or .spec suffix
|
|
71
71
|
// e.g., src/utils/helper.js → src/utils/helper.test.js
|
|
72
72
|
extensions.forEach(ext => {
|
|
@@ -89,15 +89,36 @@ export function findTestFilesForSource(sourceFile) {
|
|
|
89
89
|
const baseDir = srcMatch[1];
|
|
90
90
|
const relativePath = srcMatch[2];
|
|
91
91
|
const relParsed = path.parse(relativePath);
|
|
92
|
-
|
|
92
|
+
|
|
93
93
|
extensions.forEach(ext => {
|
|
94
94
|
testFiles.push(path.join(baseDir, 'tests', relParsed.dir, `${relParsed.name}.test${ext}`));
|
|
95
95
|
testFiles.push(path.join(baseDir, 'tests', relParsed.dir, `${relParsed.name}.spec${ext}`));
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
return testFiles;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Finds potential test files for a given source file
|
|
104
|
+
* Checks multiple naming conventions and locations
|
|
105
|
+
* @param {string} sourceFile - Path to source file
|
|
106
|
+
* @returns {string[]} - Array of potential test file paths
|
|
107
|
+
*/
|
|
108
|
+
export function findTestFilesForSource(sourceFile) {
|
|
109
|
+
return getTestFileCandidates(sourceFile).filter(testFile => fs.existsSync(testFile));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Root-aware variant of findTestFilesForSource used by the import graph walker.
|
|
114
|
+
* Existence is checked relative to `root`, while returned paths stay relative.
|
|
115
|
+
* @param {string} sourceFile - Path to source file relative to root
|
|
116
|
+
* @param {string} root - Directory the paths are relative to
|
|
117
|
+
* @returns {string[]} - Existing test file paths relative to root
|
|
118
|
+
*/
|
|
119
|
+
function findTestFilesForSourceInRoot(sourceFile, root) {
|
|
120
|
+
return getTestFileCandidates(sourceFile)
|
|
121
|
+
.filter(testFile => fs.existsSync(path.resolve(root, testFile)));
|
|
101
122
|
}
|
|
102
123
|
|
|
103
124
|
let cachedAllTestFiles = null;
|
|
@@ -235,3 +256,214 @@ export function findTestFilesByImport(sourceFile, root = process.cwd()) {
|
|
|
235
256
|
|
|
236
257
|
return matching;
|
|
237
258
|
}
|
|
259
|
+
|
|
260
|
+
let cachedAllJsFiles = null;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Collects every JS/TS file under the given root, skipping dependency/build
|
|
264
|
+
* directories. Results are cached per root for the lifetime of the CLI run.
|
|
265
|
+
* @param {string} [root] - Directory to scan (defaults to cwd)
|
|
266
|
+
* @returns {string[]} - File paths relative to root
|
|
267
|
+
*/
|
|
268
|
+
export function findAllJsFiles(root = process.cwd()) {
|
|
269
|
+
if (cachedAllJsFiles && cachedAllJsFiles.root === root) {
|
|
270
|
+
return cachedAllJsFiles.files;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const results = [];
|
|
274
|
+
const walk = dir => {
|
|
275
|
+
let entries;
|
|
276
|
+
try {
|
|
277
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
278
|
+
} catch {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
entries.forEach(entry => {
|
|
282
|
+
const fullPath = path.join(dir, entry.name);
|
|
283
|
+
if (entry.isDirectory()) {
|
|
284
|
+
if (!SKIP_DIRS.has(entry.name) && !entry.name.startsWith('.')) {
|
|
285
|
+
walk(fullPath);
|
|
286
|
+
}
|
|
287
|
+
} else if (JS_EXTENSIONS.some(ext => entry.name.endsWith(ext))) {
|
|
288
|
+
results.push(path.relative(root, fullPath));
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
walk(root);
|
|
294
|
+
cachedAllJsFiles = { root, files: results };
|
|
295
|
+
return results;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function isTestFile(filePath) {
|
|
299
|
+
return TEST_FILE_RE.test(filePath);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function stripJsExtension(filePath) {
|
|
303
|
+
return filePath.replace(/\.(js|ts|jsx|tsx|mjs|cjs)$/, '');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Builds a lookup of repo-relative paths without extensions. Multiple files can
|
|
308
|
+
* share a path stem when they use different extensions (e.g. helper.js/.ts).
|
|
309
|
+
*/
|
|
310
|
+
function buildNoExtIndex(files) {
|
|
311
|
+
const index = new Map();
|
|
312
|
+
files.forEach(file => {
|
|
313
|
+
const key = stripJsExtension(file);
|
|
314
|
+
if (!index.has(key)) index.set(key, []);
|
|
315
|
+
index.get(key).push(file);
|
|
316
|
+
});
|
|
317
|
+
return index;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Resolves a non-relative (alias or package-style) specifier to files under
|
|
322
|
+
* root. Heuristics, in order:
|
|
323
|
+
* 1. exact repo-relative stem match ('src/utils/helper'),
|
|
324
|
+
* 2. path-suffix match ('utils/helper' → 'src/utils/helper').
|
|
325
|
+
* '@/x' and '~/x' aliases are normalized before matching.
|
|
326
|
+
*/
|
|
327
|
+
function resolveNonRelativeSpecifier(specifier, root, noExtIndex) {
|
|
328
|
+
let stem = stripJsExtension(specifier);
|
|
329
|
+
if (stem.startsWith('@/') || stem.startsWith('~/')) {
|
|
330
|
+
stem = stem.slice(2);
|
|
331
|
+
}
|
|
332
|
+
if (!stem) return [];
|
|
333
|
+
|
|
334
|
+
const exact = noExtIndex.get(stem);
|
|
335
|
+
if (exact) {
|
|
336
|
+
return exact.map(file => path.resolve(root, file));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const suffix = `/${stem}`;
|
|
340
|
+
const matches = [];
|
|
341
|
+
for (const [key, files] of noExtIndex) {
|
|
342
|
+
if (key.endsWith(suffix)) {
|
|
343
|
+
files.forEach(file => matches.push(path.resolve(root, file)));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return matches;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Resolves an import specifier found in `fromFile` to absolute file paths.
|
|
351
|
+
* Relative specifiers resolve against the importing file's directory; alias
|
|
352
|
+
* and package-style specifiers resolve against the repo-relative file index.
|
|
353
|
+
*/
|
|
354
|
+
function resolveImportTargets(specifier, fromFile, root, noExtIndex) {
|
|
355
|
+
if (specifier.startsWith('.')) {
|
|
356
|
+
const fromDir = path.dirname(path.resolve(root, fromFile));
|
|
357
|
+
return resolveSpecifierCandidates(specifier, fromDir);
|
|
358
|
+
}
|
|
359
|
+
return resolveNonRelativeSpecifier(specifier, root, noExtIndex);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
let cachedReverseImportIndex = null;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Builds (and caches per root) a reverse import index:
|
|
366
|
+
* absolute module path → Set of absolute paths that import it.
|
|
367
|
+
* All JS/TS files participate, so traversal can pass through intermediate
|
|
368
|
+
* modules such as barrel files.
|
|
369
|
+
* @param {string} [root] - Directory to scan (defaults to cwd)
|
|
370
|
+
* @returns {Map<string, Set<string>>}
|
|
371
|
+
*/
|
|
372
|
+
export function buildReverseImportIndex(root = process.cwd()) {
|
|
373
|
+
if (cachedReverseImportIndex && cachedReverseImportIndex.root === root) {
|
|
374
|
+
return cachedReverseImportIndex.index;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const files = findAllJsFiles(root);
|
|
378
|
+
const noExtIndex = buildNoExtIndex(files);
|
|
379
|
+
const index = new Map();
|
|
380
|
+
|
|
381
|
+
files.forEach(relFile => {
|
|
382
|
+
let content;
|
|
383
|
+
try {
|
|
384
|
+
content = fs.readFileSync(path.resolve(root, relFile), 'utf8');
|
|
385
|
+
} catch {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const importerPath = path.resolve(root, relFile);
|
|
390
|
+
extractImportSpecifiers(content).forEach(specifier => {
|
|
391
|
+
resolveImportTargets(specifier, relFile, root, noExtIndex).forEach(target => {
|
|
392
|
+
if (!index.has(target)) index.set(target, new Set());
|
|
393
|
+
index.get(target).add(importerPath);
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
cachedReverseImportIndex = { root, index };
|
|
399
|
+
return index;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Finds test files related to one source file. Combines:
|
|
404
|
+
* 1. naming-convention matches, and
|
|
405
|
+
* 2. a reverse-dependency walk through the import graph, limited to
|
|
406
|
+
* `maxDepth` edges from the source file.
|
|
407
|
+
*
|
|
408
|
+
* Depth 1 = test imports the source directly.
|
|
409
|
+
* Depth 2 = test imports a module that imports the source (e.g. a barrel).
|
|
410
|
+
*
|
|
411
|
+
* @param {string} sourceFile - Path to source file (relative to root)
|
|
412
|
+
* @param {{root?: string, maxDepth?: number}} [options]
|
|
413
|
+
* @returns {string[]} - Related test file paths relative to root, sorted
|
|
414
|
+
*/
|
|
415
|
+
export function findRelatedTestFilesBySource(sourceFile, {
|
|
416
|
+
root = process.cwd(),
|
|
417
|
+
maxDepth = 2
|
|
418
|
+
} = {}) {
|
|
419
|
+
const related = new Set(findTestFilesForSourceInRoot(sourceFile, root));
|
|
420
|
+
const depthLimit = Math.max(0, Number.parseInt(maxDepth, 10) || 0);
|
|
421
|
+
if (depthLimit === 0) {
|
|
422
|
+
return Array.from(related).sort();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const reverseIndex = buildReverseImportIndex(root);
|
|
426
|
+
const start = path.resolve(root, sourceFile);
|
|
427
|
+
const visited = new Set([start]);
|
|
428
|
+
let frontier = new Set([start]);
|
|
429
|
+
|
|
430
|
+
for (let depth = 1; depth <= depthLimit && frontier.size > 0; depth++) {
|
|
431
|
+
const next = new Set();
|
|
432
|
+
|
|
433
|
+
frontier.forEach(node => {
|
|
434
|
+
const importers = reverseIndex.get(node);
|
|
435
|
+
if (!importers) return;
|
|
436
|
+
|
|
437
|
+
importers.forEach(importer => {
|
|
438
|
+
if (visited.has(importer)) return;
|
|
439
|
+
visited.add(importer);
|
|
440
|
+
|
|
441
|
+
const relImporter = path.relative(root, importer);
|
|
442
|
+
if (isTestFile(relImporter)) {
|
|
443
|
+
related.add(relImporter);
|
|
444
|
+
} else {
|
|
445
|
+
// Non-test importers are only useful as traversal hops.
|
|
446
|
+
next.add(importer);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
frontier = next;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return Array.from(related).sort();
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Finds test files related to any of the given source files.
|
|
459
|
+
* @param {string[]} sourceFiles - Source paths relative to root
|
|
460
|
+
* @param {{root?: string, maxDepth?: number}} [options]
|
|
461
|
+
* @returns {string[]} - Sorted, de-duplicated related test paths
|
|
462
|
+
*/
|
|
463
|
+
export function findRelatedTestFiles(sourceFiles, options = {}) {
|
|
464
|
+
const related = new Set();
|
|
465
|
+
sourceFiles.forEach(sourceFile => {
|
|
466
|
+
findRelatedTestFilesBySource(sourceFile, options).forEach(testFile => related.add(testFile));
|
|
467
|
+
});
|
|
468
|
+
return Array.from(related).sort();
|
|
469
|
+
}
|
package/src/fileUtils.test.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getJsOnlyFiles,
|
|
3
|
+
separateSourceAndTestFiles,
|
|
4
|
+
findTestFilesForSource,
|
|
5
|
+
findTestFilesByImport,
|
|
6
|
+
findRelatedTestFilesBySource
|
|
7
|
+
} from './fileUtils';
|
|
2
8
|
import fs from 'fs';
|
|
3
9
|
import os from 'os';
|
|
4
10
|
import path from 'path';
|
|
@@ -319,3 +325,94 @@ describe('findTestFilesByImport', () => {
|
|
|
319
325
|
expect(result).toEqual(['src/real.test.js']);
|
|
320
326
|
});
|
|
321
327
|
});
|
|
328
|
+
|
|
329
|
+
describe('findRelatedTestFilesBySource', () => {
|
|
330
|
+
let tempDir;
|
|
331
|
+
|
|
332
|
+
function createFixture(structure) {
|
|
333
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-related-'));
|
|
334
|
+
Object.entries(structure).forEach(([filePath, content]) => {
|
|
335
|
+
const absolutePath = path.join(tempDir, filePath);
|
|
336
|
+
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
|
337
|
+
fs.writeFileSync(absolutePath, content);
|
|
338
|
+
});
|
|
339
|
+
return tempDir;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
afterEach(() => {
|
|
343
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it('includes same-name test files and differently named importer specs', () => {
|
|
347
|
+
const root = createFixture({
|
|
348
|
+
'src/widget.js': 'export const widget = 1;',
|
|
349
|
+
'src/widget.test.js': "import { widget } from './widget';\n",
|
|
350
|
+
'src/integration.spec.js': "import { widget } from './widget';\n"
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const result = findRelatedTestFilesBySource('src/widget.js', { root });
|
|
354
|
+
|
|
355
|
+
expect(result).toEqual(['src/integration.spec.js', 'src/widget.test.js']);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('finds a direct importer at depth 1', () => {
|
|
359
|
+
const root = createFixture({
|
|
360
|
+
'lib/helper.js': 'export const add = (a, b) => a + b;',
|
|
361
|
+
'lib/custom-name.spec.js': "import { add } from './helper';\n"
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const result = findRelatedTestFilesBySource('lib/helper.js', { root, maxDepth: 1 });
|
|
365
|
+
|
|
366
|
+
expect(result).toEqual(['lib/custom-name.spec.js']);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('finds a spec that reaches the source through one intermediate module at depth 2', () => {
|
|
370
|
+
const root = createFixture({
|
|
371
|
+
'lib/helper.js': 'export const add = (a, b) => a + b;',
|
|
372
|
+
'lib/service.js': "import { add } from './helper';\nexport const run = () => add(1, 2);\n",
|
|
373
|
+
'lib/feature.spec.js': "import { run } from './service';\n"
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
const depthOne = findRelatedTestFilesBySource('lib/helper.js', { root, maxDepth: 1 });
|
|
377
|
+
const depthTwo = findRelatedTestFilesBySource('lib/helper.js', { root, maxDepth: 2 });
|
|
378
|
+
|
|
379
|
+
expect(depthOne).toEqual([]);
|
|
380
|
+
expect(depthTwo).toEqual(['lib/feature.spec.js']);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('finds specs that import the source through a barrel index', () => {
|
|
384
|
+
const root = createFixture({
|
|
385
|
+
'src/utils/helper.js': 'export const add = (a, b) => a + b;',
|
|
386
|
+
'src/utils/index.js': "export * from './helper';\n",
|
|
387
|
+
'src/app.test.js': "import { add } from './utils';\n"
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
const result = findRelatedTestFilesBySource('src/utils/helper.js', { root, maxDepth: 2 });
|
|
391
|
+
|
|
392
|
+
expect(result).toEqual(['src/app.test.js']);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('resolves alias imports while walking the graph', () => {
|
|
396
|
+
const root = createFixture({
|
|
397
|
+
'src/utils/helper.js': 'export const add = (a, b) => a + b;',
|
|
398
|
+
'src/services/user.js': "import { add } from '@/utils/helper';\nexport const total = () => add(1, 1);\n",
|
|
399
|
+
'src/user.spec.js': "import { total } from './services/user';\n"
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const result = findRelatedTestFilesBySource('src/utils/helper.js', { root, maxDepth: 2 });
|
|
403
|
+
|
|
404
|
+
expect(result).toEqual(['src/user.spec.js']);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('does not return unrelated test files', () => {
|
|
408
|
+
const root = createFixture({
|
|
409
|
+
'src/utils/helper.js': 'export const add = (a, b) => a + b;',
|
|
410
|
+
'src/unrelated.test.js': "import { other } from './other';\n",
|
|
411
|
+
'src/other.js': 'export const other = 1;'
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const result = findRelatedTestFilesBySource('src/utils/helper.js', { root, maxDepth: 2 });
|
|
415
|
+
|
|
416
|
+
expect(result).toEqual([]);
|
|
417
|
+
});
|
|
418
|
+
});
|