Newer
Older

Douglas Lagemann
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const request = require('supertest');
const app = require('./app');
const { getHealth } = require('./controller/controller');
jest.mock('./controller/controller', () => ({
getHealth: jest.fn().mockImplementation(async (req, res) => {
res.send('got health');
}),
getUser: jest.fn().mockImplementation(async (req, res) => {
res.send('got user');
}),
getVersion: jest.fn().mockImplementation(async (req, res) => {
res.send("got version");
})
}));
beforeEach(() => {
console.log = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('App routes', () => {
it('responds with hello world at the /api route', async () => {
const response = await request(app).get('/api');
expect(response.status).toEqual(200);
expect(response.text).toEqual('Hello World! at specific path /api');
});
it('calls the getHealth controller method at the /api/health route', async () => {
const response = await request(app).get('/api/health');
expect(response.text).toEqual('got health');
});
it('calls the getUser controller method at the /api/me route', async () => {
const response = await request(app).get('/api/me');
expect(response.text).toEqual('got user');
});
it('calls the getVersion controller method at the /api/version route', async () => {
const response = await request(app).get('/api/version');
expect(response.text).toEqual('got version');
});
it('responds with 404 at unknown routes', async () => {
const response = await request(app).get('/api/nope');
expect(response.status).toEqual(404);
expect(response.text).toEqual('Not found: /api/nope');
});
it('responds with 500 and a generic error message when an error is thrown', async () => {
getHealth.mockImplementation(() => {
throw new Error("Boom!");
});
console.error = jest.fn();
const response = await request(app).get('/api/health');
expect(response.status).toEqual(500);
expect(response.text).toEqual('Internal Server Error');
expect(console.error).toHaveBeenCalled();
})
});