-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(templates): will failing test fail ci * fix(templates): whitespace prettier * fix(templates): test routes * fix(templates): mock config.utils * fix(templates): file name and location consistancy * fix(templates): remove yarn lock * fix(template tests): small fixes --------- Co-authored-by: praveenkumarct <[email protected]>
- Loading branch information
1 parent
5010723
commit e12d8a7
Showing
43 changed files
with
620 additions
and
210 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dotenv/config'; | ||
|
||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
// Import routes | ||
import EventRoutes from './routes/event.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define configurations | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// Define routes | ||
app.use('/event', EventRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
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
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
7 changes: 7 additions & 0 deletions
7
application-templates/javascript/event/src/utils/__mocks__/config.utils.js
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const readConfiguration = jest.fn(() => ({ | ||
clientId: 'mockedClientId', | ||
clientSecret: 'mockedClientSecret', | ||
projectKey: 'mockedProjectKey', | ||
scope: 'mockedScope', | ||
region: 'mockedRegion', | ||
})); |
62 changes: 57 additions & 5 deletions
62
application-templates/javascript/event/tests/integration/routes.spec.js
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 |
---|---|---|
@@ -1,9 +1,61 @@ | ||
import { expect } from '@jest/globals'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
import * as eventController from '../../src/controllers/event.controller'; | ||
import { readConfiguration } from '../../src/utils/config.utils'; | ||
|
||
//@todo: test this with msw (see typescipt job) | ||
// make msw a shared package | ||
describe('Testing Event Controller', () => { | ||
test('POST `/event` route', async () => { | ||
expect(1).toBe(1); | ||
jest.mock('../../src/utils/config.utils'); | ||
describe('Testing router', () => { | ||
beforeEach(() => { | ||
readConfiguration.mockClear(); | ||
}); | ||
test('Post to non existing route', async () => { | ||
const response = await request(app).post('/none'); | ||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ | ||
message: 'Path not found.', | ||
}); | ||
}); | ||
|
||
test('Post invalid body', async () => { | ||
const response = await request(app).post('/event').send({ | ||
message: 'hello world', | ||
}); | ||
expect(response.status).toBe(400); | ||
expect(response.body).toEqual({ | ||
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); | ||
expect(response.body).toEqual({ | ||
message: 'Bad request: Wrong No Pub/Sub message format', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('unexpected error', () => { | ||
let postMock; | ||
|
||
beforeEach(() => { | ||
// Mock the post method to throw an error | ||
postMock = jest.spyOn(eventController, 'post').mockImplementation(() => { | ||
throw new Error('Test error'); | ||
}); | ||
readConfiguration.mockClear(); | ||
}); | ||
|
||
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(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
import express from 'express'; | ||
|
||
// Import routes | ||
import JobRoutes from './routes/job.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define routes | ||
app.use('/job', JobRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
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
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
7 changes: 7 additions & 0 deletions
7
application-templates/javascript/job/src/utils/__mocks__/config.utils.js
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const readConfiguration = jest.fn(() => ({ | ||
clientId: 'mockedClientId', | ||
clientSecret: 'mockedClientSecret', | ||
projectKey: 'mockedProjectKey', | ||
scope: 'mockedScope', | ||
region: 'mockedRegion', | ||
})); |
7 changes: 0 additions & 7 deletions
7
application-templates/javascript/job/tests/integration/orders.spec.js
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
application-templates/javascript/job/tests/integration/routes.spec.js
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from '@jest/globals'; | ||
import request from 'supertest'; | ||
import app from '../../src/app'; | ||
import * as jobController from '../../src/controllers/job.controller'; | ||
import { readConfiguration } from '../../src/utils/config.utils'; | ||
|
||
jest.mock('../../src/utils/config.utils'); | ||
describe('Testing router', () => { | ||
beforeEach(() => { | ||
readConfiguration.mockClear(); | ||
}); | ||
test('Post to non existing route', async () => { | ||
const response = await request(app).post('/none'); | ||
expect(response.status).toBe(404); | ||
expect(response.body).toEqual({ | ||
message: 'Path not found.', | ||
}); | ||
}); | ||
}); | ||
describe('unexpected error', () => { | ||
let postMock; | ||
|
||
beforeEach(() => { | ||
// Mock the post method to throw an error | ||
postMock = jest.spyOn(jobController, '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('/job'); | ||
expect(response.status).toBe(500); | ||
expect(response.body).toEqual({ message: 'Internal server error' }); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'dotenv/config'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
|
||
// Import routes | ||
import ServiceRoutes from './routes/service.route.js'; | ||
|
||
import { readConfiguration } from './utils/config.utils.js'; | ||
import { errorMiddleware } from './middleware/error.middleware.js'; | ||
import CustomError from './errors/custom.error.js'; | ||
|
||
// Read env variables | ||
readConfiguration(); | ||
|
||
// Create the express app | ||
const app = express(); | ||
app.disable('x-powered-by'); | ||
|
||
// Define configurations | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
// Define routes | ||
app.use('/service', ServiceRoutes); | ||
app.use('*', () => { | ||
throw new CustomError(404, 'Path not found.'); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorMiddleware); | ||
|
||
export default app; |
Oops, something went wrong.