You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(logger): disable logs while testing with jest --silent in dev env (#1165)
* feat(logger): disable logs while testing with jest --silent in dev env
* refactor(logger): rename method
* docs(logger): add testing section with jest --silent option, which is also suppresses logs (#1165)
* Update docs/core/logger.md
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
* test(logger): add test for setConsole() method
* test(logger): add comment for assertion
* docs(logger): add an example of using POWERTOOLS_DEV with jest --silent
* Update packages/logger/src/Logger.ts
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
Copy file name to clipboardExpand all lines: docs/core/logger.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -971,3 +971,11 @@ This is a Jest sample that provides the minimum information necessary for Logger
971
971
972
972
!!! tip
973
973
If you don't want to declare your own dummy Lambda Context, you can use [`ContextExamples.helloworldContext`](https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/commons/src/samples/resources/contexts/hello-world.ts#L3-L16) from [`@aws-lambda-powertools/commons`](https://www.npmjs.com/package/@aws-lambda-powertools/commons).
974
+
975
+
### Suppress logs with Jest
976
+
977
+
When unit testing your code with [Jest](https://jestjs.io) you can use the `POWERTOOLS_DEV` environment variable in conjunction with the Jest `--silent` CLI option to suppress logs from Logger.
978
+
979
+
```bash title="Disabling logs while testing with Jest"
test('When the `POWERTOOLS_DEV` env var is SET console object is set to the global node console otherwise to the instance of the internal version of console',()=>{
1471
+
1472
+
// Prepare
1473
+
constlogger=newLogger();
1474
+
process.env.POWERTOOLS_DEV='true';
1475
+
constdevLogger=newLogger();
1476
+
1477
+
// Assess
1478
+
expect(devLogger).toEqual({
1479
+
...devLogger,
1480
+
console: console,
1481
+
});
1482
+
// since instances of a class are not equal objects,
1483
+
// we assert the opposite – console is not the global node object
0 commit comments