Spaces:
Running
Running
Download server/logCapture.ts from Felladrin/MiniSearch: direct link, hf CLI and curl.
- Browser
- Download file 890 Bytes
-
https://huggingface.co/spaces/Felladrin/MiniSearch/resolve/main/server/logCapture.ts
- Command line
-
hf download hf://spaces/Felladrin/MiniSearch/server/logCapture.ts
-
curl -L -o logCapture.ts https://huggingface.co/spaces/Felladrin/MiniSearch/resolve/main/server/logCapture.ts
890 Bytes
| import debug from "debug"; | |
| import { vi } from "vitest"; | |
| /** | |
| * Captures what a module logs. `debug` resolves its writer at call time, so | |
| * replacing it here catches everything the module logs through `debug`, which | |
| * under jsdom never reaches `console` in a spy-able way. The console spies | |
| * cover the calls the module makes directly, so a new logging line is caught | |
| * whichever it uses. | |
| */ | |
| export function startLogCapture() { | |
| const lines: string[] = []; | |
| const record = (...args: unknown[]) => { | |
| lines.push(args.map(String).join(" ")); | |
| }; | |
| const originalLog = debug.log; | |
| debug.log = record; | |
| const spies = (["log", "info", "warn", "error", "debug"] as const).map( | |
| (method) => vi.spyOn(console, method).mockImplementation(record), | |
| ); | |
| return { | |
| lines, | |
| stop() { | |
| debug.log = originalLog; | |
| for (const spy of spies) spy.mockRestore(); | |
| }, | |
| }; | |
| } | |