@gp_jcisneros/aws-utils
Version:
AWS SDK utilities for GreenPay microservices
195 lines (160 loc) • 6.4 kB
JavaScript
const { LambdaUtils } = require('../src/LambdaUtils');
const { AWSError, IntegrationError } = require('@gp_jcisneros/errors');
const { LambdaClient } = require('@aws-sdk/client-lambda');
// Mock functions
const mockSend = jest.fn();
// Reset mocks before each test
beforeEach(() => {
jest.clearAllMocks();
// Setup default mock implementation
LambdaClient.mockImplementation(() => ({
send: mockSend,
destroy: jest.fn().mockResolvedValue(undefined),
}));
});
describe('LambdaUtils', () => {
describe('constructor', () => {
it('should create LambdaUtils instance', () => {
const lambdaUtils = new LambdaUtils();
expect(lambdaUtils).toBeInstanceOf(LambdaUtils);
expect(lambdaUtils.client).toBeDefined();
});
it('should create LambdaUtils instance with custom region', () => {
const lambdaUtils = new LambdaUtils('us-west-2');
expect(lambdaUtils).toBeInstanceOf(LambdaUtils);
});
});
describe('invokeLambda', () => {
it('should invoke lambda successfully with RequestResponse', async () => {
const responsePayload = { result: 'success' };
const mockResponse = {
Payload: Buffer.from(JSON.stringify(responsePayload)),
StatusCode: 200,
};
mockSend.mockResolvedValue(mockResponse);
const lambdaUtils = new LambdaUtils();
const result = await lambdaUtils.invokeLambda('test-function', {
test: 'data',
});
expect(result).toEqual(responsePayload);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle lambda invocation error', async () => {
mockSend.mockRejectedValue(new Error('Lambda error'));
const lambdaUtils = new LambdaUtils();
await expect(
lambdaUtils.invokeLambda('test-function', { test: 'data' })
).rejects.toThrow(AWSError);
});
it('should handle function error response', async () => {
const mockResponse = {
Payload: Buffer.from(
JSON.stringify({ errorMessage: 'Function error' })
),
StatusCode: 200,
FunctionError: 'Handled',
};
mockSend.mockResolvedValue(mockResponse);
const lambdaUtils = new LambdaUtils();
await expect(
lambdaUtils.invokeLambda('test-function', { test: 'data' })
).rejects.toThrow(IntegrationError);
});
});
describe('invokeLambdaAsync', () => {
it('should invoke lambda asynchronously', async () => {
const mockResponse = {
StatusCode: 202,
ResponseMetadata: { RequestId: 'test-request-id' },
};
mockSend.mockResolvedValue(mockResponse);
const lambdaUtils = new LambdaUtils();
const result = await lambdaUtils.invokeLambdaAsync('test-function', {
test: 'data',
});
expect(result).toEqual({
statusCode: 202,
requestId: 'test-request-id',
});
});
it('should handle async invocation error', async () => {
mockSend.mockRejectedValue(new Error('Async error'));
const lambdaUtils = new LambdaUtils();
await expect(
lambdaUtils.invokeLambdaAsync('test-function', { test: 'data' })
).rejects.toThrow(AWSError);
});
});
describe('getFunctionConfiguration', () => {
it('should get function configuration successfully', async () => {
const mockConfig = {
FunctionName: 'test-function',
Runtime: 'nodejs18.x',
Handler: 'index.handler',
CodeSize: 1024,
Description: 'Test function',
};
mockSend.mockResolvedValue(mockConfig);
const lambdaUtils = new LambdaUtils();
const result =
await lambdaUtils.getFunctionConfiguration('test-function');
expect(result).toEqual(mockConfig);
});
it('should handle configuration error', async () => {
mockSend.mockRejectedValue(new Error('Config error'));
const lambdaUtils = new LambdaUtils();
await expect(
lambdaUtils.getFunctionConfiguration('test-function')
).rejects.toThrow(AWSError);
});
});
describe('static methods', () => {
it('should have static invokeLambda method', () => {
expect(typeof LambdaUtils.invokeLambda).toBe('function');
});
it('should have static invokeLambdaAsync method', () => {
expect(typeof LambdaUtils.invokeLambdaAsync).toBe('function');
});
it('should have static getFunctionConfiguration method', () => {
expect(typeof LambdaUtils.getFunctionConfiguration).toBe('function');
});
});
describe('error handling', () => {
it('should create AWSError with correct properties', () => {
const error = new AWSError('Test error', 'LAMBDA', 'INVOCATION_ERROR');
expect(error).toBeInstanceOf(AWSError);
expect(error.message).toBe('Test error');
expect(error.service).toBe('LAMBDA');
expect(error.awsErrorCode).toBe('INVOCATION_ERROR');
expect(error.statusCode).toBe(500);
expect(error.errorCode).toBe('AWS_LAMBDA');
expect(error.description).toBe('Test error');
expect(error.integration).toBe('aws-service');
});
it('should create IntegrationError with correct properties', () => {
const error = new IntegrationError(
'Test error',
'LAMBDA_EXECUTION_ERROR',
'lambda',
{ functionName: 'test' }
);
expect(error).toBeInstanceOf(IntegrationError);
expect(error.message).toBe('Test error');
expect(error.integrationCode).toBe('LAMBDA_EXECUTION_ERROR');
expect(error.integrationName).toBe('lambda');
expect(error.statusCode).toBe(502);
expect(error.errorCode).toBe('LAMBDA_EXECUTION_ERROR');
expect(error.description).toBe('Test error');
expect(error.integration).toBe('lambda');
expect(error.context).toEqual({ functionName: 'test' });
});
it('should re-throw custom errors', async () => {
const customError = new AWSError('Custom error', 'LAMBDA', 'TEST_ERROR');
mockSend.mockRejectedValue(customError);
const lambdaUtils = new LambdaUtils();
await expect(
lambdaUtils.invokeLambda('test-function', { test: 'data' })
).rejects.toBe(customError);
});
});
});