<I, S extends object>(service: Context.Key<I, S>): (
implementation: PartialEffectful<S>
) => Layer<I>
<I, S extends object>(
service: Context.Key<I, S>,
implementation: Types.NoInfer<PartialEffectful<S>>
): Layer<I>Creates a mock layer for testing purposes. You can provide a partial
implementation of the service. Any missing members that are Effects,
Streams, Channels, or functions returning them will fail with an
unimplemented defect when used.
Details
Missing members are represented by a value that can be used as an Effect,
Stream, Channel, or as a function returning an Effect. This lets the
mock preserve the shape of common service methods while still failing loudly
when an unimplemented member is exercised.
Example (Mocking services for tests)
import { Context, Effect, Layer } from "effect"
class UserService extends Context.Service<UserService, {
readonly config: { apiUrl: string }
readonly getUser: (
id: string
) => Effect.Effect<{ id: string; name: string }, Error>
readonly deleteUser: (id: string) => Effect.Effect<void, Error>
readonly updateUser: (
id: string,
data: object
) => Effect.Effect<{ id: string; name: string }, Error>
}>()("UserService") {}
// Create a partial mock - only implement what you need for testing
const testUserLayer = Layer.mock(UserService, {
config: { apiUrl: "https://test-api.com" }, // Required - non-Effect property
getUser: (id: string) => Effect.succeed({ id, name: "Test User" }) // Mock implementation
// deleteUser and updateUser are omitted - will throw UnimplementedError if called
})
// Use in tests
const testProgram = Effect.gen(function*() {
const userService = yield* UserService
// This works - we provided an implementation
const user = yield* userService.getUser("123")
console.log(user.name) // "Test User"
// This would throw - we didn't implement deleteUser
// yield* userService.deleteUser("123") // UnimplementedError
}).pipe(
Effect.provide(testUserLayer)
)export const const mock: {
<I, S extends object>(
service: Context.Key<I, S>
): (
implementation: PartialEffectful<S>
) => Layer<I>
<I, S extends object>(
service: Context.Key<I, S>,
implementation: Types.NoInfer<
PartialEffectful<S>
>
): Layer<I>
}
Creates a mock layer for testing purposes. You can provide a partial
implementation of the service. Any missing members that are Effects,
Streams, Channels, or functions returning them will fail with an
unimplemented defect when used.
Details
Missing members are represented by a value that can be used as an Effect,
Stream, Channel, or as a function returning an Effect. This lets the
mock preserve the shape of common service methods while still failing loudly
when an unimplemented member is exercised.
Example (Mocking services for tests)
import { Context, Effect, Layer } from "effect"
class UserService extends Context.Service<UserService, {
readonly config: { apiUrl: string }
readonly getUser: (
id: string
) => Effect.Effect<{ id: string; name: string }, Error>
readonly deleteUser: (id: string) => Effect.Effect<void, Error>
readonly updateUser: (
id: string,
data: object
) => Effect.Effect<{ id: string; name: string }, Error>
}>()("UserService") {}
// Create a partial mock - only implement what you need for testing
const testUserLayer = Layer.mock(UserService, {
config: { apiUrl: "https://test-api.com" }, // Required - non-Effect property
getUser: (id: string) => Effect.succeed({ id, name: "Test User" }) // Mock implementation
// deleteUser and updateUser are omitted - will throw UnimplementedError if called
})
// Use in tests
const testProgram = Effect.gen(function*() {
const userService = yield* UserService
// This works - we provided an implementation
const user = yield* userService.getUser("123")
console.log(user.name) // "Test User"
// This would throw - we didn't implement deleteUser
// yield* userService.deleteUser("123") // UnimplementedError
}).pipe(
Effect.provide(testUserLayer)
)
mock: {
<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>I, function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>S extends object>(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>I, function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>S>): (implementation: PartialEffectful<S>implementation: type PartialEffectful<A extends object> =
{
[K in keyof ({
[K in keyof A as any]?: A[K] | undefined
} & { [K in keyof A as any]: A[K] })]: ({
[K in keyof A as any]?: A[K] | undefined
} & { [K in keyof A as any]: A[K] })[K]
} extends infer B
? B
: never
A utility type for creating partial mocks of services in testing.
When to use
Use to type partial test service implementations where only exercised
effectful members are stubbed.
Details
This type makes Effect, Stream, and Channel values and functions
returning them optional, while keeping non-effectful properties required.
This allows you to provide only the methods you need to test while leaving
others unimplemented.
PartialEffectful<function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>S>) => interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>): (implementation: PartialEffectful<S>) => Layer<I>I>
<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>I, function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>S extends object>(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>I, function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>S>, implementation: Types.NoInfer<PartialEffectful<S>>implementation: import TypesTypes.type NoInfer<A> = [A][A extends any
? 0
: never]
Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<type PartialEffectful<A extends object> =
{
[K in keyof ({
[K in keyof A as any]?: A[K] | undefined
} & { [K in keyof A as any]: A[K] })]: ({
[K in keyof A as any]?: A[K] | undefined
} & { [K in keyof A as any]: A[K] })[K]
} extends infer B
? B
: never
A utility type for creating partial mocks of services in testing.
When to use
Use to type partial test service implementations where only exercised
effectful members are stubbed.
Details
This type makes Effect, Stream, and Channel values and functions
returning them optional, while keeping non-effectful properties required.
This allows you to provide only the methods you need to test while leaving
others unimplemented.
PartialEffectful<function (type parameter) S in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>S>>): interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) I in <I, S extends object>(service: Context.Key<I, S>, implementation: Types.NoInfer<PartialEffectful<S>>): Layer<I>I>
} = function() {
if (function (local var) arguments: IArgumentsarguments.IArguments.length: numberlength === 1) {
return (implementation: anyimplementation: any) => const mockImpl: <I, S extends object>(
service: Context.Key<I, S>,
implementation: PartialEffectful<S>
) => Layer<I>
mockImpl(function (local var) arguments: IArgumentsarguments[0], implementation: anyimplementation)
}
return const mockImpl: <I, S extends object>(
service: Context.Key<I, S>,
implementation: PartialEffectful<S>
) => Layer<I>
mockImpl(function (local var) arguments: IArgumentsarguments[0], function (local var) arguments: IArgumentsarguments[1])
} as any