DEV Community

Discussion on: Mocking third party classes in TypeScript

Collapse
 
timoschinkel profile image
Timo Schinkel

Thank you for your feedback. Your suggestion will result in the following error:

Conversion of type '{ putObject: jest.Mock; }' to type 'S3' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type '{ putObject: Mock; }' is missing the following properties from type 'S3': config, abortMultipartUpload, completeMultipartUpload, copyObject, and 98 more.ts(2352)

But this can be remedied by casting to unknown first:

const s3 = ({
  putObject: jest.fn().mockReturnValue({
    promise: jest.fn().mockResolvedValue({})
  })
} as unknown) as S3;
Enter fullscreen mode Exit fullscreen mode

I opted for the Pick solution as it actually changes the interface of the object to be more concise and more explicit about what part of the object is actually required.

I think this mostly will come down to preference.