forked from goldbergyoni/nodejs-testing-best-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake-message-queue-provider.js
43 lines (35 loc) · 1.26 KB
/
fake-message-queue-provider.js
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
const { EventEmitter } = require('events');
// This class is the heart of the MQ testing - It replaces the MQ provider client library
// and implement the same signature, but each method does nothing but emit an event which the test
// can verify that indeed happened
class FakeMessageQueueProvider extends EventEmitter {
async consume(queueName, messageHandler) {
this.messageHandler = messageHandler;
Promise.resolve();
}
async publish(exchangeName, routingKey, newMessage) {
if (this.messageHandler) {
this.messageHandler({ content: newMessage });
this.emit('publish', { exchangeName, routingKey, newMessage });
}
Promise.resolve();
}
async nack() {
const eventDescription = { event: 'message-rejected' };
this.emit('message-rejected', eventDescription); // Multiple events allows the test to filter for the relevant event
this.emit('message-handled', eventDescription);
}
async ack() {
const eventDescription = { event: 'message-acknowledged' };
this.emit('message-acknowledged', eventDescription);
this.emit('message-handled', eventDescription);
}
async assertQueue() {}
async createChannel() {
return this;
}
async connect() {
return this;
}
}
module.exports = { FakeMessageQueueProvider };