Skip to content

Commit 45473d0

Browse files
committed
chore: test coverage
1 parent 6030bd4 commit 45473d0

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

test/unit/ui/scan/printMissing.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2-
import chalk from 'chalk';
32
import { printMissing } from '../../../../src/ui/scan/printMissing.js';
43
import type { EnvUsage } from '../../../../src/config/types.js';
54

@@ -21,6 +20,7 @@ describe('printMissing', () => {
2120

2221
it('returns false when no missing variables', () => {
2322
const result = printMissing([], [], '.env');
23+
2424
expect(result).toBe(false);
2525
expect(logSpy).not.toHaveBeenCalled();
2626
});
@@ -29,6 +29,70 @@ describe('printMissing', () => {
2929
const used: EnvUsage[] = [];
3030

3131
const result = printMissing(['UNKNOWN'], used, '.env');
32+
33+
expect(result).toBe(true);
34+
expect(logSpy).toHaveBeenCalledTimes(4);
35+
expect(logSpy).toHaveBeenNthCalledWith(
36+
2,
37+
expect.stringContaining('Missing in .env'),
38+
);
39+
});
40+
41+
it('prints grouped missing usages and falls back to environment file label', () => {
42+
const used: EnvUsage[] = [
43+
{
44+
variable: 'API_KEY',
45+
file: 'src/a.ts',
46+
line: 10,
47+
column: 1,
48+
pattern: 'process.env',
49+
context: 'process.env.API_KEY',
50+
},
51+
{
52+
variable: 'API_KEY',
53+
file: 'src/a.ts',
54+
line: 12,
55+
column: 1,
56+
pattern: 'process.env',
57+
context: 'process.env.API_KEY',
58+
},
59+
{
60+
variable: 'JWT_SECRET',
61+
file: 'src/b.ts',
62+
line: 7,
63+
column: 1,
64+
pattern: 'process.env',
65+
context: 'process.env.JWT_SECRET',
66+
},
67+
{
68+
variable: 'UNUSED',
69+
file: 'src/c.ts',
70+
line: 1,
71+
column: 1,
72+
pattern: 'process.env',
73+
context: 'process.env.UNUSED',
74+
},
75+
];
76+
77+
const result = printMissing(['API_KEY', 'JWT_SECRET'], used, '');
78+
3279
expect(result).toBe(true);
80+
expect(logSpy).toHaveBeenCalledTimes(7);
81+
expect(logSpy).toHaveBeenNthCalledWith(
82+
2,
83+
expect.stringContaining('Missing in environment file'),
84+
);
85+
expect(logSpy).toHaveBeenNthCalledWith(
86+
4,
87+
expect.stringContaining('normalized:src/a.ts:10'),
88+
);
89+
expect(logSpy).toHaveBeenNthCalledWith(
90+
5,
91+
expect.stringContaining('normalized:src/a.ts:12'),
92+
);
93+
expect(logSpy).toHaveBeenNthCalledWith(
94+
6,
95+
expect.stringContaining('normalized:src/b.ts:7'),
96+
);
3397
});
3498
});

test/unit/ui/scan/printUnused.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2-
import chalk from 'chalk';
32
import { printUnused } from '../../../../src/ui/scan/printUnused.js';
43

54
describe('printUnused', () => {
@@ -17,4 +16,50 @@ describe('printUnused', () => {
1716
printUnused([], '.env');
1817
expect(logSpy).not.toHaveBeenCalled();
1918
});
19+
20+
it('prints unused variables with provided file name in non-strict mode', () => {
21+
printUnused(['API_KEY', 'JWT_SECRET'], '.env');
22+
23+
expect(logSpy).toHaveBeenCalledTimes(6);
24+
expect(logSpy).toHaveBeenNthCalledWith(
25+
2,
26+
expect.stringContaining('Unused in .env'),
27+
);
28+
expect(logSpy).toHaveBeenNthCalledWith(
29+
4,
30+
expect.stringContaining('API_KEY'),
31+
);
32+
expect(logSpy).toHaveBeenNthCalledWith(
33+
5,
34+
expect.stringContaining('JWT_SECRET'),
35+
);
36+
});
37+
38+
it('uses fallback file label when comparedAgainst is empty', () => {
39+
printUnused(['SOME_VAR'], '');
40+
41+
expect(logSpy).toHaveBeenCalledTimes(5);
42+
expect(logSpy).toHaveBeenNthCalledWith(
43+
2,
44+
expect.stringContaining('Unused in environment file'),
45+
);
46+
});
47+
48+
it('prints output in strict mode', () => {
49+
printUnused(['SOME_VAR'], '.env', true);
50+
51+
expect(logSpy).toHaveBeenCalledTimes(5);
52+
expect(logSpy).toHaveBeenNthCalledWith(
53+
2,
54+
expect.stringContaining('Unused in .env'),
55+
);
56+
expect(logSpy).toHaveBeenNthCalledWith(
57+
4,
58+
expect.stringContaining('SOME_VAR'),
59+
);
60+
expect(logSpy).toHaveBeenNthCalledWith(
61+
4,
62+
expect.stringContaining('unused'),
63+
);
64+
});
2065
});

0 commit comments

Comments
 (0)