Skip to content

Commit 815d1d6

Browse files
committed
fix(tests): cover routes in debug, metrics endpoints
1 parent 2a62978 commit 815d1d6

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

test/endpoint/TestDebugEndpoint.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import { NextFunction, Request, Response, Router } from 'express';
33
import { ineeda } from 'ineeda';
44
import passport from 'passport';
5-
import { spy } from 'sinon';
5+
import { match, spy } from 'sinon';
66
import { Repository } from 'typeorm';
77

88
import { Bot } from '../../src/Bot';
@@ -39,6 +39,14 @@ async function createEndpoint(botReady: boolean, storageReady: boolean): Promise
3939
});
4040
}
4141

42+
function createRequest() {
43+
const json = spy();
44+
const response = ineeda<Response>({
45+
json,
46+
});
47+
return { json, response };
48+
}
49+
4250
// tslint:disable:no-identical-functions
4351
describeLeaks('debug endpoint', async () => {
4452
itLeaks('should have paths', async () => {
@@ -66,4 +74,13 @@ describeLeaks('debug endpoint', async () => {
6674
expect(result).to.equal(router, 'must return the passed router');
6775
expect(get).to.have.callCount(1);
6876
});
77+
78+
describeLeaks('index route', async () => {
79+
itLeaks('should return services', async () => {
80+
const endpoint = await createEndpoint(true, true);
81+
const { json, response } = createRequest();
82+
await endpoint.getIndex(ineeda<Request>({}), response);
83+
expect(json).to.have.been.calledOnce.and.calledWithMatch(match.array);
84+
});
85+
});
6986
});

test/endpoint/TestMetricsEndpoint.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { expect } from 'chai';
2+
import { Request, Response, Router } from 'express';
3+
import { ineeda } from 'ineeda';
4+
import passport from 'passport';
5+
import { match, spy } from 'sinon';
6+
import { Repository } from 'typeorm';
7+
8+
import { Bot } from '../../src/Bot';
9+
import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService';
10+
import { MetricsEndpoint } from '../../src/endpoint/MetricsEndpoint';
11+
import { BotModule } from '../../src/module/BotModule';
12+
import { Storage } from '../../src/storage';
13+
import { describeLeaks, itLeaks } from '../helpers/async';
14+
import { createService, createServiceContainer } from '../helpers/container';
15+
import { getTestLogger } from '../helpers/logger';
16+
17+
async function createEndpoint(botReady: boolean, storageReady: boolean): Promise<MetricsEndpoint> {
18+
const storage = ineeda<Storage>({
19+
getRepository() {
20+
return ineeda<Repository<{}>>();
21+
},
22+
});
23+
const bot = ineeda<Bot>({
24+
getStorage() {
25+
return storage;
26+
},
27+
});
28+
29+
const { container, module } = await createServiceContainer(new BotModule({
30+
logger: getTestLogger(),
31+
}));
32+
module.bind(INJECT_BOT).toInstance(bot);
33+
module.bind(INJECT_STORAGE).toInstance(storage);
34+
35+
return createService(container, MetricsEndpoint, {
36+
data: {
37+
filters: [],
38+
strict: false,
39+
},
40+
metadata: {
41+
kind: 'metrics-endpoint',
42+
name: 'test-endpoint',
43+
},
44+
});
45+
}
46+
47+
function createRequest() {
48+
const end = spy();
49+
const set = spy();
50+
const response = ineeda<Response>({
51+
end,
52+
set,
53+
});
54+
return { end, response, set };
55+
}
56+
57+
// tslint:disable:no-identical-functions
58+
describeLeaks('metrics endpoint', async () => {
59+
itLeaks('should have paths', async () => {
60+
const endpoint = await createEndpoint(false, false);
61+
expect(endpoint.paths.length).to.equal(3);
62+
expect(endpoint.paths).to.include('/metrics');
63+
});
64+
65+
itLeaks('should configure a router', async () => {
66+
const endpoint = await createEndpoint(false, false);
67+
const get = spy();
68+
const router = ineeda<Router>({
69+
get,
70+
});
71+
const result = await endpoint.createRouter({
72+
passport: ineeda<passport.Authenticator>(),
73+
router,
74+
});
75+
expect(result).to.equal(router, 'must return the passed router');
76+
expect(get).to.have.callCount(1);
77+
});
78+
79+
describeLeaks('index route', async () => {
80+
itLeaks('should return metrics', async () => {
81+
const endpoint = await createEndpoint(true, true);
82+
const { end, response } = createRequest();
83+
await endpoint.getIndex(ineeda<Request>({}), response);
84+
expect(end).to.have.been.calledOnce.and.calledWithMatch(match.string);
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)