Fix/post list excerpt rendering (#41)

* feat: use excerpts in post lists

* chore: made testing less noisy

---------

Co-authored-by: hugo <hugoms@me.com>
This commit is contained in:
Georg Bauer
2026-03-07 10:15:57 +01:00
committed by GitHub
parent 9871cb827f
commit f1c9038803
8 changed files with 349 additions and 124 deletions

View File

@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest';
import { withCapturedConsole } from './consoleCapture';
describe('withCapturedConsole', () => {
it('captures expected console output without leaking it to the test run', async () => {
await withCapturedConsole('error', async (captured) => {
const error = new Error('expected failure');
console.error('[Test]', error);
expect(captured.spy).toHaveBeenCalledWith('[Test]', error);
expect(captured.text()).toContain('[Test]');
expect(captured.text()).toContain('expected failure');
});
});
});

View File

@@ -0,0 +1,40 @@
import { vi } from 'vitest';
type ConsoleMethod = 'error' | 'warn' | 'log' | 'info' | 'debug';
function stringifyConsoleArg(arg: unknown): string {
if (arg instanceof Error) {
return arg.stack ?? arg.message;
}
if (typeof arg === 'string') {
return arg;
}
try {
return JSON.stringify(arg);
} catch {
return String(arg);
}
}
export async function withCapturedConsole<T>(
method: ConsoleMethod,
run: (captured: {
spy: ReturnType<typeof vi.spyOn>;
calls: () => unknown[][];
text: () => string;
}) => Promise<T> | T,
): Promise<T> {
const spy = vi.spyOn(console, method).mockImplementation(() => {});
try {
return await run({
spy,
calls: () => spy.mock.calls,
text: () => spy.mock.calls.map((call) => call.map(stringifyConsoleArg).join(' ')).join('\n'),
});
} finally {
spy.mockRestore();
}
}

View File

@@ -3,4 +3,5 @@
* Re-exports all test utilities for convenient importing
*/
export * from './consoleCapture';
export * from './factories';