Issue with `LayerMap` Rebuilding Inner Layer in Tests

Hi! I'm trying to use LayerMap and I have an issue in tests where the inner layer gets rebuilt every time it's used while I would not expected this to be the case.
Here's an example to demo this and the difference with when using the inner Layer directly (playground here):
import { Context, Effect, Layer, LayerMap, pipe } from "effect"

const buildFoo = (): Context.Tag.Service<Api> => {
  console.log("building foo")
  return { foo: () => "foo" }
}

class Api extends Effect.Tag("Api")<Api, { foo: () => string }>() {
  static layer = () => Layer.succeed(Api, buildFoo())
}

class ApiMap extends LayerMap.Service<ApiMap>()(
  "ApiMap",
  {
    lookup: (_: "foo" | "bar") => Layer.succeed(Api, buildFoo()),
    idleTimeToLive: "1 hour",
    dependencies: []
  }
) {
  static layerMock = () =>
    Layer.scoped(
      this,
      LayerMap.make(() => Api.layer())
    )
}

const effect = Effect.provide(Api.foo(), ApiMap.get("foo"))

console.log("with Map")

pipe(Effect.all([effect, effect, effect]), Effect.provide(ApiMap.layerMock()), Effect.runSync) // -> "building foo" gets logged 3 times

console.log("without Map")

pipe(Effect.all([Api.foo(), Api.foo(), Api.foo()]), Effect.provide(Api.layer()), Effect.runSync) // -> "building foo" gets logged only once

I insist that this is in the case of tests where I'd like to customize the behavior of the inner layer, hence the lazy functions everywhere.
Am I missing something? I did not find something else than Layer.make to build the layer in layerMock and maybe the issue comes from this
Was this page helpful?