m-radzikowski / aws-sdk-client-mock

AWS JavaScript SDK v3 mocks for easy unit testing. πŸ–‹οΈ Typed πŸ”¬ Tested πŸ“„ Documented πŸ› οΈ Maintained
https://m-radzikowski.github.io/aws-sdk-client-mock/
MIT License
811 stars 40 forks source link

"Not Assignable to type" error trying to mock S3 GetObject for Binary File. #227

Open gbbayareametro opened 5 months ago

gbbayareametro commented 5 months ago

Checklist

Bug description

Mocking getting an excel workbook from an S3 bucket. The functionality I'm testing is going to be in a NodeJS lambda.

Here's the code I'm testing:

export const getFile = async (
  bucket: string,
  object: string
): Promise<SdkStream<IncomingMessage>> => {
  const logger = instance;
  const client = new S3Client({}) as NodeJsClient<S3Client>;

  const body: SdkStream<IncomingMessage> = (
    await client.send(new GetObjectCommand({ Key: "", Bucket: "" }))
  ).Body!;
  return body;
};

Here's my test (No real tests yet because I'm blocked trying to get the mock going):

 test("It gets the file successfully from S3", async () => {
    const readStream = createReadStream("./mockListings.xslx");
    const stream = sdkStreamMixin(readStream);
    const s3ClientMock = mockClient(S3Client);
    s3ClientMock.on(GetObjectCommand).resolves({ Body: stream });
  });

I'm getting this error:

Type 'SdkStream<ReadableStream | Blob> | SdkStream<Readable>' is not assignable to type 'StreamingBlobPayloadOutputTypes | undefined'.
  Type 'ReadableStream & SdkStreamMixin' is not assignable to type 'StreamingBlobPayloadOutputTypes | undefined'.
    Type 'ReadableStream & SdkStreamMixin' is not assignable to type 'BrowserRuntimeStreamingBlobPayloadOutputTypes'.
      Type 'ReadableStream & SdkStreamMixin' is missing the following properties from type 'Blob': size, type, arrayBuffer, slice, and 2 more.

Here's the output of 'npm ls' indicating that I only have the latest version of @smithy/types installed:

@aws-sdk/client-s3@3.598.0
β”‚ β”œβ”€β”¬ @aws-sdk/client-sso-oidc@3.598.0
β”‚ β”‚ β”œβ”€β”€ @smithy/types@3.1.0 deduped
β”‚ β”‚ └─┬ @smithy/util-middleware@3.0.1
β”‚ β”‚   └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/client-sts@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/core@3.598.0
β”‚ β”‚ β”œβ”€β”¬ @smithy/signature-v4@3.1.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-node@3.598.0
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-env@3.598.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-http@3.598.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-ini@3.598.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-process@3.598.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-sso@3.598.0
β”‚ β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/client-sso@3.598.0
β”‚ β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/token-providers@3.598.0
β”‚ β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @aws-sdk/credential-provider-web-identity@3.598.0
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @smithy/credential-provider-imds@3.1.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @smithy/property-provider@3.1.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”œβ”€β”¬ @smithy/shared-ini-file-loader@3.1.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-bucket-endpoint@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-expect-continue@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-flexible-checksums@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-host-header@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-location-constraint@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-logger@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-recursion-detection@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-sdk-s3@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-signing@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-ssec@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/middleware-user-agent@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/region-config-resolver@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/signature-v4-multi-region@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/types@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/util-endpoints@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/util-user-agent-browser@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/util-user-agent-node@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @aws-sdk/xml-builder@3.598.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/config-resolver@3.0.2
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/core@2.2.2
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/eventstream-serde-browser@3.0.2
β”‚ β”‚ β”œβ”€β”¬ @smithy/eventstream-serde-universal@3.0.2
β”‚ β”‚ β”‚ β”œβ”€β”¬ @smithy/eventstream-codec@3.1.0
β”‚ β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/eventstream-serde-config-resolver@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/eventstream-serde-node@3.0.2
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/fetch-http-handler@3.0.3
β”‚ β”‚ β”œβ”€β”¬ @smithy/querystring-builder@3.0.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/hash-blob-browser@3.1.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/hash-node@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/hash-stream-node@3.1.0
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/invalid-dependency@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/md5-js@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/middleware-content-length@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/middleware-endpoint@3.0.2
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/middleware-retry@3.0.5
β”‚ β”‚ β”œβ”€β”¬ @smithy/service-error-classification@3.0.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/middleware-serde@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/middleware-stack@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/node-config-provider@3.1.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/node-http-handler@3.0.1
β”‚ β”‚ β”œβ”€β”¬ @smithy/abort-controller@3.0.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/protocol-http@4.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/smithy-client@3.1.3
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”€ @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/url-parser@3.0.1
β”‚ β”‚ β”œβ”€β”¬ @smithy/querystring-parser@3.0.1
β”‚ β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/util-defaults-mode-browser@3.0.5
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/util-defaults-mode-node@3.0.5
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/util-endpoints@2.0.2
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ β”œβ”€β”¬ @smithy/util-retry@3.0.1
β”‚ β”‚ └── @smithy/types@3.1.0 deduped
β”‚ └─┬ @smithy/util-waiter@3.0.1
β”‚   └── @smithy/types@3.1.0 deduped
β”œβ”€β”€ @smithy/types@3.1.0
└─┬ @smithy/util-stream@3.0.3
  └── @smithy/types@3.1.0 deduped

Any help resolving the error would be appreciated.

Reproduction

Environment

m-radzikowski commented 2 months ago

I don't see any type errors with your snippets and latest dependency versions:

    "@aws-sdk/client-s3": "3.654.0",
    "@smithy/util-stream": "3.1.8",
    "@smithy/types": "3.4.2",
    "aws-sdk-client-mock": "4.0.1",
    "typescript": "5.6.2"

Please update dependencies and if problem remains, please create a small repo with proper reproduction. It should be possible to run:

npm install
tsc

to see the error.