-
Notifications
You must be signed in to change notification settings - Fork 139
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
fix(logger): wait for decorated method return before clearing out state #1087
Merged
saragerion
merged 3 commits into
main
from
1085-bug-logger-decorator-clears-states-too-early
Sep 29, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1166,6 +1166,48 @@ describe('Class: Logger', () => { | |||||
|
||||||
}); | ||||||
|
||||||
test('when used as decorator on an async method, the method is awaited correctly', async () => { | ||||||
|
||||||
// Prepare | ||||||
const injectLambdaContextAfterOrOnErrorMock = jest.fn().mockReturnValue('worked'); | ||||||
// Temporarily override the cleanup static method so that we can "spy" on it. | ||||||
// This method is always called after the handler has returned in the decorator | ||||||
// implementation. | ||||||
Logger.injectLambdaContextAfterOrOnError = injectLambdaContextAfterOrOnErrorMock; | ||||||
const logger = new Logger({ | ||||||
logLevel: 'DEBUG', | ||||||
}); | ||||||
const consoleSpy = jest.spyOn(logger['console'], 'info').mockImplementation(); | ||||||
class LambdaFunction implements LambdaInterface { | ||||||
@logger.injectLambdaContext() | ||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||||||
// @ts-ignore | ||||||
public async handler<TResult>(_event: unknown, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||||||
await this.dummyMethod(); | ||||||
logger.info('This is a DEBUG log'); | ||||||
|
||||||
return; | ||||||
} | ||||||
|
||||||
private async dummyMethod(): Promise<void> { | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
||||||
// Act | ||||||
const lambda = new LambdaFunction(); | ||||||
const handler = lambda.handler.bind(lambda); | ||||||
await handler({}, dummyContext, () => console.log('Lambda invoked!')); | ||||||
|
||||||
// Assess | ||||||
expect(consoleSpy).toBeCalledTimes(1); | ||||||
// Here we assert that the logger.info method is called before the cleanup function that should awlays | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit
Suggested change
|
||||||
// be called after the handler has returned. If logger.info is called after it means the decorator is | ||||||
// NOT awaiting the handler which would cause the test to fail. | ||||||
expect(consoleSpy.mock.invocationCallOrder[0]).toBeLessThan(injectLambdaContextAfterOrOnErrorMock.mock.invocationCallOrder[0]); | ||||||
|
||||||
}); | ||||||
|
||||||
}); | ||||||
|
||||||
describe('Method: refreshSampleRateCalculation', () => { | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Housekeeping. The
target
param is never used so I'm prefixing it with underscore to tell the IDE that this is intended.