Tear down what a test opens in t.after so a failing assertion ends the run

This commit is contained in:
2026-08-30 21:35:09 +02:00
parent fa720e244a
commit 427565ed3e
8 changed files with 317 additions and 415 deletions
+21
View File
@@ -0,0 +1,21 @@
import type { CloseOptions } from '../src/session-options.ts';
import type { Server, Socket } from 'node:net';
import type { TestContext } from 'node:test';
type Closable = { close: (options: CloseOptions) => Promise<unknown> };
/** Aborted: a cleanup that drained would hang the run in exactly the case it exists for. */
export function closeAfter(t: TestContext, closable: Closable): void {
t.after(() => closable.close({ signal: AbortSignal.abort() }));
}
/** A listener with a socket still on it never closes, so the sockets go first. */
export function endListenerAfter(t: TestContext, listener: Server, sockets: Socket[]): void {
t.after(async () => {
for (const sock of sockets) {
sock.destroy();
}
await new Promise<void>(resolve => { listener.close(() => { resolve(); }); });
});
}