Mocking `Effect.Service` with Dependencies in TypeScript's Effect Library
I'm having trouble mocking a service that has platform dependencies when using the new
Effect.ServiceEffect.Service pattern. Here's a minimal example:// GitService.ts
import { Effect } from 'effect'
import { Command } from '@effect/platform'
import { NodeContext } from '@effect/platform-node'
export class GitService extends Effect.Service<GitService>()('GitService', {
effect: Effect.gen(function* () {
const getCurrentBranch = Effect.fn('getCurrentBranch')(function* () {
// This uses CommandExecutor under the hood
return yield* Command.make("git", "branch", "--show-current").pipe(
Command.string,
Effect.map(s => s.trim())
)
})
return { getCurrentBranch }
}),
dependencies: [NodeContext.layer]
}) {}
// TodoService.ts
import { Effect } from 'effect'
import { GitService } from './GitService'
export class TodoService extends Effect.Service<TodoService>()('TodoService', {
effect: Effect.gen(function* () {
const git = yield* GitService
const createTodo = Effect.fn('createTodo')(function* () {
const branch = yield* git.getCurrentBranch()
return `Created todo for branch: ${branch}`
})
return { createTodo }
}),
dependencies: [GitService.Default]
}) {}// GitService.ts
import { Effect } from 'effect'
import { Command } from '@effect/platform'
import { NodeContext } from '@effect/platform-node'
export class GitService extends Effect.Service<GitService>()('GitService', {
effect: Effect.gen(function* () {
const getCurrentBranch = Effect.fn('getCurrentBranch')(function* () {
// This uses CommandExecutor under the hood
return yield* Command.make("git", "branch", "--show-current").pipe(
Command.string,
Effect.map(s => s.trim())
)
})
return { getCurrentBranch }
}),
dependencies: [NodeContext.layer]
}) {}
// TodoService.ts
import { Effect } from 'effect'
import { GitService } from './GitService'
export class TodoService extends Effect.Service<TodoService>()('TodoService', {
effect: Effect.gen(function* () {
const git = yield* GitService
const createTodo = Effect.fn('createTodo')(function* () {
const branch = yield* git.getCurrentBranch()
return `Created todo for branch: ${branch}`
})
return { createTodo }
}),
dependencies: [GitService.Default]
}) {}