test(@nestjs/microservices) fix failing unit tests

This commit is contained in:
Kamil Myśliwiec
2018-08-16 14:07:37 +02:00
parent d446a56381
commit 62e733c018
4 changed files with 53 additions and 68 deletions

View File

@@ -54,12 +54,12 @@ describe('ClientMqtt', () => {
});
it('should subscribe to response pattern name', async () => {
await client['publish'](msg, () => {});
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
expect(subscribeSpy.calledWith(`${pattern}_res`)).to.be.true;
});
it('should publish stringified message to acknowledge pattern name', async () => {
await client['publish'](msg, () => {});
expect(publishSpy.calledWith(`"${pattern}"_ack`, JSON.stringify(msg))).to
.be.true;
expect(publishSpy.calledWith(`${pattern}_ack`, JSON.stringify(msg))).to.be
.true;
});
it('should listen on messages', async () => {
await client['publish'](msg, () => {});
@@ -68,9 +68,11 @@ describe('ClientMqtt', () => {
describe('on error', () => {
let assignPacketIdStub: sinon.SinonStub;
beforeEach(() => {
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
throw new Error();
});
assignPacketIdStub = sinon
.stub(client, 'assignPacketId')
.callsFake(() => {
throw new Error();
});
});
afterEach(() => {
assignPacketIdStub.restore();
@@ -146,10 +148,15 @@ describe('ClientMqtt', () => {
beforeEach(async () => {
callback = sinon.spy();
subscription = client.createResponseCallback(msg, callback);
subscription('channel', new Buffer(JSON.stringify({
...responseMessage,
isDisposed: true,
})));
subscription(
'channel',
new Buffer(
JSON.stringify({
...responseMessage,
isDisposed: true,
}),
),
);
});
it('should call callback with dispose param', () => {
@@ -166,10 +173,13 @@ describe('ClientMqtt', () => {
describe('disposed and "id" is incorrect', () => {
beforeEach(async () => {
callback = sinon.spy();
subscription = client.createResponseCallback({
...msg,
id: '2',
}, callback);
subscription = client.createResponseCallback(
{
...msg,
id: '2',
},
callback,
);
subscription('channel', new Buffer(JSON.stringify(responseMessage)));
});

View File

@@ -5,21 +5,8 @@ import { ERROR_EVENT } from '../../constants';
// tslint:disable:no-string-literal
describe('ClientNats', () => {
const test = 'test';
const client = new ClientNats({});
describe('getAckPatternName', () => {
it(`should append _ack to string`, () => {
const expectedResult = test + '_ack';
expect(client.getAckPatternName(test)).to.equal(expectedResult);
});
});
describe('getResPatternName', () => {
it(`should append _res to string`, () => {
const expectedResult = test + '_res';
expect(client.getResPatternName(test)).to.equal(expectedResult);
});
});
describe('publish', () => {
const pattern = 'test';
const msg = { pattern, data: 'data' };
@@ -30,6 +17,7 @@ describe('ClientNats', () => {
publishSpy: sinon.SinonSpy,
onSpy: sinon.SinonSpy,
removeListenerSpy: sinon.SinonSpy,
requestSpy: sinon.SinonSpy,
unsubscribeSpy: sinon.SinonSpy,
connectSpy: sinon.SinonStub,
natsClient,
@@ -41,6 +29,7 @@ describe('ClientNats', () => {
onSpy = sinon.spy();
removeListenerSpy = sinon.spy();
unsubscribeSpy = sinon.spy();
requestSpy = sinon.spy(() => subscriptionId);
natsClient = {
subscribe: subscribeSpy,
@@ -49,6 +38,7 @@ describe('ClientNats', () => {
unsubscribe: unsubscribeSpy,
addListener: () => ({}),
publish: publishSpy,
request: requestSpy,
};
(client as any).natsClient = natsClient;
@@ -61,20 +51,18 @@ describe('ClientNats', () => {
connectSpy.restore();
createClient.restore();
});
it('should subscribe to response pattern name', async () => {
it('should publish stringified message to pattern name', async () => {
await client['publish'](msg, () => {});
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
});
it('should publish stringified message to acknowledge pattern name', async () => {
await client['publish'](msg, () => {});
expect(publishSpy.getCall(0).args[0]).to.be.eql(`"${pattern}"_ack`);
expect(requestSpy.getCall(0).args[0]).to.be.eql(pattern);
});
describe('on error', () => {
let assignPacketIdStub: sinon.SinonStub;
beforeEach(() => {
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
throw new Error();
});
assignPacketIdStub = sinon
.stub(client, 'assignPacketId')
.callsFake(() => {
throw new Error();
});
});
afterEach(() => {
assignPacketIdStub.restore();

View File

@@ -57,12 +57,12 @@ describe('ClientRedis', () => {
});
it('should subscribe to response pattern name', () => {
client['publish'](msg, () => {});
expect(subscribeSpy.calledWith(`"${pattern}"_res`)).to.be.true;
expect(subscribeSpy.calledWith(`${pattern}_res`)).to.be.true;
});
it('should publish stringified message to acknowledge pattern name', async () => {
await client['publish'](msg, () => {});
expect(publishSpy.calledWith(`"${pattern}"_ack`, JSON.stringify(msg))).to
.be.true;
expect(publishSpy.calledWith(`${pattern}_ack`, JSON.stringify(msg))).to.be
.true;
});
it('should listen on messages', () => {
client['publish'](msg, () => {});
@@ -71,9 +71,11 @@ describe('ClientRedis', () => {
describe('on error', () => {
let assignPacketIdStub: sinon.SinonStub;
beforeEach(() => {
assignPacketIdStub = sinon.stub(client, 'assignPacketId').callsFake(() => {
throw new Error();
});
assignPacketIdStub = sinon
.stub(client, 'assignPacketId')
.callsFake(() => {
throw new Error();
});
});
afterEach(() => {
assignPacketIdStub.restore();

View File

@@ -1,8 +1,7 @@
import * as sinon from 'sinon';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { NO_PATTERN_MESSAGE } from '../../constants';
import { ServerNats } from '../../server/server-nats';
import { Observable } from 'rxjs';
describe('ServerNats', () => {
let server: ServerNats;
@@ -60,9 +59,7 @@ describe('ServerNats', () => {
[pattern]: handler,
};
server.bindEvents(natsClient);
const expectedPattern = 'test_ack';
expect(subscribeSpy.calledWith(expectedPattern)).to.be.true;
expect(subscribeSpy.calledWith(pattern)).to.be.true;
});
});
describe('getMessageHandler', () => {
@@ -76,7 +73,10 @@ describe('ServerNats', () => {
const handleMessageStub = sinon
.stub(server, 'handleMessage')
.callsFake(() => null);
(await server.getMessageHandler('', (server as any).natsClient))('');
(await server.getMessageHandler('', (server as any).natsClient))(
'',
'',
);
expect(handleMessageStub.called).to.be.true;
});
});
@@ -93,7 +93,7 @@ describe('ServerNats', () => {
sinon.stub(server, 'getPublisher').callsFake(() => getPublisherSpy);
});
it(`should publish NO_PATTERN_MESSAGE if pattern not exists in messageHandlers object`, () => {
server.handleMessage(channel, { id, pattern: '', data: '' }, null);
server.handleMessage(channel, { id, pattern: '', data: '' }, null, '');
expect(
getPublisherSpy.calledWith({
id,
@@ -108,7 +108,7 @@ describe('ServerNats', () => {
[channel]: handler,
};
server.handleMessage(channel, { pattern: '', data, id: '2' }, null);
server.handleMessage(channel, { pattern: '', data, id: '2' }, null, '');
expect(handler.calledWith(data)).to.be.true;
});
});
@@ -117,14 +117,14 @@ describe('ServerNats', () => {
let pub, publisher;
const id = '1';
const pattern = 'test';
const replyTo = 'test';
beforeEach(() => {
publisherSpy = sinon.spy();
pub = {
publish: publisherSpy,
};
publisher = server.getPublisher(pub, pattern, id);
publisher = server.getPublisher(pub, replyTo, id);
});
it(`should return function`, () => {
expect(typeof server.getPublisher(null, null, id)).to.be.eql('function');
@@ -132,22 +132,7 @@ describe('ServerNats', () => {
it(`should call "publish" with expected arguments`, () => {
const respond = 'test';
publisher({ respond, id });
expect(publisherSpy.calledWith(`${pattern}_res`, { respond, id })).to.be
.true;
});
});
describe('getAckPatternName', () => {
const test = 'test';
it(`should append _ack to string`, () => {
const expectedResult = test + '_ack';
expect(server.getAckQueueName(test)).to.equal(expectedResult);
});
});
describe('getResPatternName', () => {
const test = 'test';
it(`should append _res to string`, () => {
const expectedResult = test + '_res';
expect(server.getResQueueName(test)).to.equal(expectedResult);
expect(publisherSpy.calledWith(replyTo, { respond, id })).to.be.true;
});
});
});