Skip to content

test(logger): add unit tests with most important scenarios and features #52

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 29 commits into from
Jun 9, 2021

Conversation

saragerion
Copy link
Contributor

@saragerion saragerion commented May 14, 2021

Description of your changes

This PR contains the code related to unit tests for the Logger utility.

Unit tests alone do not warranty by any means that the code is bullet proof and performant, but it's a starting point.
Integration tests and other types of test suites can be added after the beta release.

No dependencies change needed for this code change.

Next item on the list: opening a PR with the documentation of the Logger utility.

How to verify this change

cd packages/logger
npm t

Test output in the console will be added in the comments below.

Related issues, RFCs

#24

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

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

saragerion and others added 24 commits May 14, 2021 14:54
Bumps [eslint](https://github.com/eslint/eslint) from 7.26.0 to 7.27.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/master/CHANGELOG.md)
- [Commits](eslint/eslint@v7.26.0...v7.27.0)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [ts-node](https://github.com/TypeStrong/ts-node) from 9.1.1 to 10.0.0.
- [Release notes](https://github.com/TypeStrong/ts-node/releases)
- [Commits](TypeStrong/ts-node@v9.1.1...v10.0.0)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.2.4 to 4.3.2.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](microsoft/TypeScript@v4.2.4...v4.3.2)

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* Updating shrinkwrap

* Logger deps. update:
* Updating shrinkwrap

* Logger deps. update:

* Bumping version of ws

* Bumping version of ws
* build(deps-dev): bump jest from 26.6.3 to 27.0.4

Bumps [jest](https://github.com/facebook/jest) from 26.6.3 to 27.0.4.
- [Release notes](https://github.com/facebook/jest/releases)
- [Changelog](https://github.com/facebook/jest/blob/master/CHANGELOG.md)
- [Commits](jestjs/jest@v26.6.3...v27.0.4)

---
updated-dependencies:
- dependency-name: jest
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bumping ts-jest along side jest

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Alan Churley <[email protected]>
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 15.12.1 to 15.12.2.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) from 4.26.0 to 4.26.1.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v4.26.1/packages/eslint-plugin)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/eslint-plugin"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…#80)

Bumps [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) from 4.26.0 to 4.26.1.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/parser/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v4.26.1/packages/parser)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/parser"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@saragerion saragerion changed the title test(logger): Logger class, logger function test(logger): add unit tests with most important scenarios and features Jun 9, 2021
@saragerion saragerion marked this pull request as ready for review June 9, 2021 22:41
@saragerion
Copy link
Contributor Author

saragerion commented Jun 9, 2021


logger (logger-unit-tests)$ npm t

> @aws-lambda-powertools/[email protected] test
> jest  --detectOpenHandles --coverage --verbose

 PASS   AWS Lambda Powertools utility: LOGGER  tests/unit/formatter/PowertoolLogFormatter.test.ts
  Class: PowertoolLogFormatter
    Method: formatAttributes
      ✓ When optional parameters DO NOT have a value set, it returns an object with expected structure and values (3 ms)
      ✓ When optional parameters DO have a value set, it returns an object with expected structure and values
    Method: formatError
      ✓ When an error of type Error is passed, it returns an object with expected structure and values (2 ms)
      ✓ When an error of type ReferenceError is passed, it returns an object with expected structure and values (2 ms)
      ✓ When an error of type AssertionError is passed, it returns an object with expected structure and values (3 ms)
      ✓ When an error of type RangeError is passed, it returns an object with expected structure and values (1 ms)
      ✓ When an error of type SyntaxError is passed, it returns an object with expected structure and values (1 ms)
      ✓ When an error of type TypeError is passed, it returns an object with expected structure and values (2 ms)
    Method: formatTimestamp
      ✓ It returns a datetime value ISO 8601 compliant
    Method: getCodeLocation
      ✓ When the stack IS present, it returns a datetime value ISO 8601 compliant
      ✓ When the stack IS NOT present, it returns a datetime value ISO 8601 compliant (1 ms)
      ✓ When the stack IS NOT present, it returns a datetime value ISO 8601 compliant (1 ms)

 PASS   AWS Lambda Powertools utility: LOGGER  tests/unit/Logger.test.ts
  Class: Logger
    Method: "debug"
      Feature: log level
        ✓ when the Logger's log level is DEBUG, it DOES prints to stdout (5 ms)
        ✓ when the Logger's log level is INFO, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is WARN, it DOES NOT print to stdout
        ✓ when the Logger's log level is ERROR, it DOES NOT print to stdout (1 ms)
      Feature: sample rate
        ✓ when the Logger's log level is higher and the current Lambda invocation IS NOT sampled for logging, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is higher and the current Lambda invocation IS sampled for logging, it DOES print to stdout (1 ms)
      Feature: capture Lambda context information and add it in the printed logs
        ✓ when the Lambda context is not captured and a string is passed as log message, it should print a valid DEBUG log (2 ms)
        ✓ when the Lambda context is captured, it returns a valid DEBUG log (1 ms)
      Feature: ephemeral log attributes
        ✓ when added, they should appear in that log item only (3 ms)
      Feature: persistent log attributes
        ✓ when persistent log attributes are added to the Logger instance, they should appear in all logs printed by the instance (2 ms)
      Feature: handle safely unexpected errors
        ✓ when a logged item references itself, the logger ignores the keys that cause a circular reference (1 ms)
    Method: "info"
      Feature: log level
        ✓ when the Logger's log level is DEBUG, it DOES prints to stdout (1 ms)
        ✓ when the Logger's log level is INFO, it DOES print to stdout (1 ms)
        ✓ when the Logger's log level is WARN, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is ERROR, it DOES NOT print to stdout (1 ms)
      Feature: sample rate
        ✓ when the Logger's log level is higher and the current Lambda invocation IS NOT sampled for logging, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is higher and the current Lambda invocation IS sampled for logging, it DOES print to stdout (1 ms)
      Feature: capture Lambda context information and add it in the printed logs
        ✓ when the Lambda context is not captured and a string is passed as log message, it should print a valid INFO log (1 ms)
        ✓ when the Lambda context is captured, it returns a valid INFO log (1 ms)
      Feature: ephemeral log attributes
        ✓ when added, they should appear in that log item only (2 ms)
      Feature: persistent log attributes
        ✓ when persistent log attributes are added to the Logger instance, they should appear in all logs printed by the instance (1 ms)
      Feature: handle safely unexpected errors
        ✓ when a logged item references itself, the logger ignores the keys that cause a circular reference
    Method: "warn"
      Feature: log level
        ✓ when the Logger's log level is DEBUG, it DOES prints to stdout
        ✓ when the Logger's log level is INFO, it DOES print to stdout (1 ms)
        ✓ when the Logger's log level is WARN, it DOES print to stdout (4 ms)
        ✓ when the Logger's log level is ERROR, it DOES NOT print to stdout (1 ms)
      Feature: sample rate
        ✓ when the Logger's log level is higher and the current Lambda invocation IS NOT sampled for logging, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is higher and the current Lambda invocation IS sampled for logging, it DOES print to stdout (1 ms)
      Feature: capture Lambda context information and add it in the printed logs
        ✓ when the Lambda context is not captured and a string is passed as log message, it should print a valid WARN log
        ✓ when the Lambda context is captured, it returns a valid WARN log (1 ms)
      Feature: ephemeral log attributes
        ✓ when added, they should appear in that log item only (2 ms)
      Feature: persistent log attributes
        ✓ when persistent log attributes are added to the Logger instance, they should appear in all logs printed by the instance (1 ms)
      Feature: handle safely unexpected errors
        ✓ when a logged item references itself, the logger ignores the keys that cause a circular reference (1 ms)
    Method: "error"
      Feature: log level
        ✓ when the Logger's log level is DEBUG, it DOES prints to stdout (1 ms)
        ✓ when the Logger's log level is INFO, it DOES print to stdout (1 ms)
        ✓ when the Logger's log level is WARN, it DOES print to stdout (1 ms)
        ✓ when the Logger's log level is ERROR, it DOES print to stdout (1 ms)
      Feature: sample rate
        ✓ when the Logger's log level is higher and the current Lambda invocation IS NOT sampled for logging, it DOES NOT print to stdout (1 ms)
        ✓ when the Logger's log level is higher and the current Lambda invocation IS sampled for logging, it DOES print to stdout
      Feature: capture Lambda context information and add it in the printed logs
        ✓ when the Lambda context is not captured and a string is passed as log message, it should print a valid ERROR log (1 ms)
        ✓ when the Lambda context is captured, it returns a valid ERROR log (1 ms)
      Feature: ephemeral log attributes
        ✓ when added, they should appear in that log item only (2 ms)
      Feature: persistent log attributes
        ✓ when persistent log attributes are added to the Logger instance, they should appear in all logs printed by the instance (1 ms)
      Feature: handle safely unexpected errors
        ✓ when a logged item references itself, the logger ignores the keys that cause a circular reference (1 ms)
    Method: appendKeys
      ✓ when called, populates the logger's propriety persistentLogAttributes  (1 ms)
    Method: createChild
      ✓ when called, creates a distinct clone of the original logger instance (2 ms)
      ✓ when called, it returns a DISTINCT clone of the logger instance (6 ms)
    Method: injectLambdaContext
      ✓ when used as decorator, it returns a function that captures Lambda's context information and adds it in the printed logs (1 ms)
    Method: isColdStart
      ✓ when called, it returns false the first time and always true after that (1 ms)
    Method: refreshSampleRateCalculation
      ✓ when called, it recalculates whether the current Lambda invocation's logs will be printed or not (2 ms)

 PASS   AWS Lambda Powertools utility: LOGGER  tests/unit/helpers.test.ts
  Helper: createLogger function
    LoggerOptions parameters
      ✓ when no logger options are passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when all logger options are passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when a custom logFormatter is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when a custom serviceName is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when a custom logLevel is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when no log level is set, returns a Logger instance with INFO level (1 ms)
      ✓ when a custom sampleRateValue is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when a custom customConfigService is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when custom persistentLogAttributes is passed, returns a Logger instance with the correct proprieties (1 ms)
      ✓ when A custom environment is passed, returns a Logger instance with the correct proprieties (1 ms)

 PASS   AWS Lambda Powertools utility: LOGGER  tests/unit/config/EnvironmentVariablesService.test.ts
  Class: EnvironmentVariablesService
    Method: get
      ✓ When the variable IS present, it returns the value of a runtime variable (1 ms)
      ✓ When the variable IS NOT present, it returns an empty string (1 ms)
    Method: getAwsRegion
      ✓ It returns the value of the environment variable AWS_REGION (1 ms)
    Method: getCurrentEnvironment
      ✓ It returns the value of the environment variable AWS_REGION (1 ms)
    Method: getFunctionMemory
      ✓ It returns the value of the environment variable AWS_LAMBDA_FUNCTION_MEMORY_SIZE
    Method: getFunctionName
      ✓ It returns the value of the environment variable AWS_LAMBDA_FUNCTION_NAME (1 ms)
    Method: getFunctionVersion
      ✓ It returns the value of the environment variable AWS_LAMBDA_FUNCTION_VERSION (1 ms)
    Method: getLogLevel
      ✓ It returns the value of the environment variable LOG_LEVEL (1 ms)
    Method: getSampleRateValue
      ✓ It returns the value of the environment variable POWERTOOLS_LOGGER_SAMPLE_RATE
    Method: getServiceName
      ✓ It returns the value of the environment variable POWERTOOLS_SERVICE_NAME (4 ms)
    Method: getXrayTraceId
      ✓ It returns the value of the environment variable _X_AMZN_TRACE_ID (1 ms)

---------------------------------|---------|----------|---------|---------|-------------------
File                             | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------|---------|----------|---------|---------|-------------------
All files                        |     100 |      100 |     100 |     100 |
 src                             |     100 |      100 |     100 |     100 |
  Logger.ts                      |     100 |      100 |     100 |     100 |
  helpers.ts                     |     100 |      100 |     100 |     100 |
  index.ts                       |     100 |      100 |     100 |     100 |
 src/config                      |     100 |      100 |     100 |     100 |
  ConfigService.ts               |     100 |      100 |     100 |     100 |
  EnvironmentVariablesService.ts |     100 |      100 |     100 |     100 |
  index.ts                       |     100 |      100 |     100 |     100 |
 src/formatter                   |     100 |      100 |     100 |     100 |
  LogFormatter.ts                |     100 |      100 |     100 |     100 |
  PowertoolLogFormatter.ts       |     100 |      100 |     100 |     100 |
  index.ts                       |     100 |      100 |     100 |     100 |
 src/log                         |     100 |      100 |     100 |     100 |
  LogItem.ts                     |     100 |      100 |     100 |     100 |
  index.ts                       |     100 |      100 |     100 |     100 |
---------------------------------|---------|----------|---------|---------|-------------------
Test Suites: 4 passed, 4 total
Tests:       83 passed, 83 total
Snapshots:   0 total
Time:        4.021 s
Ran all test suites.

@saragerion saragerion merged commit ad907cd into main Jun 9, 2021
@saragerion saragerion deleted the logger-unit-tests branch June 9, 2021 23:23
@heitorlessa heitorlessa added the internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) label Jan 5, 2022
@saragerion saragerion self-assigned this Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants