Skip to content
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(aws-amplify): destroyAmplifyServerContext may not be called #12608

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe('runWithAmplifyServerContext', () => {
mockCreateAmplifyServerContext.mockReturnValueOnce(mockContextSpec);
});

afterEach(() => {
mockDestroyAmplifyServerContext.mockReset();
});

it('should run the operation with the context', () => {
const mockOperation = jest.fn();
runWithAmplifyServerContext(
Expand All @@ -45,7 +49,7 @@ describe('runWithAmplifyServerContext', () => {
expect(mockOperation).toHaveBeenCalledWith(mockContextSpec);
});

it('should destroy the context after the operation', async () => {
it('should destroy the context after the operation completed', async () => {
const mockOperation = jest.fn();
await runWithAmplifyServerContext(
mockAmplifyConfig,
Expand All @@ -63,6 +67,29 @@ describe('runWithAmplifyServerContext', () => {
);
});

it('should destroy the context when the operation throws', async () => {
const testError = new Error('some error');
const mockOperation = jest.fn();
mockOperation.mockRejectedValueOnce(testError);

await expect(
runWithAmplifyServerContext(
mockAmplifyConfig,
{
Auth: {
tokenProvider: mockTokenProvider,
credentialsProvider: mockCredentialAndIdentityProvider,
},
},
mockOperation
)
).rejects.toThrow(testError);

expect(mockDestroyAmplifyServerContext).toHaveBeenCalledWith(
mockContextSpec
);
});

it('should return the result returned by the operation callback function', async () => {
const mockResultValue = {
url: 'http://123.com',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export const runWithAmplifyServerContext: AmplifyServer.RunOperationWithContext
);

// run the operation with injecting the context
const result = await operation(contextSpec);
try {
const result = await operation(contextSpec);

destroyAmplifyServerContext(contextSpec);

return result;
return result;
} finally {
// ensures destroy the context regardless whether the operation succeeded or failed
destroyAmplifyServerContext(contextSpec);
}
};
Loading