Skip to content

Commit

Permalink
chore: standardized env setup for tests (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Jan 18, 2023
1 parent 897cda6 commit 0991021
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 27 deletions.
4 changes: 4 additions & 0 deletions packages/commons/jest.config.js
@@ -1,4 +1,8 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: COMMONS',
color: 'red',
},
'preset': 'ts-jest',
'transform': {
'^.+\\.ts?$': 'ts-jest',
Expand Down
2 changes: 1 addition & 1 deletion packages/idempotency/jest.config.js
@@ -1,7 +1,7 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: IDEMPOTENCY',
color: 'blue',
color: 'yellow',
},
'runner': 'groups',
'preset': 'ts-jest',
Expand Down
Expand Up @@ -3,5 +3,7 @@ process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_REGION = 'eu-west-1';
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
process.env.AWS_REGION = 'eu-west-1';
}
process.env._HANDLER = 'index.handler';
Expand Up @@ -2,7 +2,9 @@
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_REGION = 'eu-west-1';
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
process.env.AWS_REGION = 'eu-west-1';
}

// Powertools variables
process.env.LOG_LEVEL = 'DEBUG';
Expand Down
7 changes: 7 additions & 0 deletions packages/metrics/jest.config.js
@@ -1,4 +1,8 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: METRICS',
color: 'green',
},
'runner': 'groups',
'preset': 'ts-jest',
'transform': {
Expand Down Expand Up @@ -33,4 +37,7 @@ module.exports = {
'json-summary',
'text',
'lcov' ],
'setupFiles': [
'<rootDir>/tests/helpers/populateEnvironmentVariables.ts'
]
};

This file was deleted.

10 changes: 10 additions & 0 deletions packages/metrics/tests/helpers/populateEnvironmentVariables.ts
@@ -0,0 +1,10 @@
// Reserved variables
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
process.env.AWS_REGION = 'eu-west-1';
}

// Powertools variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
10 changes: 2 additions & 8 deletions packages/metrics/tests/unit/Metrics.test.ts
Expand Up @@ -7,7 +7,6 @@
import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface } from '@aws-lambda-powertools/commons';
import { Context, Callback } from 'aws-lambda';
import { Metrics, MetricUnits } from '../../src/';
import { populateEnvironmentVariables } from '../helpers';

const MAX_METRICS_SIZE = 100;
const MAX_DIMENSION_COUNT = 29;
Expand All @@ -20,7 +19,7 @@ interface LooseObject {
}

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

Expand All @@ -29,12 +28,7 @@ describe('Class: Metrics', () => {
});

beforeAll(() => {
populateEnvironmentVariables();
});

afterEach(() => {
process.env = originalEnvironmentVariables;
delete process.env.POWERTOOLS_SERVICE_NAME;
process.env = { ...ENVIRONMENT_VARIABLES };
});

describe('Feature: Dimensions logging', () => {
Expand Down
Expand Up @@ -2,8 +2,6 @@
process.env._X_AMZN_TRACE_ID = 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_REGION = 'eu-west-1';

// Powertools variables
process.env.LOG_LEVEL = 'DEBUG';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
process.env.AWS_REGION = 'eu-west-1';
}
2 changes: 1 addition & 1 deletion packages/tracer/jest.config.js
@@ -1,7 +1,7 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: TRACER',
color: 'cyan',
color: 'white',
},
'runner': 'groups',
'preset': 'ts-jest',
Expand Down
Expand Up @@ -3,7 +3,9 @@ process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_REGION = 'eu-west-1';
if (process.env.AWS_REGION === undefined && process.env.CDK_DEFAULT_REGION === undefined) {
process.env.AWS_REGION = 'eu-west-1';
}
process.env._HANDLER = 'index.handler';

// Powertools variables
Expand Down

0 comments on commit 0991021

Please sign in to comment.