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

Tests: lambda.ts test #46

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions src/libs/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import middy from '@middy/core';
import middyJsonBodyParser from '@middy/http-json-body-parser';
import { middyfy } from './lambda';

jest.mock('@middy/core', () => {
const use = jest.fn().mockImplementation(() => 'middyfied handler');
return jest.fn().mockImplementation(() => ({ use }));
});
jest.mock('@middy/http-json-body-parser');

const mockMiddy = middy as jest.MockedFunction<typeof middy>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For top-level scope constants, the naming can be MOCK_MIDDY


describe('middyfy', () => {
it('should wrap handler with middy, apply JSON body parser, and return middy instance', () => {
const handler = jest.fn();
const middyfiedHandler = middyfy(handler);
expect(middy).toHaveBeenCalledWith(handler);
const mockMiddyInstance = mockMiddy.mock.results[0].value;
expect(mockMiddyInstance.use).toHaveBeenCalledWith(middyJsonBodyParser());
expect(middyfiedHandler).toBe('middyfied handler');
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"removeComments": true,
"sourceMap": true,
"target": "ES2020",
"outDir": "lib"
"outDir": "lib",
"esModuleInterop": true,
},
"include": ["src/**/*.ts", "serverless.ts"],
"exclude": [
Expand Down