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:
17
tests/utils/consoleCapture.test.ts
Normal file
17
tests/utils/consoleCapture.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
||||
40
tests/utils/consoleCapture.ts
Normal file
40
tests/utils/consoleCapture.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,5 @@
|
||||
* Re-exports all test utilities for convenient importing
|
||||
*/
|
||||
|
||||
export * from './consoleCapture';
|
||||
export * from './factories';
|
||||
|
||||
Reference in New Issue
Block a user