|
- import wrapAnsi from 'wrap-ansi';
- import {
- afterAll,
- beforeAll,
- describe,
- expect,
- it, SpyInstanceFn,
- vi,
- } from 'vitest';
- import centered from '../centered';
-
- vi.mock('wrap-ansi');
-
- describe('centered', () => {
- let log: SpyInstanceFn<[], void>;
-
- let defaultConsole: typeof console;
- beforeAll(() => {
- defaultConsole = console;
- log = vi.fn();
- global.console = {
- log,
- } as unknown as typeof console;
- });
-
- afterAll(() => {
- global.console = defaultConsole;
- });
-
- it('calls wrap and ensures console has an assumed width of 80 text columns', async () => {
- const defaultColumns = process.stdout.columns;
- process.stdout.columns = undefined as unknown as typeof process.stdout.columns;
- await centered()('foo\n-italic');
- expect(wrapAnsi).toBeCalledWith('foo\n-italic', 80, { hard: true, trim: true });
- process.stdout.columns = defaultColumns;
- });
-
- it('calls wrap and respects current text column count', async () => {
- const defaultColumns = process.stdout.columns;
- process.stdout.columns = 420;
- await centered()('foo\n-italic');
- expect(wrapAnsi).toBeCalledWith('foo\n-italic', 420, { hard: true, trim: true });
- process.stdout.columns = defaultColumns;
- });
-
- it('calls the console output function', async () => {
- await centered()('foo\n-italic');
- expect(log).toBeCalled();
- });
- });
|