From a605d65cee4743210987e4a64bd8e302648cd992 Mon Sep 17 00:00:00 2001 From: praveenkumarct Date: Wed, 25 Sep 2024 11:07:21 +0200 Subject: [PATCH] fix(template tests): small fixes --- .../event/src/middleware/error.middleware.js | 2 +- .../event/tests/integration/routes.spec.js | 12 ++++++++---- .../service/tests/integration/routes.spec.js | 13 ++++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/application-templates/javascript/event/src/middleware/error.middleware.js b/application-templates/javascript/event/src/middleware/error.middleware.js index 1a3d13f..8a43b45 100644 --- a/application-templates/javascript/event/src/middleware/error.middleware.js +++ b/application-templates/javascript/event/src/middleware/error.middleware.js @@ -17,7 +17,7 @@ export const errorMiddleware = (error, _req, res, _next) => { .status(500) .send( isDevelopment - ? { messge: error.message } + ? { message: error.message } : { message: 'Internal server error' } ); }; diff --git a/application-templates/javascript/event/tests/integration/routes.spec.js b/application-templates/javascript/event/tests/integration/routes.spec.js index a772b1d..3e96371 100644 --- a/application-templates/javascript/event/tests/integration/routes.spec.js +++ b/application-templates/javascript/event/tests/integration/routes.spec.js @@ -16,6 +16,7 @@ describe('Testing router', () => { message: 'Path not found.', }); }); + test('Post invalid body', async () => { const response = await request(app).post('/event').send({ message: 'hello world', @@ -25,6 +26,7 @@ describe('Testing router', () => { message: 'Bad request: No customer id in the Pub/Sub message', }); }); + test('Post empty body', async () => { const response = await request(app).post('/event'); expect(response.status).toBe(400); @@ -33,6 +35,7 @@ describe('Testing router', () => { }); }); }); + describe('unexpected error', () => { let postMock; @@ -44,14 +47,15 @@ describe('unexpected error', () => { readConfiguration.mockClear(); }); - afterEach(() => { - // Restore the original implementation - postMock.mockRestore(); - }); test('should handle errors thrown by post method', async () => { // Call the route handler const response = await request(app).post('/event'); expect(response.status).toBe(500); expect(response.body).toEqual({ message: 'Internal server error' }); }); + + afterEach(() => { + // Restore the original implementation + postMock.mockRestore(); + }); }); diff --git a/application-templates/javascript/service/tests/integration/routes.spec.js b/application-templates/javascript/service/tests/integration/routes.spec.js index 6c01c88..17683b3 100644 --- a/application-templates/javascript/service/tests/integration/routes.spec.js +++ b/application-templates/javascript/service/tests/integration/routes.spec.js @@ -16,6 +16,7 @@ describe('Testing router', () => { message: 'Path not found.', }); }); + test('Post invalid body', async () => { const response = await request(app).post('/service').send({ message: 'hello world', @@ -25,6 +26,7 @@ describe('Testing router', () => { message: 'Bad request - Missing body parameters.', }); }); + test('Post empty body', async () => { const response = await request(app).post('/service'); expect(response.status).toBe(400); @@ -33,6 +35,7 @@ describe('Testing router', () => { }); }); }); + describe('unexpected error', () => { let postMock; @@ -41,17 +44,17 @@ describe('unexpected error', () => { postMock = jest.spyOn(serviceController, 'post').mockImplementation(() => { throw new Error('Test error'); }); - readConfiguration.mockClear(); }); - afterEach(() => { - // Restore the original implementation - postMock.mockRestore(); - }); test('should handle errors thrown by post method', async () => { // Call the route handler const response = await request(app).post('/service'); expect(response.status).toBe(500); expect(response.body).toEqual({ message: 'Internal server error' }); }); + + afterEach(() => { + // Restore the original implementation + postMock.mockRestore(); + }); });