@mbc-cqrs-serverless/core
Version:
CQRS and event base core
336 lines • 9.49 kB
JavaScript
"use strict";
/**
* AWS Mock Manager
*
* Provides standardized setup and teardown for AWS SDK mock clients
* in integration tests. This eliminates repetitive mock configuration
* across test files and ensures consistent mock behavior.
*
* Usage:
* import { AWSMockManager, createDynamoDBMock } from './utilities/aws-mock-manager'
*
* // Option 1: Use individual mock creators
* const { mock, client, reset, restore } = createDynamoDBMock()
*
* // Option 2: Use the manager for multiple services
* const manager = new AWSMockManager()
* const dynamoDB = manager.getDynamoDB()
* const s3 = manager.getS3()
*
* beforeEach(() => manager.resetAll())
* afterAll(() => manager.restoreAll())
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AWSMockManager = void 0;
exports.createDynamoDBMock = createDynamoDBMock;
exports.createS3Mock = createS3Mock;
exports.createSQSMock = createSQSMock;
exports.createSNSMock = createSNSMock;
exports.createSFNMock = createSFNMock;
exports.createSESMock = createSESMock;
exports.setupAWSMocks = setupAWSMocks;
exports.createTestMocks = createTestMocks;
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
const client_s3_1 = require("@aws-sdk/client-s3");
const client_sesv2_1 = require("@aws-sdk/client-sesv2");
const client_sfn_1 = require("@aws-sdk/client-sfn");
const client_sns_1 = require("@aws-sdk/client-sns");
const client_sqs_1 = require("@aws-sdk/client-sqs");
const aws_sdk_client_mock_1 = require("aws-sdk-client-mock");
/**
* Default AWS region for test clients
*/
const DEFAULT_REGION = 'ap-northeast-1';
// ============================================================================
// Individual Mock Creators
// ============================================================================
/**
* Creates a mocked DynamoDB client
*/
function createDynamoDBMock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_dynamodb_1.DynamoDBClient);
const client = new client_dynamodb_1.DynamoDBClient({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
/**
* Creates a mocked S3 client
*/
function createS3Mock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_s3_1.S3Client);
const client = new client_s3_1.S3Client({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
/**
* Creates a mocked SQS client
*/
function createSQSMock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_sqs_1.SQSClient);
const client = new client_sqs_1.SQSClient({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
/**
* Creates a mocked SNS client
*/
function createSNSMock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_sns_1.SNSClient);
const client = new client_sns_1.SNSClient({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
/**
* Creates a mocked Step Functions client
*/
function createSFNMock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_sfn_1.SFNClient);
const client = new client_sfn_1.SFNClient({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
/**
* Creates a mocked SES v2 client
*/
function createSESMock(options = {}) {
const mock = (0, aws_sdk_client_mock_1.mockClient)(client_sesv2_1.SESv2Client);
const client = new client_sesv2_1.SESv2Client({
region: options.region || DEFAULT_REGION,
maxAttempts: options.maxAttempts,
retryMode: options.retryMode,
});
return {
mock,
client,
reset: () => mock.reset(),
restore: () => {
mock.restore();
client.destroy();
},
};
}
// ============================================================================
// AWS Mock Manager Class
// ============================================================================
/**
* Manages multiple AWS mock clients for test suites
*/
class AWSMockManager {
constructor(options = {}) {
this.options = options;
}
/**
* Gets or creates the DynamoDB mock
*/
getDynamoDB() {
if (!this.dynamoDB) {
this.dynamoDB = createDynamoDBMock(this.options);
}
return this.dynamoDB;
}
/**
* Gets or creates the S3 mock
*/
getS3() {
if (!this.s3) {
this.s3 = createS3Mock(this.options);
}
return this.s3;
}
/**
* Gets or creates the SQS mock
*/
getSQS() {
if (!this.sqs) {
this.sqs = createSQSMock(this.options);
}
return this.sqs;
}
/**
* Gets or creates the SNS mock
*/
getSNS() {
if (!this.sns) {
this.sns = createSNSMock(this.options);
}
return this.sns;
}
/**
* Gets or creates the Step Functions mock
*/
getSFN() {
if (!this.sfn) {
this.sfn = createSFNMock(this.options);
}
return this.sfn;
}
/**
* Gets or creates the SES mock
*/
getSES() {
if (!this.ses) {
this.ses = createSESMock(this.options);
}
return this.ses;
}
/**
* Resets all active mocks
*/
resetAll() {
this.dynamoDB?.reset();
this.s3?.reset();
this.sqs?.reset();
this.sns?.reset();
this.sfn?.reset();
this.ses?.reset();
}
/**
* Restores all mocks and destroys clients
*/
restoreAll() {
this.dynamoDB?.restore();
this.s3?.restore();
this.sqs?.restore();
this.sns?.restore();
this.sfn?.restore();
this.ses?.restore();
this.dynamoDB = undefined;
this.s3 = undefined;
this.sqs = undefined;
this.sns = undefined;
this.sfn = undefined;
this.ses = undefined;
}
}
exports.AWSMockManager = AWSMockManager;
// ============================================================================
// Test Setup Helpers
// ============================================================================
/**
* Creates a standard test setup with beforeEach/afterAll hooks
* for use with Jest describe blocks
*/
function setupAWSMocks(services, options = {}) {
const manager = new AWSMockManager(options);
// Initialize requested services
services.forEach((service) => {
switch (service) {
case 'dynamodb':
manager.getDynamoDB();
break;
case 's3':
manager.getS3();
break;
case 'sqs':
manager.getSQS();
break;
case 'sns':
manager.getSNS();
break;
case 'sfn':
manager.getSFN();
break;
case 'ses':
manager.getSES();
break;
}
});
return {
manager,
beforeEachHook: () => manager.resetAll(),
afterAllHook: () => manager.restoreAll(),
};
}
/**
* Quick setup function that returns individual mocks for common patterns
*/
function createTestMocks(services, options = {}) {
const mocks = {};
services.forEach((service) => {
switch (service) {
case 'dynamodb':
mocks.dynamodb = createDynamoDBMock(options);
break;
case 's3':
mocks.s3 = createS3Mock(options);
break;
case 'sqs':
mocks.sqs = createSQSMock(options);
break;
case 'sns':
mocks.sns = createSNSMock(options);
break;
case 'sfn':
mocks.sfn = createSFNMock(options);
break;
case 'ses':
mocks.ses = createSESMock(options);
break;
}
});
return {
...mocks,
resetAll: () => {
Object.values(mocks).forEach((m) => m.reset());
},
restoreAll: () => {
Object.values(mocks).forEach((m) => m.restore());
},
};
}
//# sourceMappingURL=aws-mock-manager.js.map