effect.ts 937 B

12345678910111213141516171819202122232425262728
  1. import { test } from "bun:test"
  2. import { Cause, Effect, Exit, Layer } from "effect"
  3. import type { Scope } from "effect/Scope"
  4. import { TestClock, TestConsole } from "effect/testing"
  5. type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)
  6. const layer = Layer.mergeAll(TestConsole.layer, TestClock.layer())
  7. const effect = <A, E>(name: string, body: Body<A, E, Scope>, options?: Parameters<typeof test>[2]) =>
  8. test(
  9. name,
  10. () =>
  11. Effect.gen(function* () {
  12. const exit = yield* Effect.suspend(() => (typeof body === "function" ? body() : body)).pipe(
  13. Effect.scoped,
  14. Effect.provide(layer),
  15. Effect.exit,
  16. )
  17. if (Exit.isFailure(exit)) {
  18. yield* Effect.forEach(Cause.prettyErrors(exit.cause), Effect.logError, { discard: true })
  19. }
  20. return yield* exit
  21. }).pipe(Effect.runPromise),
  22. options,
  23. )
  24. export const it = { effect }