Skip to content

Commit 62e733c

Browse files
test(@nestjs/microservices) fix failing unit tests
1 parent d446a56 commit 62e733c

4 files changed

Lines changed: 53 additions & 68 deletions

File tree

packages/microservices/test/client/client-mqtt.spec.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ describe('ClientMqtt', () => {
5454
});
5555
it('should subscribe to response pattern name', async () => {
5656
await client['publish'](msg, () => {});
57-
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
57+
expect(subscribeSpy.calledWith(`${pattern}_res`)).to.be.true;
5858
});
5959
it('should publish stringified message to acknowledge pattern name', async () => {
6060
await client['publish'](msg, () => {});
61-
expect(publishSpy.calledWith(`"${pattern}"_ack`, JSON.stringify(msg))).to
62-
.be.true;
61+
expect(publishSpy.calledWith(`${pattern}_ack`, JSON.stringify(msg))).to.be
62+
.true;
6363
});
6464
it('should listen on messages', async () => {
6565
await client['publish'](msg, () => {});
@@ -68,9 +68,11 @@ describe('ClientMqtt', () => {
6868
describe('on error', () => {
6969
let assignPacketIdStub: sinon.SinonStub;
7070
beforeEach(() => {
71-
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
72-
throw new Error();
73-
});
71+
assignPacketIdStub = sinon
72+
.stub(client, 'assignPacketId')
73+
.callsFake(() => {
74+
throw new Error();
75+
});
7476
});
7577
afterEach(() => {
7678
assignPacketIdStub.restore();
@@ -146,10 +148,15 @@ describe('ClientMqtt', () => {
146148
beforeEach(async () => {
147149
callback = sinon.spy();
148150
subscription = client.createResponseCallback(msg, callback);
149-
subscription('channel', new Buffer(JSON.stringify({
150-
...responseMessage,
151-
isDisposed: true,
152-
})));
151+
subscription(
152+
'channel',
153+
new Buffer(
154+
JSON.stringify({
155+
...responseMessage,
156+
isDisposed: true,
157+
}),
158+
),
159+
);
153160
});
154161

155162
it('should call callback with dispose param', () => {
@@ -166,10 +173,13 @@ describe('ClientMqtt', () => {
166173
describe('disposed and "id" is incorrect', () => {
167174
beforeEach(async () => {
168175
callback = sinon.spy();
169-
subscription = client.createResponseCallback({
170-
...msg,
171-
id: '2',
172-
}, callback);
176+
subscription = client.createResponseCallback(
177+
{
178+
...msg,
179+
id: '2',
180+
},
181+
callback,
182+
);
173183
subscription('channel', new Buffer(JSON.stringify(responseMessage)));
174184
});
175185

packages/microservices/test/client/client-nats.spec.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,8 @@ import { ERROR_EVENT } from '../../constants';
55
// tslint:disable:no-string-literal
66

77
describe('ClientNats', () => {
8-
const test = 'test';
98
const client = new ClientNats({});
109

11-
describe('getAckPatternName', () => {
12-
it(`should append _ack to string`, () => {
13-
const expectedResult = test + '_ack';
14-
expect(client.getAckPatternName(test)).to.equal(expectedResult);
15-
});
16-
});
17-
describe('getResPatternName', () => {
18-
it(`should append _res to string`, () => {
19-
const expectedResult = test + '_res';
20-
expect(client.getResPatternName(test)).to.equal(expectedResult);
21-
});
22-
});
2310
describe('publish', () => {
2411
const pattern = 'test';
2512
const msg = { pattern, data: 'data' };
@@ -30,6 +17,7 @@ describe('ClientNats', () => {
3017
publishSpy: sinon.SinonSpy,
3118
onSpy: sinon.SinonSpy,
3219
removeListenerSpy: sinon.SinonSpy,
20+
requestSpy: sinon.SinonSpy,
3321
unsubscribeSpy: sinon.SinonSpy,
3422
connectSpy: sinon.SinonStub,
3523
natsClient,
@@ -41,6 +29,7 @@ describe('ClientNats', () => {
4129
onSpy = sinon.spy();
4230
removeListenerSpy = sinon.spy();
4331
unsubscribeSpy = sinon.spy();
32+
requestSpy = sinon.spy(() => subscriptionId);
4433

4534
natsClient = {
4635
subscribe: subscribeSpy,
@@ -49,6 +38,7 @@ describe('ClientNats', () => {
4938
unsubscribe: unsubscribeSpy,
5039
addListener: () => ({}),
5140
publish: publishSpy,
41+
request: requestSpy,
5242
};
5343
(client as any).natsClient = natsClient;
5444

@@ -61,20 +51,18 @@ describe('ClientNats', () => {
6151
connectSpy.restore();
6252
createClient.restore();
6353
});
64-
it('should subscribe to response pattern name', async () => {
54+
it('should publish stringified message to pattern name', async () => {
6555
await client['publish'](msg, () => {});
66-
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
67-
});
68-
it('should publish stringified message to acknowledge pattern name', async () => {
69-
await client['publish'](msg, () => {});
70-
expect(publishSpy.getCall(0).args[0]).to.be.eql(`"${pattern}"_ack`);
56+
expect(requestSpy.getCall(0).args[0]).to.be.eql(pattern);
7157
});
7258
describe('on error', () => {
7359
let assignPacketIdStub: sinon.SinonStub;
7460
beforeEach(() => {
75-
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
76-
throw new Error();
77-
});
61+
assignPacketIdStub = sinon
62+
.stub(client, 'assignPacketId')
63+
.callsFake(() => {
64+
throw new Error();
65+
});
7866
});
7967
afterEach(() => {
8068
assignPacketIdStub.restore();

packages/microservices/test/client/client-redis.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ describe('ClientRedis', () => {
5757
});
5858
it('should subscribe to response pattern name', () => {
5959
client['publish'](msg, () => {});
60-
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
60+
expect(subscribeSpy.calledWith(`${pattern}_res`)).to.be.true;
6161
});
6262
it('should publish stringified message to acknowledge pattern name', async () => {
6363
await client['publish'](msg, () => {});
64-
expect(publishSpy.calledWith(`"${pattern}"_ack`, JSON.stringify(msg))).to
65-
.be.true;
64+
expect(publishSpy.calledWith(`${pattern}_ack`, JSON.stringify(msg))).to.be
65+
.true;
6666
});
6767
it('should listen on messages', () => {
6868
client['publish'](msg, () => {});
@@ -71,9 +71,11 @@ describe('ClientRedis', () => {
7171
describe('on error', () => {
7272
let assignPacketIdStub: sinon.SinonStub;
7373
beforeEach(() => {
74-
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
75-
throw new Error();
76-
});
74+
assignPacketIdStub = sinon
75+
.stub(client, 'assignPacketId')
76+
.callsFake(() => {
77+
throw new Error();
78+
});
7779
});
7880
afterEach(() => {
7981
assignPacketIdStub.restore();

packages/microservices/test/server/server-nats.spec.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as sinon from 'sinon';
21
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
33
import { NO_PATTERN_MESSAGE } from '../../constants';
44
import { ServerNats } from '../../server/server-nats';
5-
import { Observable } from 'rxjs';
65

76
describe('ServerNats', () => {
87
let server: ServerNats;
@@ -60,9 +59,7 @@ describe('ServerNats', () => {
6059
[pattern]: handler,
6160
};
6261
server.bindEvents(natsClient);
63-
64-
const expectedPattern = 'test_ack';
65-
expect(subscribeSpy.calledWith(expectedPattern)).to.be.true;
62+
expect(subscribeSpy.calledWith(pattern)).to.be.true;
6663
});
6764
});
6865
describe('getMessageHandler', () => {
@@ -76,7 +73,10 @@ describe('ServerNats', () => {
7673
const handleMessageStub = sinon
7774
.stub(server, 'handleMessage')
7875
.callsFake(() => null);
79-
(await server.getMessageHandler('', (server as any).natsClient))('');
76+
(await server.getMessageHandler('', (server as any).natsClient))(
77+
'',
78+
'',
79+
);
8080
expect(handleMessageStub.called).to.be.true;
8181
});
8282
});
@@ -93,7 +93,7 @@ describe('ServerNats', () => {
9393
sinon.stub(server, 'getPublisher').callsFake(() => getPublisherSpy);
9494
});
9595
it(`should publish NO_PATTERN_MESSAGE if pattern not exists in messageHandlers object`, () => {
96-
server.handleMessage(channel, { id, pattern: '', data: '' }, null);
96+
server.handleMessage(channel, { id, pattern: '', data: '' }, null, '');
9797
expect(
9898
getPublisherSpy.calledWith({
9999
id,
@@ -108,7 +108,7 @@ describe('ServerNats', () => {
108108
[channel]: handler,
109109
};
110110

111-
server.handleMessage(channel, { pattern: '', data, id: '2' }, null);
111+
server.handleMessage(channel, { pattern: '', data, id: '2' }, null, '');
112112
expect(handler.calledWith(data)).to.be.true;
113113
});
114114
});
@@ -117,37 +117,22 @@ describe('ServerNats', () => {
117117
let pub, publisher;
118118

119119
const id = '1';
120-
const pattern = 'test';
120+
const replyTo = 'test';
121121

122122
beforeEach(() => {
123123
publisherSpy = sinon.spy();
124124
pub = {
125125
publish: publisherSpy,
126126
};
127-
publisher = server.getPublisher(pub, pattern, id);
127+
publisher = server.getPublisher(pub, replyTo, id);
128128
});
129129
it(`should return function`, () => {
130130
expect(typeof server.getPublisher(null, null, id)).to.be.eql('function');
131131
});
132132
it(`should call "publish" with expected arguments`, () => {
133133
const respond = 'test';
134134
publisher({ respond, id });
135-
expect(publisherSpy.calledWith(`${pattern}_res`, { respond, id })).to.be
136-
.true;
137-
});
138-
});
139-
describe('getAckPatternName', () => {
140-
const test = 'test';
141-
it(`should append _ack to string`, () => {
142-
const expectedResult = test + '_ack';
143-
expect(server.getAckQueueName(test)).to.equal(expectedResult);
144-
});
145-
});
146-
describe('getResPatternName', () => {
147-
const test = 'test';
148-
it(`should append _res to string`, () => {
149-
const expectedResult = test + '_res';
150-
expect(server.getResQueueName(test)).to.equal(expectedResult);
135+
expect(publisherSpy.calledWith(replyTo, { respond, id })).to.be.true;
151136
});
152137
});
153138
});

0 commit comments

Comments
 (0)