test(microservices): add unit tests (populate errors by servers)

This commit is contained in:
Kamil Myśliwiec
2021-01-28 14:35:02 +01:00
parent 8779a44402
commit f8bc434bf4
8 changed files with 101 additions and 20 deletions

View File

@@ -31,7 +31,7 @@ export class ClientProxyFactory {
): ClientGrpcProxy;
public static create(clientOptions: ClientOptions): ClientProxy & Closeable;
public static create(clientOptions: ClientOptions): ClientProxy & Closeable {
const { transport, options } = clientOptions;
const { transport, options } = clientOptions || {};
switch (transport) {
case Transport.REDIS:
return new ClientRedis(options as RedisOptions['options']);

View File

@@ -68,7 +68,11 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
public async listen(
callback: (err?: unknown, ...optionalParams: unknown[]) => void,
): Promise<void> {
await this.start(callback);
try {
await this.start(callback);
} catch (err) {
callback(err);
}
}
public close(): void {

View File

@@ -61,6 +61,20 @@ describe('ServerGrpc', () => {
server.close();
expect(callback.called).to.be.true;
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', async () => {
const error = new Error('random error');
const callbackSpy = sinon.spy();
sinon.stub(server, 'createClient').callsFake(async () => null);
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
await server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('listen (multiple proto)', () => {

View File

@@ -126,6 +126,18 @@ describe('ServerKafka', () => {
await server.listen(callback);
expect(callback.called).to.be.true;
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
const callbackSpy = sinon.spy();
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('close', () => {

View File

@@ -14,30 +14,41 @@ describe('ServerMqtt', () => {
server = new ServerMqtt({});
});
describe('listen', () => {
let createMqttClient;
let onSpy: sinon.SinonSpy;
let client;
let client: any;
let callbackSpy: sinon.SinonSpy;
beforeEach(() => {
onSpy = sinon.spy();
client = {
on: onSpy,
};
createMqttClient = sinon
.stub(server, 'createMqttClient')
.callsFake(() => client);
server.listen(null);
sinon.stub(server, 'createMqttClient').callsFake(() => client);
callbackSpy = sinon.spy();
});
it('should bind "error" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(0).args[0]).to.be.equal('error');
});
it('should bind "message" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(1).args[0]).to.be.equal('message');
});
it('should bind "connect" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(2).args[0]).to.be.equal('connect');
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('close', () => {
const mqttClient = { end: sinon.spy() };

View File

@@ -16,7 +16,8 @@ describe('ServerNats', () => {
});
describe('listen', () => {
let onSpy: sinon.SinonSpy;
let client;
let client: any;
let callbackSpy: sinon.SinonSpy;
beforeEach(() => {
onSpy = sinon.spy();
@@ -25,15 +26,27 @@ describe('ServerNats', () => {
once: sinon.spy(),
};
sinon.stub(server, 'createNatsClient').callsFake(() => client);
server.listen(err => null);
callbackSpy = sinon.spy();
});
it('should bind "error" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(0).args[0]).to.be.equal('error');
});
it('should bind "connect" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(1).args[0]).to.be.equal('connect');
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('close', () => {
const natsClient = { close: sinon.spy() };

View File

@@ -14,30 +14,43 @@ describe('ServerRedis', () => {
server = new ServerRedis({});
});
describe('listen', () => {
let createRedisClient;
let onSpy: sinon.SinonSpy;
let client;
let client: any;
let callbackSpy: sinon.SinonSpy;
beforeEach(() => {
onSpy = sinon.spy();
client = {
on: onSpy,
};
createRedisClient = sinon
.stub(server, 'createRedisClient')
.callsFake(() => client);
sinon.stub(server, 'createRedisClient').callsFake(() => client);
server.listen(null);
callbackSpy = sinon.spy();
});
it('should bind "error" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(0).args[0]).to.be.equal('error');
});
it('should bind "connect" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(3).args[0]).to.be.equal('connect');
});
it('should bind "message" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(2).args[0]).to.be.equal('message');
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
const callbackSpy = sinon.spy();
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('close', () => {
const pub = { quit: sinon.spy() };

View File

@@ -19,6 +19,7 @@ describe('ServerRMQ', () => {
let createChannelStub: sinon.SinonStub;
let setupChannelStub: sinon.SinonStub;
let client: any;
let callbackSpy: sinon.SinonSpy;
beforeEach(() => {
onStub = sinon
@@ -34,21 +35,34 @@ describe('ServerRMQ', () => {
createChannel: createChannelStub,
};
createClient = sinon.stub(server, 'createClient').callsFake(() => client);
server.listen(null);
callbackSpy = sinon.spy();
});
afterEach(() => {
setupChannelStub.restore();
});
it('should call "createClient"', () => {
server.listen(callbackSpy);
expect(createClient.called).to.be.true;
});
it('should bind "connect" event to handler', () => {
server.listen(callbackSpy);
expect(onStub.getCall(0).args[0]).to.be.equal('connect');
});
it('should bind "disconnect" event to handler', () => {
server.listen(callbackSpy);
expect(onStub.getCall(1).args[0]).to.be.equal('disconnect');
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
sinon.stub(server, 'start').callsFake(() => {
throw error;
});
server.listen(callbackSpy);
expect(callbackSpy.calledWith(error)).to.be.true;
});
});
});
describe('close', () => {
const rmqServer = { close: sinon.spy() };