|
| 1 | +import * as util from "node:util"; |
| 2 | +import { afterEach, beforeEach, vi } from "vitest"; |
| 3 | +import { normalizeString } from "./normalize"; |
| 4 | +import type { MockInstance } from "vitest"; |
| 5 | + |
| 6 | +/** |
| 7 | + * We use this module to mock console methods, and optionally |
| 8 | + * assert on the values they're called with in our tests. |
| 9 | + */ |
| 10 | + |
| 11 | +let debugSpy: MockInstance, |
| 12 | + logSpy: MockInstance, |
| 13 | + infoSpy: MockInstance, |
| 14 | + errorSpy: MockInstance, |
| 15 | + warnSpy: MockInstance; |
| 16 | + |
| 17 | +/** |
| 18 | + * An object containing the normalized output of each console method. |
| 19 | + * |
| 20 | + * We use `defineProperties` to add non enumerable methods to the object, |
| 21 | + * so they don't show up in test assertions that iterate over the object's keys. |
| 22 | + * i.e. `expect(std).toMatchInlineSnapshot('...')`; |
| 23 | + */ |
| 24 | +const std = Object.defineProperties( |
| 25 | + { debug: "", out: "", info: "", err: "", warn: "", getAndClearOut: () => "" }, |
| 26 | + { |
| 27 | + debug: { |
| 28 | + get: () => normalizeOutput(debugSpy), |
| 29 | + enumerable: true, |
| 30 | + }, |
| 31 | + out: { |
| 32 | + get: () => normalizeOutput(logSpy), |
| 33 | + enumerable: true, |
| 34 | + }, |
| 35 | + info: { |
| 36 | + get: () => normalizeOutput(infoSpy), |
| 37 | + enumerable: true, |
| 38 | + }, |
| 39 | + err: { |
| 40 | + get: () => normalizeOutput(errorSpy), |
| 41 | + enumerable: true, |
| 42 | + }, |
| 43 | + warn: { |
| 44 | + get: () => normalizeOutput(warnSpy), |
| 45 | + enumerable: true, |
| 46 | + }, |
| 47 | + /** |
| 48 | + * Return the content of the mocked stdout and clear the mock's history. |
| 49 | + * |
| 50 | + * Helpful for tests that need to assert on multiple sequential console outputs. |
| 51 | + */ |
| 52 | + getAndClearOut: { |
| 53 | + value: () => { |
| 54 | + const output = normalizeOutput(logSpy); |
| 55 | + logSpy.mockClear(); |
| 56 | + return output; |
| 57 | + }, |
| 58 | + enumerable: false, |
| 59 | + }, |
| 60 | + } |
| 61 | +); |
| 62 | + |
| 63 | +function normalizeOutput(spy: MockInstance, join = "\n"): string { |
| 64 | + return normalizeString(captureCalls(spy, join)); |
| 65 | +} |
| 66 | + |
| 67 | +function captureCalls(spy: MockInstance, join = "\n"): string { |
| 68 | + return spy.mock.calls |
| 69 | + .map((args: unknown[]) => util.format("%s", ...args)) |
| 70 | + .join(join); |
| 71 | +} |
| 72 | + |
| 73 | +export function mockConsoleMethods() { |
| 74 | + beforeEach(() => { |
| 75 | + debugSpy = vi.spyOn(console, "debug").mockImplementation(() => {}); |
| 76 | + logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 77 | + infoSpy = vi.spyOn(console, "info").mockImplementation(() => {}); |
| 78 | + errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 79 | + warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 80 | + }); |
| 81 | + afterEach(() => { |
| 82 | + debugSpy.mockRestore(); |
| 83 | + logSpy.mockRestore(); |
| 84 | + infoSpy.mockRestore(); |
| 85 | + errorSpy.mockRestore(); |
| 86 | + warnSpy.mockRestore(); |
| 87 | + }); |
| 88 | + return std; |
| 89 | +} |
0 commit comments