mo3ta-coverage 1.2.1 → 1.3.3

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.
@@ -0,0 +1,430 @@
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import { execSync } from 'child_process';
5
+ import {
6
+ buildCapturedJestArgs,
7
+ checkChangedLineCoverage,
8
+ listChangedFiles,
9
+ mapRelatedTests,
10
+ Mo3taError
11
+ } from './api.js';
12
+
13
+ function git(repo, command) {
14
+ execSync(command, { cwd: repo, stdio: 'pipe' });
15
+ }
16
+
17
+ function initRepo(prefix) {
18
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
19
+ git(root, 'git init -b main -q');
20
+ git(root, 'git config user.email test@example.com');
21
+ git(root, 'git config user.name Test');
22
+ fs.mkdirSync(path.join(root, 'src'));
23
+ fs.writeFileSync(path.join(root, 'src', 'keep.js'), 'module.exports = { keep: 1 };\n');
24
+ git(root, 'git add src/keep.js && git commit -q -m base');
25
+ return root;
26
+ }
27
+
28
+ function writeMathFeature(root) {
29
+ git(root, 'git checkout -q -b feature');
30
+ fs.writeFileSync(path.join(root, 'src', 'math.js'), [
31
+ 'function add(a, b) {',
32
+ ' return a + b;',
33
+ '}',
34
+ 'function sub(a, b) {',
35
+ ' return a - b;',
36
+ '}',
37
+ 'module.exports = { add, sub };',
38
+ ''
39
+ ].join('\n'));
40
+ fs.writeFileSync(path.join(root, 'src', 'math.test.js'), [
41
+ "const { add } = require('./math');",
42
+ "test('add', () => {",
43
+ ' expect(add(1, 2)).toBe(3);',
44
+ '});',
45
+ ''
46
+ ].join('\n'));
47
+ fs.mkdirSync(path.join(root, 'specs'));
48
+ fs.writeFileSync(path.join(root, 'src', 'bare.js'), 'module.exports = { bare: 1 };\n');
49
+ fs.writeFileSync(path.join(root, 'specs', 'custom-name.spec.js'), "const { bare } = require('../src/bare');\ntest('bare', () => expect(bare.bare).toBe(1));\n");
50
+ git(root, 'git add src/math.js src/math.test.js src/bare.js specs/custom-name.spec.js && git commit -q -m feature');
51
+ }
52
+
53
+ describe('programmatic coverage API', () => {
54
+ let root;
55
+
56
+ afterEach(() => {
57
+ if (root && fs.existsSync(root)) {
58
+ fs.rmSync(root, { recursive: true, force: true });
59
+ }
60
+ root = undefined;
61
+ });
62
+
63
+ it('adds --runInBand by default for captured runs', () => {
64
+ expect(buildCapturedJestArgs(['--coverage'])).toEqual(['--coverage', '--watchAll=false', '--runInBand']);
65
+ });
66
+
67
+ it('drops --runInBand when the caller sets a worker strategy', () => {
68
+ expect(buildCapturedJestArgs(['--maxWorkers=2'])).toEqual(['--maxWorkers=2', '--watchAll=false']);
69
+ expect(buildCapturedJestArgs(['--maxWorkers', '2'])).toEqual(['--maxWorkers', '2', '--watchAll=false']);
70
+ expect(buildCapturedJestArgs(['-w', '2'])).toEqual(['-w', '2', '--watchAll=false']);
71
+ expect(buildCapturedJestArgs(['--runInBand'])).toEqual(['--runInBand', '--watchAll=false']);
72
+ expect(buildCapturedJestArgs(['-i'])).toEqual(['-i', '--watchAll=false']);
73
+ });
74
+
75
+ it('rejects a directory that is not a git repo', () => {
76
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-not-git-'));
77
+ expect(() => listChangedFiles({ root, useConfig: false })).toThrow(Mo3taError);
78
+ try {
79
+ listChangedFiles({ root, useConfig: false });
80
+ } catch (error) {
81
+ expect(error.code).toBe('NOT_A_GIT_REPO');
82
+ expect(error.hint).toMatch(/checkout/);
83
+ }
84
+ });
85
+
86
+ it('rejects a missing base ref with a fetch hint', () => {
87
+ root = initRepo('mo3ta-bad-base-');
88
+ expect(() => listChangedFiles({ root, base: 'origin/missing', useConfig: false })).toThrow(Mo3taError);
89
+ try {
90
+ listChangedFiles({ root, base: 'origin/missing', useConfig: false });
91
+ } catch (error) {
92
+ expect(error.code).toBe('INVALID_BASE_REF');
93
+ expect(error.message).toContain('origin/missing');
94
+ expect(error.hint).toMatch(/git fetch/);
95
+ }
96
+ });
97
+
98
+ it('lists committed and unstaged JS/TS changes versus the base ref', () => {
99
+ root = initRepo('mo3ta-list-');
100
+ writeMathFeature(root);
101
+ fs.writeFileSync(path.join(root, 'src', 'keep.js'), 'module.exports = { keep: 2 };\n');
102
+
103
+ const result = listChangedFiles({ root, base: 'main', useConfig: false });
104
+
105
+ expect(result.detection).toBe('git');
106
+ expect(result.sourceFiles).toEqual(['src/bare.js', 'src/keep.js', 'src/math.js']);
107
+ expect(result.testFiles).toEqual(['specs/custom-name.spec.js', 'src/math.test.js']);
108
+ expect(result.summary).toContain('src/math.js');
109
+ });
110
+
111
+ it('scans an explicit list and does not fall back to git when nothing scannable remains', () => {
112
+ root = initRepo('mo3ta-explicit-');
113
+ writeMathFeature(root);
114
+
115
+ const explicit = listChangedFiles({
116
+ root,
117
+ base: 'main',
118
+ files: ['src/math.js'],
119
+ useConfig: false
120
+ });
121
+ expect(explicit.detection).toBe('explicit');
122
+ expect(explicit.sourceFiles).toEqual(['src/math.js']);
123
+
124
+ const empty = listChangedFiles({
125
+ root,
126
+ base: 'main',
127
+ files: ['node_modules/pkg/index.js'],
128
+ useConfig: false
129
+ });
130
+ expect(empty.files).toEqual([]);
131
+ expect(empty.warning).toMatch(/was not used/);
132
+ });
133
+
134
+ it('maps naming matches and the fast-mode import fallback', () => {
135
+ root = initRepo('mo3ta-map-');
136
+ writeMathFeature(root);
137
+
138
+ const smart = mapRelatedTests({
139
+ root,
140
+ base: 'main',
141
+ files: ['src/math.js', 'src/bare.js'],
142
+ mode: 'fast',
143
+ useConfig: false
144
+ });
145
+
146
+ const math = smart.mappings.find(mapping => mapping.source === 'src/math.js');
147
+ const bare = smart.mappings.find(mapping => mapping.source === 'src/bare.js');
148
+ expect(math.matchType).toBe('naming convention');
149
+ expect(math.tests).toContain('src/math.test.js');
150
+ expect(bare.matchType).toBe('import scan');
151
+ expect(bare.tests).toEqual(['specs/custom-name.spec.js']);
152
+ expect(smart.testsToRun).toEqual(expect.arrayContaining([
153
+ 'src/math.test.js',
154
+ 'specs/custom-name.spec.js'
155
+ ]));
156
+ });
157
+
158
+ it('tells the caller that full mode cannot expand tests without Jest', () => {
159
+ root = initRepo('mo3ta-full-');
160
+ writeMathFeature(root);
161
+
162
+ const result = mapRelatedTests({
163
+ root,
164
+ base: 'main',
165
+ files: ['src/math.js'],
166
+ mode: 'full',
167
+ useConfig: false
168
+ });
169
+
170
+ expect(result.delegatedToJest).toBe(true);
171
+ expect(result.testsToRun).toEqual([]);
172
+ expect(result.jestTargets).toEqual(['src/math.js']);
173
+ expect(result.warning).toMatch(/findRelatedTests/);
174
+ });
175
+
176
+ it('reads base and mode from .mo3ta-coverage.json unless arguments override them', () => {
177
+ root = initRepo('mo3ta-config-');
178
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), JSON.stringify({
179
+ base: 'main',
180
+ mode: 'fast',
181
+ depth: 1
182
+ }));
183
+
184
+ const fromFile = mapRelatedTests({ root, files: ['src/keep.js'] });
185
+ expect(fromFile.base).toBe('main');
186
+ expect(fromFile.mode).toBe('fast');
187
+
188
+ const override = mapRelatedTests({ root, files: ['src/keep.js'], mode: 'full' });
189
+ expect(override.mode).toBe('full');
190
+ });
191
+
192
+ it('accepts extra Jest flags and reads a jest default from the config file', () => {
193
+ root = initRepo('mo3ta-jest-config-');
194
+ writeMathFeature(root);
195
+ const source = path.join(root, 'src', 'math.js');
196
+ const lcov = path.join(root, 'coverage', 'lcov.info');
197
+ fs.mkdirSync(path.dirname(lcov), { recursive: true });
198
+ fs.writeFileSync(lcov, ['TN:', `SF:${source}`, 'DA:2,1', 'end_of_record', ''].join('\n'));
199
+
200
+ const explicit = checkChangedLineCoverage({
201
+ root,
202
+ base: 'main',
203
+ files: ['src/math.js'],
204
+ mode: 'fast',
205
+ runTests: false,
206
+ jest: ['--maxWorkers=2', '--silent'],
207
+ useConfig: false
208
+ });
209
+ expect(explicit.jestArgs).toEqual(['--maxWorkers=2', '--silent']);
210
+
211
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), JSON.stringify({
212
+ base: 'main',
213
+ mode: 'fast',
214
+ jest: ['--bail']
215
+ }));
216
+
217
+ const fromConfig = checkChangedLineCoverage({
218
+ root,
219
+ files: ['src/math.js'],
220
+ runTests: false
221
+ });
222
+ expect(fromConfig.jestArgs).toEqual(['--bail']);
223
+
224
+ const override = checkChangedLineCoverage({
225
+ root,
226
+ files: ['src/math.js'],
227
+ runTests: false,
228
+ jest: '--maxWorkers=1,--silent'
229
+ });
230
+ expect(override.jestArgs).toEqual(['--maxWorkers=1', '--silent']);
231
+ });
232
+
233
+ it('reports an invalid config file instead of throwing a raw parse error', () => {
234
+ root = initRepo('mo3ta-bad-config-');
235
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), '{');
236
+
237
+ expect(() => listChangedFiles({ root })).toThrow(Mo3taError);
238
+ try {
239
+ listChangedFiles({ root });
240
+ } catch (error) {
241
+ expect(error.code).toBe('INVALID_CONFIG');
242
+ }
243
+ });
244
+
245
+ it('grades an existing lcov file and returns the uncovered source line', () => {
246
+ root = initRepo('mo3ta-lcov-');
247
+ writeMathFeature(root);
248
+ const source = path.join(root, 'src', 'math.js');
249
+ const lcov = path.join(root, 'coverage', 'lcov.info');
250
+ fs.mkdirSync(path.dirname(lcov), { recursive: true });
251
+ fs.writeFileSync(lcov, [
252
+ 'TN:',
253
+ `SF:${source}`,
254
+ 'DA:2,1',
255
+ 'DA:5,0',
256
+ 'end_of_record',
257
+ ''
258
+ ].join('\n'));
259
+
260
+ const result = checkChangedLineCoverage({
261
+ root,
262
+ base: 'main',
263
+ files: ['src/math.js'],
264
+ mode: 'fast',
265
+ limit: 80,
266
+ runTests: false,
267
+ showCovered: true,
268
+ ciReport: 'coverage/mo3ta-coverage-report.md',
269
+ useConfig: false
270
+ });
271
+
272
+ expect(result.coveragePassed).toBe(false);
273
+ expect(result.ok).toBe(false);
274
+ expect(result.cliWouldExit).toBe(1);
275
+ expect(result.totals).toEqual({ changed: 2, covered: 1, uncovered: 1, coverage: 50 });
276
+ const math = result.files.find(file => file.file === 'src/math.js');
277
+ expect(math.uncoveredLines).toEqual([5]);
278
+ expect(math.coveredLines).toEqual([2]);
279
+ expect(math.uncovered).toEqual([{ line: 5, text: ' return a - b;' }]);
280
+ expect(result.summary).toContain('FAILED');
281
+ expect(result.summary).toContain('return a - b');
282
+ const report = fs.readFileSync(path.join(root, 'coverage', 'mo3ta-coverage-report.md'), 'utf8');
283
+ expect(report).toContain('src/math.js');
284
+ expect(report).toContain('50.00%');
285
+ });
286
+
287
+ it('fails clearly when Jest is not installed and tests were requested', () => {
288
+ root = initRepo('mo3ta-no-jest-');
289
+ writeMathFeature(root);
290
+
291
+ expect(() => checkChangedLineCoverage({
292
+ root,
293
+ base: 'main',
294
+ files: ['src/math.js'],
295
+ runTests: true,
296
+ useConfig: false
297
+ })).toThrow(Mo3taError);
298
+
299
+ try {
300
+ checkChangedLineCoverage({
301
+ root,
302
+ base: 'main',
303
+ files: ['src/math.js'],
304
+ runTests: true,
305
+ useConfig: false
306
+ });
307
+ } catch (error) {
308
+ expect(error.code).toBe('JEST_NOT_FOUND');
309
+ expect(error.hint).toMatch(/node_modules\/jest/);
310
+ }
311
+ });
312
+
313
+ it('does not run Jest when no test file is selected', () => {
314
+ root = initRepo('mo3ta-no-tests-');
315
+
316
+ const result = checkChangedLineCoverage({
317
+ root,
318
+ base: 'main',
319
+ files: ['src/keep.js'],
320
+ mode: 'fast',
321
+ runTests: true,
322
+ useConfig: false
323
+ });
324
+
325
+ expect(result.ok).toBe(false);
326
+ expect(result.testsNotRunReason).toBe('no-tests');
327
+ expect(result.cliWouldExit).toBe(0);
328
+ expect(result.jest.ran).toBe(false);
329
+ expect(result.summary).toMatch(/exit 0/);
330
+ });
331
+
332
+ it('reports a missing lcov file instead of crashing', () => {
333
+ root = initRepo('mo3ta-no-lcov-');
334
+ writeMathFeature(root);
335
+
336
+ expect(() => checkChangedLineCoverage({
337
+ root,
338
+ base: 'main',
339
+ files: ['src/math.js'],
340
+ runTests: false,
341
+ useConfig: false
342
+ })).toThrow(Mo3taError);
343
+
344
+ try {
345
+ checkChangedLineCoverage({
346
+ root,
347
+ base: 'main',
348
+ files: ['src/math.js'],
349
+ runTests: false,
350
+ useConfig: false
351
+ });
352
+ } catch (error) {
353
+ expect(error.code).toBe('LCOV_NOT_FOUND');
354
+ expect(error.hint).toMatch(/runTests/);
355
+ }
356
+ });
357
+
358
+ it('rejects an unknown mode and an out-of-range limit', () => {
359
+ root = initRepo('mo3ta-invalid-');
360
+
361
+ expect(() => mapRelatedTests({ root, mode: 'turbo', useConfig: false })).toThrow(Mo3taError);
362
+ try {
363
+ mapRelatedTests({ root, mode: 'turbo', useConfig: false });
364
+ } catch (error) {
365
+ expect(error.code).toBe('INVALID_MODE');
366
+ }
367
+
368
+ try {
369
+ checkChangedLineCoverage({ root, limit: 140, runTests: false, useConfig: false });
370
+ throw new Error('expected limit to throw');
371
+ } catch (error) {
372
+ expect(error).toBeInstanceOf(Mo3taError);
373
+ expect(error.code).toBe('INVALID_LIMIT');
374
+ }
375
+ });
376
+
377
+ it('narrowing tests with a filter keeps the pre-filter list for validation', () => {
378
+ root = initRepo('mo3ta-filter-');
379
+ writeMathFeature(root);
380
+
381
+ const result = mapRelatedTests({
382
+ root,
383
+ base: 'main',
384
+ files: ['src/math.js', 'src/bare.js'],
385
+ mode: 'smart',
386
+ filter: 'math',
387
+ useConfig: false
388
+ });
389
+
390
+ expect(result.filter).toBe('math');
391
+ expect(result.allTestsToRun).toEqual(expect.arrayContaining([
392
+ 'src/math.test.js',
393
+ 'specs/custom-name.spec.js'
394
+ ]));
395
+ expect(result.allTestsToRun).toHaveLength(2);
396
+ expect(result.testsToRun).toEqual(['src/math.test.js']);
397
+ expect(result.summary).toContain('Filter /math/ kept 1 of 2 candidate test file(s).');
398
+ });
399
+
400
+ it('accepts a filter default from .mo3ta-coverage.json and overrides it explicitly', () => {
401
+ root = initRepo('mo3ta-filter-config-');
402
+ writeMathFeature(root);
403
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), JSON.stringify({
404
+ base: 'main',
405
+ mode: 'smart',
406
+ filter: 'specs/'
407
+ }));
408
+
409
+ const fromConfig = mapRelatedTests({ root, files: ['src/math.js'] });
410
+ expect(fromConfig.filter).toBe('specs\\/');
411
+ expect(fromConfig.testsToRun).toEqual([]);
412
+
413
+ const override = mapRelatedTests({ root, files: ['src/math.js'], filter: 'math' });
414
+ expect(override.filter).toBe('math');
415
+ expect(override.testsToRun).toEqual(['src/math.test.js']);
416
+ });
417
+
418
+ it('fails fast on an invalid filter regex before running anything', () => {
419
+ root = initRepo('mo3ta-bad-filter-');
420
+ writeMathFeature(root);
421
+
422
+ expect(() => mapRelatedTests({ root, base: 'main', files: ['src/math.js'], filter: '(unclosed', useConfig: false })).toThrow(Mo3taError);
423
+ try {
424
+ mapRelatedTests({ root, base: 'main', files: ['src/math.js'], filter: '(unclosed', useConfig: false });
425
+ } catch (error) {
426
+ expect(error.code).toBe('INVALID_FILTER');
427
+ expect(error.hint).toMatch(/regular expression/i);
428
+ }
429
+ });
430
+ });