Skip to content

refactor(logger): use vitest env helpers in unit tests #4360

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

Conversation

dwrth
Copy link
Contributor

@dwrth dwrth commented Aug 19, 2025

Summary

Implements Vitest environment helpers to avoid direct manipulations on process.env

Changes

  1. Main Changes:
  • Replacing direct process.env manipulations with Vitest's environment helper functions:
    • Using vi.stubEnv() to set environment variables
    • Using vi.unstubAllEnvs() to clear environment variables
    • Removing manual environment variable backups and restorations
  1. Files Modified:
  • packages/logger/tests/unit/formatters.test.ts
  • packages/logger/tests/unit/initializeLogger.test.ts
  • packages/logger/tests/unit/injectLambdaContext.test.ts
  • packages/logger/tests/unit/logBuffer.test.ts
  • packages/logger/tests/unit/logEvent.test.ts
  • packages/logger/tests/unit/logLevels.test.ts
  1. Pattern of Changes:
  • Removing const ENVIRONMENT_VARIABLES = process.env storage
  • Adding afterEach hooks with vi.unstubAllEnvs()
  • Replacing process.env = {...} assignments with vi.stubEnv()
  • Moving logger instance creation into beforeEach blocks

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #4269


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@boring-cyborg boring-cyborg bot added the tests PRs that add or change tests label Aug 19, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Aug 19, 2025
Copy link
Contributor

@sdangol sdangol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Added a few minor comments.

@dreamorosi dreamorosi requested a review from sdangol August 20, 2025 09:13
Copy link
Contributor

@sdangol sdangol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this @dwrth

Copy link

@sdangol sdangol merged commit f9ea611 into aws-powertools:main Aug 20, 2025
5 checks passed
dreamorosi added a commit that referenced this pull request Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: Use Vitest environment variable helpers in Logger unit test
2 participants