Skip to content

Maintenance: Use Vitest environment variable helpers in Logger unit test #4269

@svozza

Description

@svozza

Summary

The logger unit test have multiple tests that rely on specific environment variables being set. Currently the tests manage the environment manually by copying the process.env object the top of the test suite and over-wrtiing in in a before each block. However, Vitest povides specific utility functions for mocking environment variables and cleaning them up.

Why is this needed?

Allowing Vitest to manage environment variable handle reduces the chances of the global test environment being polluted by tests that forget to clean up after themselves.

Which area does this relate to?

Logger

Solution

In packages/logger/tests/unit/formatters.test.ts, make the following changes:

- process.env.POWERTOOLS_DEV = 'true';

describe('Formatters', () => {
-  const ENVIRONMENT_VARIABLES = process.env;
-
   beforeEach(() => {
-    process.env = { ...ENVIRONMENT_VARIABLES };
+    vi.unstubAllEnvs();
+    vi.stubEnv('POWERTOOLS_DEV', 'true');
     const mockDate = new Date(1466424490000);
     vi.useFakeTimers().setSystemTime(mockDate);
     vi.clearAllMocks();
     unformattedAttributes.timestamp = mockDate;
   });

  it('formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset', () => {
    // Prepare
-    process.env.TZ = 'America/New_York';
+    vi.stubEnv('TZ', 'America/New_York');

  it('formats the timestamp to ISO 8601, adjusting for `America/New_York` timezone, preserving milliseconds and accounting for date change', () => {
    // Prepare
-    process.env.TZ = 'America/New_York';
+    vi.stubEnv('TZ', 'America/New_York');

  it('formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset', () => {
    // Prepare
-    process.env.TZ = 'America/New_York';
+    vi.stubEnv('TZ', 'America/New_York');

  it('it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone', () => {
    // Prepare
-    process.env.TZ = 'Asia/Dhaka';
+    vi.stubEnv('TZ', 'Asia/Dhaka');


  it('returns defaults to :UTC when an env variable service is not set', () => {
    // Prepare
-    process.env.TZ = 'Asia/Dhaka';
+    vi.stubEnv('TZ', 'Asia/Dhaka');

  it('defaults to :UTC when the TZ env variable is set to :/etc/localtime', () => {
    // Prepare
-    process.env.TZ = ':/etc/localtime';
+    vi.stubEnv('TZ', ':/etc/localtime');

In packages/logger/tests/unit/logBuffer.test.ts, make the following changes:

describe('Buffer logs', () => {
-  const ENVIRONMENT_VARIABLES = process.env;
-
   beforeEach(() => {
-    process.env = {
-      ...ENVIRONMENT_VARIABLES,
-      POWERTOOLS_DEV: 'true',
-    };
+    vi.unstubAllEnvs();
+    vi.stubEnv('POWERTOOLS_DEV', 'true');
     vi.clearAllMocks();
   });

  it('outputs a warning when the Advanced Logging Configuration Log Level is less verbose than the Log Buffering Log Level', () => {
    // Assemble
-    process.env.AWS_LAMBDA_LOG_LEVEL = 'INFO';
+    vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'INFO');

  it('it safely short circuits when clearBuffer is called without a trace id', () => {
    // Prepare
-    process.env._X_AMZN_TRACE_ID = undefined;
+    vi.stubEnv('_X_AMZN_TRACE_ID', undefined);

  it('it safely short circuits when clearBuffer is called without a trace id', () => {
    // Prepare
-    process.env._X_AMZN_TRACE_ID = undefined;
+    vi.stubEnv('_X_AMZN_TRACE_ID', undefined);

There are similar updates to be made in the all other unit tests in the packages/logger/tests/unit folder that all follow the same pattern outlined above.

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good-first-issueSomething that is suitable for those who want to start contributinghelp-wantedWe would really appreciate some support from community for this oneinternalPRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)loggerThis item relates to the Logger Utility

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions