|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
| 4 | +import { S3ServiceException } from "@aws-sdk/client-s3"; |
4 | 5 | import { describe, it, expect, vi } from "vitest"; |
5 | 6 |
|
6 | | -const send = vi.fn(); |
| 7 | +const paginateListBuckets = vi.fn().mockImplementation(async function* () { |
| 8 | + yield { |
| 9 | + Buckets: [{ Name: "amzn-s3-demo-bucket" }], |
| 10 | + Owner: { DisplayName: "bar" }, |
| 11 | + }; |
| 12 | +}); |
7 | 13 |
|
8 | 14 | vi.doMock("@aws-sdk/client-s3", async () => { |
9 | 15 | const actual = await vi.importActual("@aws-sdk/client-s3"); |
10 | 16 | return { |
11 | 17 | ...actual, |
12 | | - S3Client: class { |
13 | | - send = send; |
14 | | - }, |
| 18 | + paginateListBuckets, |
15 | 19 | }; |
16 | 20 | }); |
17 | 21 |
|
18 | 22 | const { main } = await import("../actions/list-buckets.js"); |
19 | 23 |
|
20 | 24 | describe("list-buckets", () => { |
21 | 25 | it("should log the response from the service", async () => { |
22 | | - send.mockResolvedValue({ |
23 | | - Buckets: [{ Name: "foo" }], |
24 | | - Owner: { DisplayName: "bar" }, |
25 | | - }); |
26 | | - |
27 | 26 | const spy = vi.spyOn(console, "log"); |
28 | 27 |
|
29 | 28 | await main(); |
30 | 29 |
|
31 | 30 | expect(spy).toHaveBeenNthCalledWith(1, "bar owns 1 bucket:"); |
32 | | - expect(spy).toHaveBeenNthCalledWith(2, " • foo"); |
| 31 | + expect(spy).toHaveBeenNthCalledWith(2, " • amzn-s3-demo-bucket"); |
33 | 32 | }); |
34 | 33 |
|
35 | | - it("should log errors", async () => { |
36 | | - send.mockRejectedValue("foo"); |
| 34 | + it("should indicate a failure came from S3 when the error isn't generic", async () => { |
| 35 | + const error = new S3ServiceException("Some S3 service exception."); |
| 36 | + error.name = "ServiceException"; |
| 37 | + const bucketName = "amzn-s3-demo-bucket"; |
| 38 | + paginateListBuckets.mockImplementationOnce( |
| 39 | + // eslint-disable-next-line require-yield |
| 40 | + async function* () { |
| 41 | + throw error; |
| 42 | + }, |
| 43 | + ); |
37 | 44 |
|
38 | 45 | const spy = vi.spyOn(console, "error"); |
39 | 46 |
|
40 | | - await main(); |
| 47 | + await main({ bucketName, keys: ["foo"] }); |
| 48 | + |
| 49 | + expect(spy).toHaveBeenCalledWith( |
| 50 | + `Error from S3 while listing buckets. ${error.name}: ${error.message}`, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should throw errors that are not S3 specific", async () => { |
| 55 | + const bucketName = "amzn-s3-demo-bucket"; |
| 56 | + paginateListBuckets.mockImplementationOnce( |
| 57 | + // eslint-disable-next-line require-yield |
| 58 | + async function* () { |
| 59 | + throw new Error(); |
| 60 | + }, |
| 61 | + ); |
41 | 62 |
|
42 | | - expect(spy).toHaveBeenCalledWith("foo"); |
| 63 | + await expect(() => |
| 64 | + main({ bucketName, keys: ["foo"] }), |
| 65 | + ).rejects.toBeTruthy(); |
43 | 66 | }); |
44 | 67 | }); |
0 commit comments