Effect CommunityEC
Effect Community3y ago
2 replies
KnirB

Writing Tests for Async Code using Effect

Hey everyone, I'm trying to understand how to best write tests for async code using effect. Wondering if anyone has some input

I have a simple db-call for a card that looks like this:

  create = (
    createCardInput: CreateCardInput
  ): Effect.Effect<undefined, Error, Card> =>
    Effect.tryPromise({
      try: () =>
        this.prisma.card.create({
          data: {
            ...createCardInput
          }
        }),
      catch: (e) => new Error('Error creating new card:' + JSON.stringify(e))
    })


In my test I want to test the case where the database call fails, but can't seem to the the error type from the function when using Effect.runPromise

it('should return an effect with an error if the database call fails', async () => {
  let mockCreate = jest.fn().mockImplementationOnce(async () => {
    return Promise.reject('Something went wrong')
  })

  prisma.card.create = mockCreate
  // try/catch or .then/.catch returns error with type any
  await Effect.runPromise(
    service.create({
      front: 'frontText',
      back: 'backText'
    })
  )
})


I am probably misunderstanding something here, but wouldn't a return type like an Either be more suitable for something like this? Or do we just have to accept that once we try to get values out of effects, we don't have typed errors anymore?
Was this page helpful?