Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK Error types (ResourceNotFoundError etc) should be exposed as constructable values to allow for unit test mocks #110

Open
dmeehan1968 opened this issue Feb 24, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@dmeehan1968
Copy link

Errors are exposed only as types, which means that its not easy to create mocks for the AWS services. Example for jest/jest-mock-extended:

    this.scheduler = mock<Context.Tag.Service<SchedulerService>>()

    this.scheduler.updateSchedule.mockReturnValue(
      Effect.fail({
        ...new ResourceNotFoundException({
          message: 'not found',
          Message: 'Not Found',
          $metadata: {},
        }),
        _tag: 'ResourceNotFoundException',
      }),
    )

Usability could be vastly improved by providing a constructor for the error, with some possible optimisation to provide sensible defaults, eg.:

this.scheduler.updateSchedule.mockReturnValue(
  Effect.fail(new ResourceNotFoundError({ Message: 'Not Found' }))
)

One could apply a kludge such as:

this.scheduler.updateSchedule.mockReturnValue(
      Effect.fail({
        _tag: 'ResourceNotFoundException',
      } as ResourceNotFoundError),
)

although possibly ugly and error prone should the effect-aws library change the constants.

Different mock frameworks will have their own behaviours, but at a fundamental level, the ability to more readily construct error types would be useful.

As an aside, the service type could be exported within the service namespace, so we can avoid the Context.Tag.Service<xxx> and have something like Service.Type. I think that effect-aws could improve the exports, to better mirror those used within effect as a whole (Effect.Service exports Default as the primary layer for example).

@floydspace
Copy link
Owner

floydspace commented Feb 24, 2025

@dmeehan1968 good observations..

Example for jest/jest-mock-extended:

You can use a little more elegant mocking:

import { SchedulerService, ResourceNotFoundError } from "@effect-aws/client-scheduler";

// ...

    this.scheduler = mock<Context.Tag.Service<SchedulerService>>()

    this.scheduler.updateSchedule.mockReturnValue(
      Effect.fail(
        mock<ResourceNotFoundError>({
          _tag: "ResourceNotFoundException",
          message: "not found",
          Message: "Not Found",
        })
      ),
    )

the ResourceNotFoundError is a copy of ResourceNotFoundException (I know it is a bit wierd that it is renamed, but I though initially it would be better to make namings consistent, inspite what aws-sdk provides. But now I think it was a mistake...)

currently effect-aws copies the error and adds the _tag to it, I know it is not perfect, I'd rather keep the error instances original and add _tag in prototype: this gives benefit of using instanceof on errors as they stay native..

we can avoid the Context.Tag.Service and have something like Service.Type.

I also noticed inconviniece with that, agree, it is good to improve

I think that effect-aws could improve the exports, to better mirror those used within effect

Also agree, effect-aws was written before this service Default was introduced. in fact it is not using the Service data structure. Can be improved too..

@floydspace floydspace added the enhancement New feature or request label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants