Files
repomix-mirror/tests/cli/cliSpinner.test.ts
Kazuki YamadaandClaude Opus 5.5 0fbf578630 test(config): Restore and clear mocks globally via Vitest config
intent(test-hygiene): stop repeating mock cleanup by hand in 60 test files
decision(vitest-config): enable restoreMocks and clearMocks; both run before each test, ahead of beforeEach hooks, so every vi.spyOn() is back on the original method and every mock starts with empty call history
decision(mock-reset): keep mockReset off; it would rewind vi.fn() doubles configured once at module or describe scope (e.g. createMcpServerDouble, file-level deps objects)
decision(reset-calls): keep vi.resetAllMocks() in configLoad, globalDirectory, packageJsonParse, fileProcessContent, fileSearch, permissionCheck and sandbox.contract, whose file-scoped vi.fn() doubles get persistent or possibly unconsumed Once implementations inside tests; drop it where doubles are rebuilt in beforeEach or per test
constraint(vitest-5): clearMocks already defaults to true and restoreAllMocks only touches vi.spyOn(), not vi.fn(), so removing the manual calls does not change behaviour of vi.fn() doubles
learned(logger-doubles): silenceLogger() spies are restored before every test now, so they must be (re)created in beforeEach, after any vi.resetAllMocks()

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 00:14:46 +09:00

122 lines
3.9 KiB
TypeScript

import type { Spinner as PicoSpinner } from 'picospinner';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Spinner } from '../../src/cli/cliSpinner.js';
import type { CliOptions } from '../../src/cli/types.js';
const mockPicoInstances: Array<{
start: ReturnType<typeof vi.fn>;
setText: ReturnType<typeof vi.fn>;
succeed: ReturnType<typeof vi.fn>;
fail: ReturnType<typeof vi.fn>;
stop: ReturnType<typeof vi.fn>;
}> = [];
class MockPicoSpinner {
start = vi.fn();
setText = vi.fn();
succeed = vi.fn();
fail = vi.fn();
stop = vi.fn();
constructor() {
mockPicoInstances.push(this);
}
}
const deps = { PicoSpinner: MockPicoSpinner as unknown as typeof PicoSpinner };
describe('cliSpinner', () => {
beforeEach(() => {
mockPicoInstances.length = 0;
});
const getLastPicoInstance = () => mockPicoInstances[mockPicoInstances.length - 1];
describe('Spinner', () => {
describe('when quiet mode is disabled', () => {
it('should start spinner', () => {
const spinner = new Spinner('Processing...', {} as CliOptions, deps);
spinner.start();
expect(getLastPicoInstance().start).toHaveBeenCalled();
});
it('should update spinner message', () => {
const spinner = new Spinner('Initial message', {} as CliOptions, deps);
spinner.start();
spinner.update('Updated message');
expect(getLastPicoInstance().setText).toHaveBeenCalledWith('Updated message', false);
});
it('should succeed with success message', () => {
const spinner = new Spinner('Processing...', {} as CliOptions, deps);
spinner.start();
spinner.succeed('Success!');
expect(getLastPicoInstance().succeed).toHaveBeenCalledWith('Success!');
});
it('should fail with error message', () => {
const spinner = new Spinner('Processing...', {} as CliOptions, deps);
spinner.start();
spinner.fail('Failed!');
expect(getLastPicoInstance().fail).toHaveBeenCalledWith('Failed!');
});
});
describe('when quiet mode is enabled', () => {
it('should not create PicoSpinner when quiet flag is true', () => {
const instancesBefore = mockPicoInstances.length;
const spinner = new Spinner('Processing...', { quiet: true } as CliOptions, deps);
spinner.start();
expect(mockPicoInstances.length).toBe(instancesBefore);
});
it('should not create PicoSpinner when verbose flag is true', () => {
const instancesBefore = mockPicoInstances.length;
const spinner = new Spinner('Processing...', { verbose: true } as CliOptions, deps);
spinner.start();
expect(mockPicoInstances.length).toBe(instancesBefore);
});
it('should not create PicoSpinner when stdout flag is true', () => {
const instancesBefore = mockPicoInstances.length;
const spinner = new Spinner('Processing...', { stdout: true } as CliOptions, deps);
spinner.start();
expect(mockPicoInstances.length).toBe(instancesBefore);
});
it('should not throw when update is called in quiet mode', () => {
const spinner = new Spinner('Initial', { quiet: true } as CliOptions, deps);
expect(() => {
spinner.start();
spinner.update('Updated');
}).not.toThrow();
});
it('should not throw when succeed is called in quiet mode', () => {
const spinner = new Spinner('Processing...', { quiet: true } as CliOptions, deps);
expect(() => {
spinner.start();
spinner.succeed('Success!');
}).not.toThrow();
});
it('should not throw when fail is called in quiet mode', () => {
const spinner = new Spinner('Processing...', { quiet: true } as CliOptions, deps);
expect(() => {
spinner.start();
spinner.fail('Failed!');
}).not.toThrow();
});
});
});
});