effect.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { test, type TestOptions } from "bun:test"
  2. import { Cause, Effect, Exit, Layer } from "effect"
  3. import type * as Scope from "effect/Scope"
  4. import * as TestClock from "effect/testing/TestClock"
  5. import * as TestConsole from "effect/testing/TestConsole"
  6. type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)
  7. const body = <A, E, R>(value: Body<A, E, R>) => Effect.suspend(() => (typeof value === "function" ? value() : value))
  8. const run = <A, E, R, E2>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2>) =>
  9. Effect.gen(function* () {
  10. const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(layer), Effect.exit)
  11. if (Exit.isFailure(exit)) {
  12. for (const err of Cause.prettyErrors(exit.cause)) {
  13. yield* Effect.logError(err)
  14. }
  15. }
  16. return yield* exit
  17. }).pipe(Effect.runPromise)
  18. const make = <R, E>(testLayer: Layer.Layer<R, E>, liveLayer: Layer.Layer<R, E>) => {
  19. const effect = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  20. test(name, () => run(value, testLayer), opts)
  21. effect.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  22. test.only(name, () => run(value, testLayer), opts)
  23. effect.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  24. test.skip(name, () => run(value, testLayer), opts)
  25. const live = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  26. test(name, () => run(value, liveLayer), opts)
  27. live.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  28. test.only(name, () => run(value, liveLayer), opts)
  29. live.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  30. test.skip(name, () => run(value, liveLayer), opts)
  31. return { effect, live }
  32. }
  33. // Test environment with TestClock and TestConsole
  34. const testEnv = Layer.mergeAll(TestConsole.layer, TestClock.layer())
  35. // Live environment - uses real clock, but keeps TestConsole for output capture
  36. const liveEnv = TestConsole.layer
  37. export const it = make(testEnv, liveEnv)
  38. export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
  39. make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))