Skip to content

Commit 976d95e

Browse files
committed
Fix question tests
1 parent 654f45a commit 976d95e

File tree

4 files changed

+308
-315
lines changed

4 files changed

+308
-315
lines changed

services/question/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
"version": "0.0.0",
44
"main": "server.js",
55
"scripts": {
6-
"clean": "rm -rf dist",
7-
"build": "npm run clean && npx tsc --build src/tsconfig.json",
6+
"build": "npx tsc",
87
"start": "npm run build && node ./dist/server.js",
98
"dev": "nodemon --files ./src/server.ts",
109
"lint": "npx eslint .",
1110
"lint:fix": "npx eslint . --fix",
12-
"test": "mocha --require ts-node/register tests/**/*.spec.ts --exit"
11+
"test": "node --env-file=.env.sample ./node_modules/mocha/bin/mocha --require ts-node/register tests/**/*.spec.ts --exit"
1312
},
1413
"author": "",
1514
"license": "ISC",
Lines changed: 136 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,136 @@
1-
// import dotenv from 'dotenv';
2-
// dotenv.config({ path: '../../.env.sample' });
3-
// import { expect } from 'chai';
4-
// import sinon, { SinonStub } from 'sinon';
5-
// import client, { Channel, Connection } from 'amqplib';
6-
// import messageBroker from '../../src/events/broker';
7-
// import config from '../../src/config';
8-
9-
// describe('MessageBroker', () => {
10-
// let connectStub: SinonStub;
11-
// let assertQueueStub: SinonStub;
12-
// let sendToQueueStub: SinonStub;
13-
// let consumeStub: SinonStub;
14-
// let ackStub: SinonStub;
15-
// let connection: Connection;
16-
// let channel: Channel;
17-
18-
// beforeEach(() => {
19-
// connection = {
20-
// createChannel: sinon.stub(),
21-
// close: sinon.stub(),
22-
// } as unknown as Connection;
23-
24-
// channel = {
25-
// assertQueue: sinon.stub(),
26-
// sendToQueue: sinon.stub(),
27-
// consume: sinon.stub(),
28-
// ack: sinon.stub(),
29-
// } as unknown as Channel;
30-
31-
// connectStub = sinon.stub(client, 'connect').resolves(connection);
32-
// (connection.createChannel as SinonStub).resolves(channel);
33-
// assertQueueStub = channel.assertQueue as SinonStub;
34-
// sendToQueueStub = channel.sendToQueue as SinonStub;
35-
// consumeStub = channel.consume as SinonStub;
36-
// ackStub = channel.ack as SinonStub;
37-
38-
// messageBroker['connected'] = false;
39-
// messageBroker['connection'] = undefined;
40-
// messageBroker['channel'] = undefined;
41-
// });
42-
43-
// afterEach(() => {
44-
// sinon.restore();
45-
// });
46-
47-
// describe('connect', () => {
48-
// it('should connect to RabbitMQ', async () => {
49-
// await messageBroker.connect();
50-
51-
// expect(connectStub).to.have.been.calledWith(config.BROKER_URL);
52-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
53-
// expect(connection.createChannel).to.have.been.called;
54-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
55-
// expect(messageBroker['connected']).to.be.true;
56-
// });
57-
58-
// it('should not reconnect if already connected', async () => {
59-
// messageBroker['connection'] = connection;
60-
// messageBroker['channel'] = channel;
61-
// messageBroker['connected'] = true;
62-
63-
// await messageBroker.connect();
64-
65-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
66-
// expect(connectStub).not.to.have.been.called;
67-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
68-
// expect(connection.createChannel).not.to.have.been.called;
69-
// });
70-
71-
// it('should throw an error if connection fails', async () => {
72-
// connectStub.rejects(new Error('Connection error'));
73-
74-
// try {
75-
// await messageBroker.connect();
76-
// } catch (error) {
77-
// if (error instanceof Error) {
78-
// expect(error.message).to.equal('Connection error');
79-
// }
80-
// }
81-
// });
82-
// });
83-
84-
// describe('produce', () => {
85-
// it('should produce a message to the queue', async () => {
86-
// await messageBroker.produce('testQueue', { test: 'message' });
87-
88-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
89-
// expect(connectStub).to.have.been.called;
90-
// expect(sendToQueueStub).to.have.been.calledWith(
91-
// 'testQueue',
92-
// Buffer.from(JSON.stringify({ test: 'message' })),
93-
// );
94-
// });
95-
96-
// it('should throw an error if producing message fails', async () => {
97-
// sendToQueueStub.rejects(new Error('Produce error'));
98-
99-
// try {
100-
// await messageBroker.produce('testQueue', { test: 'message' });
101-
// } catch (error) {
102-
// if (error instanceof Error) {
103-
// expect(error.message).to.equal('Produce error');
104-
// }
105-
// }
106-
// });
107-
// });
108-
109-
// describe('consume', () => {
110-
// it('should consume messages from the queue', async () => {
111-
// const onMessage = sinon.stub();
112-
// const msg = { content: Buffer.from(JSON.stringify({ test: 'message' })) };
113-
114-
// consumeStub.yields(msg);
115-
116-
// await messageBroker.consume('testQueue', onMessage);
117-
118-
// // eslint-disable-next-line @typescript-eslint/no-unused-expressions
119-
// expect(connectStub).to.have.been.called;
120-
// expect(assertQueueStub).to.have.been.calledWith('testQueue', { durable: true });
121-
// expect(consumeStub).to.have.been.calledWith('testQueue', sinon.match.func, { noAck: false });
122-
// expect(onMessage).to.have.been.calledWith({ test: 'message' });
123-
// expect(ackStub).to.have.been.calledWith(msg);
124-
// });
125-
126-
// it('should throw an error if consuming message fails', async () => {
127-
// consumeStub.rejects(new Error('Consume error'));
128-
129-
// try {
130-
// await messageBroker.consume('testQueue', sinon.stub());
131-
// } catch (error) {
132-
// if (error instanceof Error) {
133-
// expect(error.message).to.equal('Consume error');
134-
// }
135-
// }
136-
// });
137-
// });
138-
// });
1+
import { expect } from 'chai';
2+
import sinon, { SinonStub } from 'sinon';
3+
import client, { Channel, Connection } from 'amqplib';
4+
import messageBroker from '../../src/events/broker';
5+
import config from '../../src/config';
6+
7+
describe('MessageBroker', () => {
8+
let connectStub: SinonStub;
9+
let assertQueueStub: SinonStub;
10+
let sendToQueueStub: SinonStub;
11+
let consumeStub: SinonStub;
12+
let ackStub: SinonStub;
13+
let connection: Connection;
14+
let channel: Channel;
15+
16+
beforeEach(() => {
17+
connection = {
18+
createChannel: sinon.stub(),
19+
close: sinon.stub(),
20+
} as unknown as Connection;
21+
22+
channel = {
23+
assertQueue: sinon.stub(),
24+
sendToQueue: sinon.stub(),
25+
consume: sinon.stub(),
26+
ack: sinon.stub(),
27+
} as unknown as Channel;
28+
29+
connectStub = sinon.stub(client, 'connect').resolves(connection);
30+
(connection.createChannel as SinonStub).resolves(channel);
31+
assertQueueStub = channel.assertQueue as SinonStub;
32+
sendToQueueStub = channel.sendToQueue as SinonStub;
33+
consumeStub = channel.consume as SinonStub;
34+
ackStub = channel.ack as SinonStub;
35+
36+
messageBroker['connected'] = false;
37+
messageBroker['connection'] = undefined;
38+
messageBroker['channel'] = undefined;
39+
});
40+
41+
afterEach(() => {
42+
sinon.restore();
43+
});
44+
45+
describe('connect', () => {
46+
it('should connect to RabbitMQ', async () => {
47+
await messageBroker.connect();
48+
49+
expect(connectStub).to.have.been.calledWith(config.BROKER_URL);
50+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
51+
expect(connection.createChannel).to.have.been.called;
52+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
53+
expect(messageBroker['connected']).to.be.true;
54+
});
55+
56+
it('should not reconnect if already connected', async () => {
57+
messageBroker['connection'] = connection;
58+
messageBroker['channel'] = channel;
59+
messageBroker['connected'] = true;
60+
61+
await messageBroker.connect();
62+
63+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
64+
expect(connectStub).not.to.have.been.called;
65+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
66+
expect(connection.createChannel).not.to.have.been.called;
67+
});
68+
69+
it('should throw an error if connection fails', async () => {
70+
connectStub.rejects(new Error('Connection error'));
71+
72+
try {
73+
await messageBroker.connect();
74+
} catch (error) {
75+
if (error instanceof Error) {
76+
expect(error.message).to.equal('Connection error');
77+
}
78+
}
79+
});
80+
});
81+
82+
describe('produce', () => {
83+
it('should produce a message to the queue', async () => {
84+
await messageBroker.produce('testQueue', { test: 'message' });
85+
86+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
87+
expect(connectStub).to.have.been.called;
88+
expect(sendToQueueStub).to.have.been.calledWith(
89+
'testQueue',
90+
Buffer.from(JSON.stringify({ test: 'message' })),
91+
);
92+
});
93+
94+
it('should throw an error if producing message fails', async () => {
95+
sendToQueueStub.rejects(new Error('Produce error'));
96+
97+
try {
98+
await messageBroker.produce('testQueue', { test: 'message' });
99+
} catch (error) {
100+
if (error instanceof Error) {
101+
expect(error.message).to.equal('Produce error');
102+
}
103+
}
104+
});
105+
});
106+
107+
describe('consume', () => {
108+
it('should consume messages from the queue', async () => {
109+
const onMessage = sinon.stub();
110+
const msg = { content: Buffer.from(JSON.stringify({ test: 'message' })) };
111+
112+
consumeStub.yields(msg);
113+
114+
await messageBroker.consume('testQueue', onMessage);
115+
116+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
117+
expect(connectStub).to.have.been.called;
118+
expect(assertQueueStub).to.have.been.calledWith('testQueue', { durable: true });
119+
expect(consumeStub).to.have.been.calledWith('testQueue', sinon.match.func, { noAck: false });
120+
expect(onMessage).to.have.been.calledWith({ test: 'message' });
121+
expect(ackStub).to.have.been.calledWith(msg);
122+
});
123+
124+
it('should throw an error if consuming message fails', async () => {
125+
consumeStub.rejects(new Error('Consume error'));
126+
127+
try {
128+
await messageBroker.consume('testQueue', sinon.stub());
129+
} catch (error) {
130+
if (error instanceof Error) {
131+
expect(error.message).to.equal('Consume error');
132+
}
133+
}
134+
});
135+
});
136+
});

0 commit comments

Comments
 (0)