Skip to content

Commit

Permalink
feat: handle all other stuffs that does not match with defined routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimanzurrashid committed Oct 5, 2022
1 parent 6f407d2 commit 9791365
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 73 deletions.
18 changes: 18 additions & 0 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,22 @@ describe('app', () => {
);
expect(router).toBeDefined();
});

it('handles all other stuffs', () => {
const route = app._router.stack.find(
(x) => x.name === 'bound dispatch' && x.route.path === '*'
);

expect(route).toBeDefined();

const mockedJson = jest.fn();
const mockedStatus = jest.fn(() => ({ json: mockedJson }));

route.handle({ method: 'GET' }, { status: mockedStatus }, () => {
return;
});

expect(mockedStatus).toHaveBeenCalledWith(404);
expect(mockedJson).toHaveBeenCalled();
});
});
9 changes: 7 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Express } from 'express';
import type { Express, Response } from 'express';
import express from 'express';
import type { Logger } from 'pino';
import { container } from 'tsyringe';
Expand All @@ -25,5 +25,10 @@ export default function createApp(): Express {
.use('/users', userRouter(container.resolve(UsersController)))
.use('/events', eventsRouter(container.resolve(EventsController)))
.use('/health', healthRouter(container.resolve(HealthController)))
.use('/', openApiRouter());
.use('/', openApiRouter())
.all('*', (_, res: Response) => {
res.status(404).json({
errors: ['Resource not found']
});
});
}
190 changes: 119 additions & 71 deletions src/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,19 @@ describe('integrations', () => {
let statusCode: number;
let result: IErrorsResult;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const id = faker.datatype.uuid();
const response = await request(app).get(`/users/${id}`);
server = app.listen(async () => {
const id = faker.datatype.uuid();
const response = await request(app).get(`/users/${id}`);

statusCode = response.statusCode;
result = response.body;
statusCode = response.statusCode;
result = response.body;

done();
done();
});
});
});

Expand All @@ -308,39 +310,41 @@ describe('integrations', () => {
let user: IUser;
let statusCode: number;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
let response: Response;
server = app.listen(async () => {
let response: Response;

response = await request(app).post('/users').send({
email: faker.internet.email().toLowerCase()
});
response = await request(app).post('/users').send({
email: faker.internet.email().toLowerCase()
});

user = response.body;

response = await request(app)
.post('/events')
.send({
user: {
id: user.id
},
consents: [
{
id: consents.email,
enabled: true
user = response.body;

response = await request(app)
.post('/events')
.send({
user: {
id: user.id
},
{
id: consents.sms,
enabled: false
}
]
});
consents: [
{
id: consents.email,
enabled: true
},
{
id: consents.sms,
enabled: false
}
]
});

statusCode = response.statusCode;
statusCode = response.statusCode;

done();
done();
});
});
});

Expand All @@ -366,27 +370,29 @@ describe('integrations', () => {

let statusCode: number;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const response = await request(app)
.post('/events')
.send({
user: {
id: faker.datatype.uuid()
},
consents: [
{
id: consents.email,
enabled: true
}
]
});
server = app.listen(async () => {
const response = await request(app)
.post('/events')
.send({
user: {
id: faker.datatype.uuid()
},
consents: [
{
id: consents.email,
enabled: true
}
]
});

statusCode = response.statusCode;
statusCode = response.statusCode;

done();
done();
});
});
});

Expand All @@ -408,15 +414,17 @@ describe('integrations', () => {

let statusCode: number;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const response = await request(app).get('/health');
server = app.listen(async () => {
const response = await request(app).get('/health');

statusCode = response.statusCode;
statusCode = response.statusCode;

done();
done();
});
});
});

Expand All @@ -436,15 +444,17 @@ describe('integrations', () => {

let statusCode: number;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const response = await request(app).get('/health');
server = app.listen(async () => {
const response = await request(app).get('/health');

statusCode = response.statusCode;
statusCode = response.statusCode;

done();
done();
});
});
});

Expand All @@ -465,15 +475,17 @@ describe('integrations', () => {

let statusCode: number;

beforeAll((done) => {
const app = createApp();
beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const response = await request(app).get('/');
server = app.listen(async () => {
const response = await request(app).get('/');

statusCode = response.statusCode;
statusCode = response.statusCode;

done();
done();
});
});
});

Expand All @@ -488,6 +500,42 @@ describe('integrations', () => {
});
});

describe('ALL OTHER STUFFS', () => {
let server: Server;

let statusCode: number;
let result: IErrorsResult;

beforeAll(async () => {
return new Promise<void>((done) => {
const app = createApp();

server = app.listen(async () => {
const response = await request(app).get('/foo-bar');

statusCode = response.statusCode;
result = response.body;

done();
});
});
});

it('responds with http status code 404', () => {
expect(statusCode).toEqual(404);
});

it('returns error', () => {
expect(result.errors).toHaveLength(1);
});

afterAll(async () => {
return new Promise<void>((done) => {
server.close(() => done());
});
});
});

afterAll(async () => {
await container.resolve<Pool>('PGPool').end();
await postgresContainer.stop();
Expand Down

0 comments on commit 9791365

Please sign in to comment.