Skip to content

chore(tests): standardize env setup for tests #1255

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
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
4 changes: 4 additions & 0 deletions packages/commons/jest.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: IDEMPOTENCY',
color: 'blue',
color: 'yellow',
},
'runner': 'groups',
'preset': 'ts-jest',
Expand Down
Original file line number Diff line number Diff line change
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';
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: TRACER',
color: 'cyan',
color: 'white',
},
'runner': 'groups',
'preset': 'ts-jest',
Expand Down
Original file line number Diff line number Diff line change
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