Skip to content

Layer.mock won't accept partials if unmocked methods/properties do not return Effect (also prevents use with Service) #5833

@dmeehan1968

Description

@dmeehan1968

What version of Effect is running?

3.19.7

What steps can reproduce the bug?

  class Test extends Context.Tag('Test')<
      Test,
      {
        foo: () => void,
        bar: () => void,
      }
  >() {
  }

  Layer.mock(Test, {
    foo: () => {},
  })
// ^ TS2345: Argument of type { foo: () => void; } is not assignable to parameter of type { foo: () => void; bar: () => void; }
// Property bar is missing in type { foo: () => void; } but required in type { foo: () => void; bar: () => void; }

What is the expected behavior?

Should work as shown above.

What do you see instead?

As above

Additional information

It works as expected if you mock all methods that don't return Effect.

This should also be the case for properties that are not effects (even though that's less likely to be required) but the Layer.mock example shows a property which is an Effect.

I was trying to create a Layer mock for @effect/platform's Path, which doesn't have a layerNoop helper. For example:

    const mockPath = Layer.mock(Path.Path, {
      resolve: (...args: string[]) => [...args].join(sep),
    } as Path.Path)

The cast is necessary to get around Layer.mock not accepting the partial as all methods in Path are plain functions.

### Effect.Service

This also means that Layer.mock doesn't work with Effect.Service unless the _tag is specified, along with any none effect properties. From the docs:

const MyServiceTest = Layer.mock(MyService, {
  _tag: "MyService",                       // comment this out to see error
  two: () => Effect.succeed(2)
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions