From bb5361135cd5d7692a03faa1ea7dd68195bbdaff Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 19 Dec 2024 14:41:31 +0000 Subject: [PATCH] Fixing more test that rely on mocks. --- .../src/integrations/tests/aws-sdk.mock.ts | 76 ------------------- .../src/integrations/tests/dynamodb.spec.ts | 24 ++++-- .../server/src/integrations/tests/s3.spec.ts | 49 +++++++++++- 3 files changed, 66 insertions(+), 83 deletions(-) delete mode 100644 packages/server/src/integrations/tests/aws-sdk.mock.ts diff --git a/packages/server/src/integrations/tests/aws-sdk.mock.ts b/packages/server/src/integrations/tests/aws-sdk.mock.ts deleted file mode 100644 index 0422adfd3c..0000000000 --- a/packages/server/src/integrations/tests/aws-sdk.mock.ts +++ /dev/null @@ -1,76 +0,0 @@ -const response = (body: any, extra?: any) => () => ({ - promise: () => body, - ...extra, -}) - -class DocumentClient { - put = jest.fn(response({})) - query = jest.fn( - response({ - Items: [], - }) - ) - scan = jest.fn( - response({ - Items: [ - { - Name: "test", - }, - ], - }) - ) - get = jest.fn(response({})) - update = jest.fn(response({})) - delete = jest.fn(response({})) -} - -class S3 { - listObjects = jest.fn( - response({ - Contents: [], - }) - ) - createBucket = jest.fn( - response({ - Contents: {}, - }) - ) - deleteObjects = jest.fn( - response({ - Contents: {}, - }) - ) - getSignedUrl = jest.fn((operation, params) => { - return `http://example.com/${params.Bucket}/${params.Key}` - }) - headBucket = jest.fn( - response({ - Contents: {}, - }) - ) - upload = jest.fn( - response({ - Contents: {}, - }) - ) - getObject = jest.fn( - response( - { - Body: "", - }, - { - createReadStream: jest.fn().mockReturnValue("stream"), - } - ) - ) -} - -module.exports = { - DynamoDB: { - DocumentClient, - }, - S3, - config: { - update: jest.fn(), - }, -} diff --git a/packages/server/src/integrations/tests/dynamodb.spec.ts b/packages/server/src/integrations/tests/dynamodb.spec.ts index c992bc8bfd..75fb84ae60 100644 --- a/packages/server/src/integrations/tests/dynamodb.spec.ts +++ b/packages/server/src/integrations/tests/dynamodb.spec.ts @@ -1,4 +1,20 @@ -jest.mock("aws-sdk", () => require("./aws-sdk.mock")) +jest.mock("@aws-sdk/lib-dynamodb", () => ({ + DynamoDBDocument: { + from: jest.fn(() => ({ + update: jest.fn(), + put: jest.fn(), + query: jest.fn(() => ({ + Items: [], + })), + scan: jest.fn(() => ({ + Items: [], + })), + delete: jest.fn(), + get: jest.fn(), + })), + }, +})) +jest.mock("@aws-sdk/client-dynamodb") import { default as DynamoDBIntegration } from "../dynamodb" class TestConfiguration { @@ -57,11 +73,7 @@ describe("DynamoDB Integration", () => { TableName: tableName, IndexName: indexName, }) - expect(response).toEqual([ - { - Name: "test", - }, - ]) + expect(response).toEqual([]) }) it("calls the get method with the correct params", async () => { diff --git a/packages/server/src/integrations/tests/s3.spec.ts b/packages/server/src/integrations/tests/s3.spec.ts index abe8fb9cf1..678f15bf17 100644 --- a/packages/server/src/integrations/tests/s3.spec.ts +++ b/packages/server/src/integrations/tests/s3.spec.ts @@ -1,5 +1,52 @@ -jest.mock("aws-sdk", () => require("./aws-sdk.mock")) import { default as S3Integration } from "../s3" +jest.mock("@aws-sdk/client-s3", () => { + class S3Mock { + response(body: any, extra?: any) { + return () => ({ + promise: () => body, + ...extra, + }) + } + + listObjects = jest.fn( + this.response({ + Contents: [], + }) + ) + createBucket = jest.fn( + this.response({ + Contents: {}, + }) + ) + deleteObjects = jest.fn( + this.response({ + Contents: {}, + }) + ) + headBucket = jest.fn( + this.response({ + Contents: {}, + }) + ) + upload = jest.fn( + this.response({ + Contents: {}, + }) + ) + getObject = jest.fn( + this.response( + { + Body: "", + }, + { + createReadStream: jest.fn().mockReturnValue("stream"), + } + ) + ) + } + + return { S3: S3Mock } +}) class TestConfiguration { integration: any