Skip to content

Commit 804319b

Browse files
author
John Doe
committed
refactor: add logger helper unit tests
1 parent 2d12182 commit 804319b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import ansis from 'ansis';
2+
import path from 'node:path';
3+
import { afterEach, describe, expect, it, vi } from 'vitest';
4+
import { formatCommand } from './logger.js';
5+
6+
describe('formatCommand', () => {
7+
const originalCwd = process.cwd();
8+
9+
afterEach(() => {
10+
vi.restoreAllMocks();
11+
});
12+
13+
it('should format complex command with cwd, env, and status', () => {
14+
vi.spyOn(path, 'relative').mockReturnValue('<CWD>');
15+
expect(
16+
formatCommand(
17+
'npx eslint . --format=json',
18+
{
19+
cwd: '<CWD>',
20+
env: { CP_VERBOSE: true },
21+
},
22+
'failure',
23+
),
24+
).toBe(
25+
`${ansis.blue('<CWD>')} ${ansis.red('$')} ${ansis.gray('CP_VERBOSE=true')} npx eslint . --format=json`,
26+
);
27+
});
28+
29+
it.each([
30+
[undefined, ansis.blue], // default to pending
31+
['pending', ansis.blue],
32+
['success', ansis.green],
33+
['failure', ansis.red],
34+
])(`should format command status %s explicitly`, (status, color) => {
35+
expect(
36+
formatCommand('npx eslint . --format=json', {}, 'pending'),
37+
).toContain(`${color('$')}`);
38+
});
39+
40+
it('should include cwd prefix when cwd is provided and different from process.cwd()', () => {
41+
const mockCwd = path.join(originalCwd, 'src');
42+
vi.spyOn(path, 'relative').mockReturnValue('src');
43+
44+
expect(formatCommand('npx -v', { cwd: mockCwd })).toStartWith(
45+
`${ansis.blue('src')} `,
46+
);
47+
});
48+
49+
it('should not include cwd prefix when cwd is same as process.cwd()', () => {
50+
vi.spyOn(path, 'relative').mockReturnValue('');
51+
expect(formatCommand('npx -v', { cwd: originalCwd })).toStartWith(
52+
`${ansis.blue('$')}`,
53+
);
54+
});
55+
56+
it('should format command with single environment variable', () => {
57+
const result = formatCommand('npx eslint .', {
58+
env: { NODE_ENV: 'test' },
59+
});
60+
expect(result).toBe(
61+
`${ansis.blue('$')} ${ansis.gray('NODE_ENV=test')} npx eslint .`,
62+
);
63+
});
64+
});

0 commit comments

Comments
 (0)