-
Notifications
You must be signed in to change notification settings - Fork 146
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
Feature request: disable logs while testing with jest #1030
Comments
Thank you @IslamWahid for opening the issue. I'm going to report part of the content of our discussion on the Slack community channel. Based on what I could gather, when the On our end, we also don't use the global Reproduction example ( import { Logger } from "@aws-lambda-powertools/logger";
const logger = new Logger();
export const handler = async (event: any, _context: any) => {
logger.info("Hello from Lambda");
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello World",
}),
};
}; And corresponding test ( import { handler } from "../lib/repro";
import { ContextExamples } from "@aws-lambda-powertools/commons";
describe("MyUnitTest", () => {
test("Lambda invoked successfully", async () => {
const testEvent = { test: "test" };
await handler(testEvent, ContextExamples.helloworldContext);
});
}); Does in fact generate logs even when using the
Before deciding on whether we should support a way of disabling the logger for unit tests specifically we'd like to hear the opinions of more customers to see if there's demand or if there are other concerns / alternatives to implement it. In the meantime the pattern that you have described seems like a nice stop-gap solution and perhaps it could make sense to add a mention to it under the "Testing your code" section of the Logger docs (here). What do you think @saragerion? |
I'd definitely find this useful. Unit tests can get quite noisy, particularly when testing error cases. |
I also agree, this can be very useful |
The logger could just support a special "off" LOG_LEVEL and is up to the user to set it accordingly when needed. |
PR #1141 introduces a new environment variable called The variable at the moment focuses with pretty-printing (i.e. increasing indentation) logs, however once the PR is merged I'll look into whether there's a way of restoring the standard If this is successful, you should be able to use the I'll report back once the PR is merged & I have attempted the change. |
Leaving here a recap of the discussion plus ideas for the proposed change: Recap of the current status: As Logger user I would like to suppress logs during testing. Jest supports a Now that we have an environment variable explicitly aimed at local development - if (POWERTOOLS_DEV === true) this.console = console; The action here would be:
|
@dreamorosi hi, I'm going to look at it and create a PR. |
Great thank you @shdq, if you have any questions reach out here or on the #typescript channel on discord |
Hi @dreamorosi. I hope you are doing well! I looked at it and consider two options: 1. "Restore" console
2. "Initialize" consoleSimilar to I implemented and tested both they are working: I prefer the second one because of its readability. You start reading the code and see the initialization method, look there and understand that the But with the second approach and I tried to find out why and changed Code:
Any clues? I haven't found a solution yet. But maybe it isn't needed, and the first option is ok 😉 I'm sorry, but maybe it is a pre-optimization again. I'm asking because if dev mode options (like indentation and suppress logs) keep growing, there are two ways to handle this:
What do you think? P.S. It makes sense to rename Have a nice day! |
Hi @shdq thank you for looking into the feature! I agree with you that option 2 is clearer to read, let's go with that option and remove the default value from the Logger class for Let's use your implementation: if (!this.getEnvVarsService().getDevMode()) {
this.console = new Console({ stdout: process.stdout, stderr: process.stderr });
} else {
this.console = console;
} and place it in a private method which is called from inside Regarding the failing test one, I spent a bit of time looking into the test and I think I have an idea of what is happening: I put a breakpoint at the failing part of the test and checked the objects & property. As we can see in the screenshot, both This is expected since after the proposed change, each time a new instance of Logger is spawned we also initialise a new this.console = new Console({ stdout: process.stdout, stderr: process.stderr }); Given the above, we could change the line in that unit test like so: - expect(parentLogger).toEqual(childLogger);
+ expect(childLogger).toEqual({
+ ...parentLogger,
+ console: expect.any(Console),
+ }); In this way we are recursively checking the equality of all properties of I should also mention that we have an issue related to these tests (#483) which most probably will require some additional adjustments to these tests, perhaps it could be addressed in one of the next PRs soon. Finally, related to your last question about renaming |
|
Description of the feature request
Problem statement
While running jest I would like to suppress the logs on the console to make it readable and clean.
jest CLI is offering
--silent
option to prevent tests from printing messages through the console, but it doesn't seem to work with the logger.Summary of the feature
Suggestions:
--silent
NODE_ENV
is atest
Code examples
Benefits for you and the wider AWS community
make the console readable and clean while running tests.
Describe alternatives you've considered
I have created a
__mocks__/@aws-lambda-powertools/logger.ts
with this setupand this solves the issue.
Additional context
Related issues, RFCs
The text was updated successfully, but these errors were encountered: