// @NOTE Node < 24 has no AsyncDisposableStack. This installs nothing when the // runtime provides it. import 'core-js/es/async-disposable-stack/index.js' /** * Owns the resources a fixture's `create()` acquires until setup succeeds. * * Register each resource right after acquiring it. Disposal runs most recent * first, so register in the reverse of the order the fixture should close * them. Once setup is done, hand the stack returned by `move()` (a plain * {@link AsyncDisposableStack}) to the fixture, whose `close()` disposes it. * If setup throws first, `await using` disposes what was registered. Errors * thrown while doing so are dropped, so the caller gets the setup error itself * rather than a `SuppressedError`. * * @example * await using stack = new SetupStack() * const plc = await TestPlc.create({}) * stack.use(plc) * const pds = await TestPds.create({ didPlcUrl: plc.url }) * stack.use(pds) * return new TestNetworkNoAppView(plc, pds, stack.move()) */ export class SetupStack extends AsyncDisposableStack implements AsyncDisposable { override async [Symbol.asyncDispose](): Promise { try { await this.disposeAsync() } catch { // @NOTE dropped, so that the setup error is the one rethrown: vitest // reports a SuppressedError without the errors it wraps // (https://github.com/vitest-dev/vitest/issues/12130) } } } /** * Builds the stack a fixture owns when its constructor is called without the * one `create()` hands over. `register` must add the fixture's resources in * the same order `create()` does. */ export function disposableStack( register: (stack: AsyncDisposableStack) => void, ): AsyncDisposableStack { const stack = new AsyncDisposableStack() register(stack) return stack }