Skip to content

chore(internal): remove unused test folder from project's root #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const helloworldContext: Context = {
memoryLimitInMB: '128',
logGroupName: '/aws/lambda/foo-bar-function-123456abcdef',
logStreamName: '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456',
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:123456789012:function:Example',
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
invokedFunctionArn: 'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
getRemainingTimeInMillis: () => 1234,
done: () => console.log('Done!'),
fail: () => console.log('Failed!'),
Expand Down
37 changes: 16 additions & 21 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
* @group unit/logger/all
*/

import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
import { createLogger, Logger } from '../../src';
import { EnvironmentVariablesService } from '../../src/config';
import { PowertoolLogFormatter } from '../../src/formatter';
import { ClassThatLogs, LogJsonIndent } from '../../src/types';
import { Context, Handler } from 'aws-lambda';
import { Context } from 'aws-lambda';
import { Console } from 'console';

interface LambdaInterface {
handler: Handler
}

const mockDate = new Date(1466424490000);
const dateSpy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate as unknown as string);

describe('Class: Logger', () => {
const ENVIRONMENT_VARIABLES = process.env;

const context = dummyContext.helloworldContext;
const event = dummyEvent.Custom.CustomEvent;

beforeEach(() => {
dateSpy.mockClear();
process.env = { ...ENVIRONMENT_VARIABLES };
Expand Down Expand Up @@ -244,7 +239,7 @@ describe('Class: Logger', () => {
const logger: Logger & { addContext: (context: Context) => void } = createLogger({
logLevel: 'DEBUG',
});
logger.addContext(dummyContext);
logger.addContext(context);
const consoleSpy = jest.spyOn(logger['console'], methodOfLogger).mockImplementation();

// Act
Expand Down Expand Up @@ -829,7 +824,7 @@ describe('Class: Logger', () => {
}

// Act
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));

// Assess
expect(consoleSpy).toBeCalledTimes(1);
Expand Down Expand Up @@ -865,7 +860,7 @@ describe('Class: Logger', () => {

// Act
logger.info('An INFO log without context!');
await new LambdaFunction().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));

// Assess

Expand Down Expand Up @@ -912,7 +907,7 @@ describe('Class: Logger', () => {

// Act
logger.info('An INFO log without context!');
const actualResult = await new LambdaFunction().handler(dummyEvent, dummyContext);
const actualResult = await new LambdaFunction().handler(event, context);

// Assess

Expand Down Expand Up @@ -971,7 +966,7 @@ describe('Class: Logger', () => {
const persistentAttribs = { ...logger.getPersistentLogAttributes() };

// Act
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };

// Assess
Expand Down Expand Up @@ -1017,7 +1012,7 @@ describe('Class: Logger', () => {

// Act & Assess
const executeLambdaHandler = async (): Promise<void> => {
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));
};
await expect(executeLambdaHandler()).rejects.toThrow('Unexpected error occurred!');
const persistentAttribsAfterInvocation = { ...logger.getPersistentLogAttributes() };
Expand Down Expand Up @@ -1050,7 +1045,7 @@ describe('Class: Logger', () => {
}

// Act
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));

// Assess
expect(consoleSpy).toBeCalledTimes(1);
Expand Down Expand Up @@ -1094,7 +1089,7 @@ describe('Class: Logger', () => {
}

// Act
await new LambdaFunction().handler({ user_id: '123456' }, dummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler({ user_id: '123456' }, context, () => console.log('Lambda invoked!'));

// Assess
expect(consoleSpy).toBeCalledTimes(1);
Expand Down Expand Up @@ -1148,7 +1143,7 @@ describe('Class: Logger', () => {
// Act
const lambda = new LambdaFunction('someValue');
const handler = lambda.handler.bind(lambda);
await handler({}, dummyContext, () => console.log('Lambda invoked!'));
await handler({}, context, () => console.log('Lambda invoked!'));

// Assess
expect(consoleSpy).toBeCalledTimes(1);
Expand Down Expand Up @@ -1198,7 +1193,7 @@ describe('Class: Logger', () => {
// Act
const lambda = new LambdaFunction();
const handler = lambda.handler.bind(lambda);
await handler({}, dummyContext, () => console.log('Lambda invoked!'));
await handler({}, context, () => console.log('Lambda invoked!'));

// Assess
expect(consoleSpy).toBeCalledTimes(1);
Expand Down Expand Up @@ -1381,7 +1376,7 @@ describe('Class: Logger', () => {
const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation();

// Act
logger.logEventIfEnabled(dummyEvent);
logger.logEventIfEnabled(event);

// Assess

Expand Down
65 changes: 29 additions & 36 deletions packages/metrics/tests/unit/Metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
* @group unit/metrics/class
*/

import { ContextExamples as dummyContext, LambdaInterface } from '@aws-lambda-powertools/commons';
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
import { Context, Callback } from 'aws-lambda';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { Metrics, MetricUnits } from '../../src/';
import { populateEnvironmentVariables } from '../helpers';

Expand All @@ -22,14 +19,10 @@ interface LooseObject {
[key: string]: string
}

type DummyEvent = {
key1: string
key2: string
key3: string
};

describe('Class: Metrics', () => {
const originalEnvironmentVariables = process.env;
const context = dummyContext.helloworldContext;
const event = dummyEvent.Custom.CustomEvent;

beforeEach(() => {
consoleSpy.mockClear();
Expand Down Expand Up @@ -99,7 +92,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
Expand Down Expand Up @@ -193,7 +186,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
Expand Down Expand Up @@ -227,7 +220,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
} catch (e) {
expect((<Error>e).message).toBe('Max dimension count hit');
}
Expand Down Expand Up @@ -255,7 +248,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

expect(console.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -283,7 +276,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

expect(console.log).toBeCalledTimes(1);
Expand All @@ -297,13 +290,13 @@ describe('Class: Metrics', () => {
test('Cold start metric should only be written out once and flushed automatically', async () => {
const metrics = new Metrics({ namespace: 'test' });

const handler = async (_event: DummyEvent, _context: Context): Promise<void> => {
const handler = async (_event: unknown, _context: Context): Promise<void> => {
// Should generate only one log
metrics.captureColdStartMetric();
};

await handler(dummyEvent, dummyContext.helloworldContext);
await handler(dummyEvent, dummyContext.helloworldContext);
await handler(event, context);
await handler(event, context);
const loggedData = [JSON.parse(consoleSpy.mock.calls[0][0])];

expect(console.log).toBeCalledTimes(1);
Expand All @@ -329,8 +322,8 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked again!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked again!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(3);
Expand All @@ -356,15 +349,15 @@ describe('Class: Metrics', () => {
metrics.addMetric('test_name', MetricUnits.Seconds, 10);
}
}
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

expect(console.log).toBeCalledTimes(2);
expect(loggedData._aws.CloudWatchMetrics[0].Metrics.length).toBe(1);
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Name).toBe('ColdStart');
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Unit).toBe('Count');
expect(loggedData.service).toBe(serviceName);
expect(loggedData.function_name).toBe(dummyContext.helloworldContext.functionName);
expect(loggedData.function_name).toBe(context.functionName);
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('service');
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('function_name');
expect(loggedData.ColdStart).toBe(1);
Expand All @@ -373,8 +366,8 @@ describe('Class: Metrics', () => {
test('Cold should still log, without a function name', async () => {
const serviceName = 'test-service';
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
const newDummyContext = JSON.parse(JSON.stringify(dummyContext));
delete newDummyContext.functionName;
const newContext = JSON.parse(JSON.stringify(context));
delete newContext.functionName;
class LambdaFunction implements LambdaInterface {
@metrics.logMetrics({ captureColdStartMetric: true })
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -388,7 +381,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, newDummyContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, newContext, () => console.log('Lambda invoked!'));
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

expect(console.log).toBeCalledTimes(2);
Expand Down Expand Up @@ -421,7 +414,7 @@ describe('Class: Metrics', () => {
}

try {
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
} catch (e) {
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
}
Expand All @@ -445,7 +438,7 @@ describe('Class: Metrics', () => {
}

try {
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
} catch (e) {
fail(`Should not throw but got the following Error: ${e}`);
}
Expand All @@ -457,14 +450,14 @@ describe('Class: Metrics', () => {
expect.assertions(1);

const metrics = new Metrics({ namespace: 'test' });
const handler = async (_event: DummyEvent, _context: Context): Promise<void> => {
const handler = async (_event: unknown, _context: Context): Promise<void> => {
metrics.throwOnEmptyMetrics();
// Logic goes here
metrics.publishStoredMetrics();
};

try {
await handler(dummyEvent, dummyContext.helloworldContext);
await handler(event, context);
} catch (e) {
expect((<Error>e).message).toBe('The number of metrics recorded must be higher than zero');
}
Expand All @@ -490,7 +483,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
Expand Down Expand Up @@ -591,7 +584,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
});

test('Publish Stored Metrics should log and clear', async () => {
Expand All @@ -610,7 +603,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
Expand Down Expand Up @@ -640,7 +633,7 @@ describe('Class: Metrics', () => {
}

// Act
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext);
await new LambdaFunction().handler(event, context);

// Assess
expect(console.log).toBeCalledTimes(1);
Expand Down Expand Up @@ -669,7 +662,7 @@ describe('Class: Metrics', () => {
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext);
await new LambdaFunction().handler(event, context);
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

expect(console.log).toBeCalledTimes(1);
Expand All @@ -695,7 +688,7 @@ describe('Class: Metrics', () => {
}

try {
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
} catch (error) {
// DO NOTHING
}
Expand Down Expand Up @@ -724,7 +717,7 @@ describe('Class: Metrics', () => {
metrics.addMetric('test_name', MetricUnits.Seconds, 1);
}
}
await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
await new LambdaFunction().handler(event, context, () => console.log('Lambda invoked!'));
const loggedData = JSON.parse(consoleSpy.mock.calls[0][0]);

// Assess
Expand Down
Loading