test: stabilize json runtime captures (#52428) (thanks @karanuppal)

This commit is contained in:
Peter Steinberger
2026-03-22 23:48:15 +00:00
parent a2999c6cfb
commit aaa6068c08
21 changed files with 103 additions and 11 deletions

View File

@@ -28,8 +28,12 @@ vi.mock("../../runtime.js", () => ({
defaultRuntime: {
log: (message: string) => runtimeLogs.push(message),
error: (message: string) => runtimeErrors.push(message),
writeJson: (value: unknown, space = 2) =>
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined)),
writeStdout: (value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
},
writeJson: (value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
},
exit: (code: number) => {
throw new Error(`__exit__:${code}`);
},

View File

@@ -129,6 +129,12 @@ const runtimeLogs: string[] = [];
vi.mock("../../runtime.js", () => ({
defaultRuntime: {
log: (message: string) => runtimeLogs.push(message),
writeStdout: (value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
},
writeJson: (value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space));
},
error: vi.fn(),
exit: vi.fn(),
},

View File

@@ -8,6 +8,8 @@ export const runtimeLogs: string[] = [];
type LifecycleRuntimeHarness = OutputRuntimeEnv & {
error: MockFn<OutputRuntimeEnv["error"]>;
exit: MockFn<OutputRuntimeEnv["exit"]>;
writeStdout: MockFn<(value: string) => void>;
writeJson: MockFn<(value: unknown, space?: number) => void>;
};
type LifecycleServiceHarness = GatewayService & {
@@ -24,13 +26,13 @@ export const defaultRuntime: LifecycleRuntimeHarness = {
log: (...args: unknown[]) => {
runtimeLogs.push(args.map((arg) => String(arg)).join(" "));
},
writeStdout: (value: string) => {
runtimeLogs.push(value);
},
writeJson: (value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
},
error: vi.fn(),
writeStdout: vi.fn((value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
}),
exit: vi.fn((code: number) => {
throw new Error(`__exit__:${code}`);
}),