Skip to content

Commit 4c684a8

Browse files
committed
refactor(tests): simplify test utilities and assertions
- Use forEach instead of for...of in statusline test loop - Inline fetcher variable in system-info test - Simplify captureOutput to return both output and result via object destructuring, eliminating mutable let bindings in callers
1 parent a038201 commit 4c684a8

3 files changed

Lines changed: 23 additions & 30 deletions

File tree

tests/bin/cli-statusline.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ describe('--statusline flag', () => {
102102
},
103103
];
104104

105-
for (const mode of modes) {
105+
modes.forEach((mode) => {
106106
test(`shows ${mode.name}`, async () => {
107107
await expectStatusline(
108108
{ CLAUDE_SETTINGS_PATH: enabledSettingsPath, ...mode.env },
109109
mode.output,
110110
);
111111
});
112-
}
112+
});
113113
});
114114

115115
describe('--statusline enabled/disabled detection', () => {

tests/bin/doctor/system-info.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ describe('getSystemInfo', () => {
9797

9898
test('starts both copilot version probes immediately and prefers --binary-version', async () => {
9999
const probes = createCopilotDeferredFetcher();
100-
const fetcher = probes.fetcher;
101-
const sysInfoPromise = getSystemInfo(fetcher);
100+
const sysInfoPromise = getSystemInfo(probes.fetcher);
102101
await Promise.resolve();
103102

104103
expectCopilotVersionProbesStarted(probes.calls);

tests/bin/help.test.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,77 @@ import { printCommandHelp, printHelp, printVersion, showCommandHelp } from '@/bi
55
/**
66
* Capture console.log output during a function call.
77
*/
8-
function captureOutput(fn: () => void): string {
8+
function captureOutput<T>(fn: () => T) {
99
const originalLog = console.log;
1010
let output = '';
1111
console.log = (...args: unknown[]) => {
1212
output += `${args.map(String).join(' ')}\n`;
1313
};
1414
try {
15-
fn();
15+
const result = fn();
16+
return { output, result };
1617
} finally {
1718
console.log = originalLog;
1819
}
19-
return output;
2020
}
2121

2222
describe('help output', () => {
2323
describe('printHelp (main help)', () => {
2424
test('contains version header', () => {
25-
const output = captureOutput(() => printHelp());
25+
const { output } = captureOutput(() => printHelp());
2626
expect(output).toContain('cc-safety-net v');
2727
});
2828

2929
test('contains description', () => {
30-
const output = captureOutput(() => printHelp());
30+
const { output } = captureOutput(() => printHelp());
3131
expect(output).toContain('Blocks destructive git and filesystem commands');
3232
});
3333

3434
test('lists all visible commands', () => {
35-
const output = captureOutput(() => printHelp());
35+
const { output } = captureOutput(() => printHelp());
3636
expect(output).toContain('doctor');
3737
expect(output).toContain('explain');
3838
expect(output).toContain('claude-code');
3939
expect(output).toContain('gemini-cli');
4040
});
4141

4242
test('contains COMMANDS section', () => {
43-
const output = captureOutput(() => printHelp());
43+
const { output } = captureOutput(() => printHelp());
4444
expect(output).toContain('COMMANDS:');
4545
});
4646

4747
test('contains GLOBAL OPTIONS section', () => {
48-
const output = captureOutput(() => printHelp());
48+
const { output } = captureOutput(() => printHelp());
4949
expect(output).toContain('GLOBAL OPTIONS:');
5050
expect(output).toContain('--help');
5151
expect(output).toContain('--version');
5252
});
5353

5454
test('contains HELP section with usage hints', () => {
55-
const output = captureOutput(() => printHelp());
55+
const { output } = captureOutput(() => printHelp());
5656
expect(output).toContain('HELP:');
5757
expect(output).toContain('help <command>');
5858
expect(output).toContain('<command> --help');
5959
});
6060

6161
test('contains ENVIRONMENT VARIABLES section', () => {
62-
const output = captureOutput(() => printHelp());
62+
const { output } = captureOutput(() => printHelp());
6363
expect(output).toContain('ENVIRONMENT VARIABLES:');
6464
expect(output).toContain('SAFETY_NET_STRICT');
6565
expect(output).toContain('SAFETY_NET_PARANOID');
6666
expect(output).toContain('SAFETY_NET_WORKTREE');
6767
});
6868

6969
test('contains CONFIG FILES section', () => {
70-
const output = captureOutput(() => printHelp());
70+
const { output } = captureOutput(() => printHelp());
7171
expect(output).toContain('CONFIG FILES:');
7272
expect(output).toContain('.safety-net.json');
7373
});
7474
});
7575

7676
describe('printVersion', () => {
7777
test('prints version string', () => {
78-
const output = captureOutput(() => printVersion());
78+
const { output } = captureOutput(() => printVersion());
7979
// Version is either "dev" or a semver string
8080
expect(output.trim()).toMatch(/^(dev|\d+\.\d+\.\d+.*)$/);
8181
});
@@ -85,29 +85,29 @@ describe('help output', () => {
8585
test('prints command name', () => {
8686
const cmd = findCommand('doctor');
8787
if (!cmd) throw new Error('doctor command not found');
88-
const output = captureOutput(() => printCommandHelp(cmd));
88+
const { output } = captureOutput(() => printCommandHelp(cmd));
8989
expect(output).toContain('cc-safety-net doctor');
9090
});
9191

9292
test('prints description', () => {
9393
const cmd = findCommand('doctor');
9494
if (!cmd) throw new Error('doctor command not found');
95-
const output = captureOutput(() => printCommandHelp(cmd));
95+
const { output } = captureOutput(() => printCommandHelp(cmd));
9696
expect(output).toContain('Run diagnostic checks');
9797
});
9898

9999
test('prints USAGE section', () => {
100100
const cmd = findCommand('doctor');
101101
if (!cmd) throw new Error('doctor command not found');
102-
const output = captureOutput(() => printCommandHelp(cmd));
102+
const { output } = captureOutput(() => printCommandHelp(cmd));
103103
expect(output).toContain('USAGE:');
104104
expect(output).toContain('doctor [options]');
105105
});
106106

107107
test('prints OPTIONS section', () => {
108108
const cmd = findCommand('doctor');
109109
if (!cmd) throw new Error('doctor command not found');
110-
const output = captureOutput(() => printCommandHelp(cmd));
110+
const { output } = captureOutput(() => printCommandHelp(cmd));
111111
expect(output).toContain('OPTIONS:');
112112
expect(output).toContain('--json');
113113
expect(output).toContain('--skip-update-check');
@@ -116,36 +116,30 @@ describe('help output', () => {
116116
test('prints EXAMPLES section when available', () => {
117117
const cmd = findCommand('doctor');
118118
if (!cmd) throw new Error('doctor command not found');
119-
const output = captureOutput(() => printCommandHelp(cmd));
119+
const { output } = captureOutput(() => printCommandHelp(cmd));
120120
expect(output).toContain('EXAMPLES:');
121121
expect(output).toContain('cc-safety-net doctor');
122122
});
123123

124124
test('explain command shows --cwd option with argument', () => {
125125
const cmd = findCommand('explain');
126126
if (!cmd) throw new Error('explain command not found');
127-
const output = captureOutput(() => printCommandHelp(cmd));
127+
const { output } = captureOutput(() => printCommandHelp(cmd));
128128
expect(output).toContain('--cwd');
129129
expect(output).toContain('<path>');
130130
});
131131
});
132132

133133
describe('showCommandHelp', () => {
134134
test('returns true and prints help for valid command', () => {
135-
let result = false;
136-
const output = captureOutput(() => {
137-
result = showCommandHelp('doctor');
138-
});
135+
const { output, result } = captureOutput(() => showCommandHelp('doctor'));
139136

140137
expect(result).toBe(true);
141138
expect(output).toContain('cc-safety-net doctor');
142139
});
143140

144141
test('returns true for alias', () => {
145-
let result = false;
146-
const output = captureOutput(() => {
147-
result = showCommandHelp('-cc');
148-
});
142+
const { output, result } = captureOutput(() => showCommandHelp('-cc'));
149143

150144
expect(result).toBe(true);
151145
expect(output).toContain('cc-safety-net claude-code');

0 commit comments

Comments
 (0)