From aa7d145575c9bbcf817a1eed7d092e9063f5c9fc Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 27 Sep 2024 10:58:57 +1200 Subject: [PATCH] defer resource creation to suite creation --- packages/vitest/src/internal.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/vitest/src/internal.ts b/packages/vitest/src/internal.ts index 02c91ba808..70182350c4 100644 --- a/packages/vitest/src/internal.ts +++ b/packages/vitest/src/internal.ts @@ -103,7 +103,10 @@ const makeTester = ( export const layer = (layer_: Layer.Layer, memoMap?: Layer.MemoMap): { (f: (it: Vitest.Vitest.Methods) => void): void (name: string, f: (it: Vitest.Vitest.Methods) => void): void -} => { +} => +( + ...args: [name: string, f: (it: Vitest.Vitest.Methods) => void] | [f: (it: Vitest.Vitest.Methods) => void] +) => { memoMap = memoMap ?? Effect.runSync(Layer.makeMemoMap) const scope = Effect.runSync(Scope.make()) const runtimeEffect = Layer.toRuntimeWithMemoMap(layer_, memoMap).pipe( @@ -112,8 +115,6 @@ export const layer = (layer_: Layer.Layer, memoMap?: Layer.MemoMap): Effect.cached, Effect.runSync ) - V.beforeAll(() => runPromise()(Effect.asVoid(runtimeEffect))) - V.afterAll(() => runPromise()(Scope.close(scope, Exit.void))) const it: Vitest.Vitest.Methods = Object.assign(V.it, { effect: makeTester((effect) => @@ -150,14 +151,17 @@ export const layer = (layer_: Layer.Layer, memoMap?: Layer.MemoMap): } }) - return function( - ...args: [name: string, f: (it: Vitest.Vitest.Methods) => void] | [f: (it: Vitest.Vitest.Methods) => void] - ) { - if (args.length === 1) { - return args[0](it) - } - return V.describe(args[0], () => args[1](it)) + if (args.length === 1) { + V.beforeAll(() => runPromise()(Effect.asVoid(runtimeEffect))) + V.afterAll(() => runPromise()(Scope.close(scope, Exit.void))) + return args[0](it) } + + return V.describe(args[0], () => { + V.beforeAll(() => runPromise()(Effect.asVoid(runtimeEffect))) + V.afterAll(() => runPromise()(Scope.close(scope, Exit.void))) + return args[1](it) + }) } /** @internal */