From 50138ceb028b9e97a00aa1d5f38fd95c456a4c29 Mon Sep 17 00:00:00 2001 From: Jasmine Date: Wed, 21 Feb 2024 16:58:36 -0500 Subject: [PATCH 1/4] set esModuleInterop to true, --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 896f946..b2a9bd5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,8 @@ "removeComments": true, "sourceMap": true, "target": "ES2020", - "outDir": "lib" + "outDir": "lib", + "esModuleInterop": true, }, "include": ["src/**/*.ts", "serverless.ts"], "exclude": [ From b5af5c0974031b4508dd63eb486f1f76e03681d1 Mon Sep 17 00:00:00 2001 From: Jasmine Date: Wed, 21 Feb 2024 16:58:47 -0500 Subject: [PATCH 2/4] lambda.ts test --- src/libs/lambda.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/libs/lambda.test.ts diff --git a/src/libs/lambda.test.ts b/src/libs/lambda.test.ts new file mode 100644 index 0000000..856abb7 --- /dev/null +++ b/src/libs/lambda.test.ts @@ -0,0 +1,24 @@ +import middy from '@middy/core'; +import middyJsonBodyParser from '@middy/http-json-body-parser'; +import { middyfy } from './lambda'; + +jest.mock('@middy/core', () => { + return jest.fn().mockImplementation(() => { + return { use: jest.fn() }; + }); +}); + +jest.mock('@middy/http-json-body-parser'); + +const mockJsonBodyParser = middyJsonBodyParser as jest.Mock; +const mockMiddy = middy as jest.Mocked; + +describe('middyfy', () => { + it('should wrap handler with middy and applies the JSON body parser', () => { + const handler = jest.fn(); + middyfy(handler); + expect(mockMiddy).toHaveBeenCalledWith(handler); + expect(mockMiddy).toHaveBeenCalledTimes(1); + expect(mockJsonBodyParser).toHaveBeenCalledTimes(1); + }); +}); From 4ab6718125abe39db3ed40a57f26a9886e67f252 Mon Sep 17 00:00:00 2001 From: Jasmine Date: Thu, 22 Feb 2024 15:05:50 -0500 Subject: [PATCH 3/4] lambda.ts test --- src/libs/lambda.test.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/libs/lambda.test.ts b/src/libs/lambda.test.ts index 856abb7..669511c 100644 --- a/src/libs/lambda.test.ts +++ b/src/libs/lambda.test.ts @@ -3,22 +3,20 @@ import middyJsonBodyParser from '@middy/http-json-body-parser'; import { middyfy } from './lambda'; jest.mock('@middy/core', () => { - return jest.fn().mockImplementation(() => { - return { use: jest.fn() }; - }); + const use = jest.fn().mockImplementation(() => 'middyfied handler'); + return jest.fn().mockImplementation(() => ({ use })); }); - jest.mock('@middy/http-json-body-parser'); -const mockJsonBodyParser = middyJsonBodyParser as jest.Mock; -const mockMiddy = middy as jest.Mocked; +const mockMiddy = middy as jest.MockedFunction; describe('middyfy', () => { - it('should wrap handler with middy and applies the JSON body parser', () => { + it('should wrap handler with middy, apply JSON body parser, and return middy instance', () => { const handler = jest.fn(); - middyfy(handler); - expect(mockMiddy).toHaveBeenCalledWith(handler); - expect(mockMiddy).toHaveBeenCalledTimes(1); - expect(mockJsonBodyParser).toHaveBeenCalledTimes(1); + 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'); }); }); From 7af5886d6afa1e71d89e74938c5adb2a1fe60c66 Mon Sep 17 00:00:00 2001 From: Jasmine Date: Thu, 22 Feb 2024 18:25:07 -0500 Subject: [PATCH 4/4] updated constant name --- src/libs/lambda.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/lambda.test.ts b/src/libs/lambda.test.ts index 669511c..b0a1ea5 100644 --- a/src/libs/lambda.test.ts +++ b/src/libs/lambda.test.ts @@ -8,14 +8,14 @@ jest.mock('@middy/core', () => { }); jest.mock('@middy/http-json-body-parser'); -const mockMiddy = middy as jest.MockedFunction; +const MOCK_MIDDY = middy as jest.MockedFunction; 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; + const mockMiddyInstance = MOCK_MIDDY.mock.results[0].value; expect(mockMiddyInstance.use).toHaveBeenCalledWith(middyJsonBodyParser()); expect(middyfiedHandler).toBe('middyfied handler'); });