test: update outdated unit tests

This commit is contained in:
Kamil Myśliwiec
2025-07-14 13:28:01 +02:00
parent adc7e1547f
commit 4d436f0c2d
8 changed files with 47 additions and 19 deletions

View File

@@ -156,6 +156,10 @@ export abstract class AbstractHttpAdapter<
return this.onRouteTriggered;
}
public setOnRequestHook(onRequestHook: Function): void {}
public setOnResponseHook(onResponseHook: Function): void {}
abstract close();
abstract initHttpServer(options: NestApplicationOptions);
abstract useStaticAssets(...args: any[]);
@@ -187,6 +191,4 @@ export abstract class AbstractHttpAdapter<
version: VersionValue,
versioningOptions: VersioningOptions,
): (req: TRequest, res: TResponse, next: () => void) => Function;
abstract setOnRequestHook?(onRequestHook: Function): void;
abstract setOnResponseHook?(onResponseHook: Function): void;
}

View File

@@ -1,11 +1,11 @@
import { ApplicationConfig } from '@nestjs/core/application-config';
import { GraphInspector } from '@nestjs/core/inspector/graph-inspector';
import { Transport } from '@nestjs/microservices/enums';
import { AsyncMicroserviceOptions } from '@nestjs/microservices/interfaces';
import { NestMicroservice } from '@nestjs/microservices/nest-microservice';
import { Server, ServerTCP } from '@nestjs/microservices/server';
import { GraphInspector } from '@nestjs/core/inspector/graph-inspector';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { Transport } from '@nestjs/microservices/enums';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { AsyncMicroserviceOptions } from '@nestjs/microservices/interfaces';
const createMockGraphInspector = (): GraphInspector =>
({
@@ -23,7 +23,10 @@ const createMockAppConfig = (): ApplicationConfig =>
const mockContainer = {
getModuleCompiler: sinon.stub(),
getModules: () => new Map(),
getModules: () =>
Object.assign(new Map(), {
addRpcTarget: sinon.spy(),
}),
get: () => null,
getHttpAdapterHost: () => undefined,
} as any;

View File

@@ -2,6 +2,7 @@ import { Logger } from '@nestjs/common';
import { AssertionError, expect } from 'chai';
import * as sinon from 'sinon';
import { NO_MESSAGE_HANDLER } from '../../constants';
import { KafkaContext } from '../../ctx-host';
import { KafkaHeaders } from '../../enums';
import {
EachMessagePayload,
@@ -273,6 +274,7 @@ describe('ServerKafka', () => {
});
describe('getPublisher', () => {
const context = new KafkaContext([] as any);
let sendMessageStub: sinon.SinonStub;
let publisher;
@@ -281,15 +283,16 @@ describe('ServerKafka', () => {
replyTopic,
replyPartition,
correlationId,
context,
);
sendMessageStub = sinon
.stub(server, 'sendMessage')
.callsFake(async () => []);
});
it(`should return function`, () => {
expect(typeof server.getPublisher(null!, null!, correlationId)).to.be.eql(
'function',
);
expect(
typeof server.getPublisher(null!, null!, correlationId, context),
).to.be.eql('function');
});
it(`should call "publish" with expected arguments`, () => {
const data = {
@@ -411,10 +414,11 @@ describe('ServerKafka', () => {
});
describe('sendMessage', () => {
const context = new KafkaContext([] as any);
let sendSpy: sinon.SinonSpy;
beforeEach(() => {
sendSpy = sinon.spy();
sendSpy = sinon.stub().callsFake(() => Promise.resolve());
sinon.stub(server as any, 'producer').value({
send: sendSpy,
});
@@ -429,6 +433,7 @@ describe('ServerKafka', () => {
replyTopic,
replyPartition,
correlationId,
context,
);
expect(
@@ -455,6 +460,7 @@ describe('ServerKafka', () => {
replyTopic,
undefined,
correlationId,
context,
);
expect(
@@ -480,6 +486,7 @@ describe('ServerKafka', () => {
replyTopic,
replyPartition,
correlationId,
context,
);
expect(
@@ -507,6 +514,7 @@ describe('ServerKafka', () => {
replyTopic,
replyPartition,
correlationId,
context,
);
expect(

View File

@@ -1,6 +1,7 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { NO_MESSAGE_HANDLER } from '../../constants';
import { MqttContext } from '../../ctx-host';
import { BaseRpcContext } from '../../ctx-host/base-rpc.context';
import { ServerMqtt } from '../../server/server-mqtt';
import { objectToMap } from './utils/object-to-map';
@@ -167,16 +168,19 @@ describe('ServerMqtt', () => {
const id = '1';
const pattern = 'test';
const context = new MqttContext([pattern, {}]);
beforeEach(() => {
publisherSpy = sinon.spy();
pub = {
publish: publisherSpy,
};
publisher = server.getPublisher(pub, pattern, id);
publisher = server.getPublisher(pub, context, id);
});
it(`should return function`, () => {
expect(typeof server.getPublisher(null, null, id)).to.be.eql('function');
expect(typeof server.getPublisher(null, context, id)).to.be.eql(
'function',
);
});
it(`should call "publish" with expected arguments`, () => {
const respond = 'test';

View File

@@ -222,6 +222,7 @@ describe('ServerNats', () => {
});
});
describe('getPublisher', () => {
const context = new NatsContext([] as any);
const id = '1';
it(`should return function`, () => {
@@ -231,7 +232,9 @@ describe('ServerNats', () => {
sid: +id,
respond: sinon.spy(),
};
expect(typeof server.getPublisher(natsMsg, id)).to.be.eql('function');
expect(typeof server.getPublisher(natsMsg, id, context)).to.be.eql(
'function',
);
});
it(`should call "respond" when reply topic provided`, () => {
const replyTo = 'test';
@@ -242,7 +245,7 @@ describe('ServerNats', () => {
respond: sinon.spy(),
reply: replyTo,
};
const publisher = server.getPublisher(natsMsg, id);
const publisher = server.getPublisher(natsMsg, id, context);
const respond = 'test';
publisher({ respond, id });
@@ -258,7 +261,7 @@ describe('ServerNats', () => {
sid: +id,
respond: sinon.spy(),
};
const publisher = server.getPublisher(natsMsg, id);
const publisher = server.getPublisher(natsMsg, id, context);
const respond = 'test';
publisher({ respond, id });

View File

@@ -1,6 +1,7 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { NO_MESSAGE_HANDLER } from '../../constants';
import { RedisContext } from '../../ctx-host';
import { BaseRpcContext } from '../../ctx-host/base-rpc.context';
import { ServerRedis } from '../../server/server-redis';
import { objectToMap } from './utils/object-to-map';
@@ -172,16 +173,19 @@ describe('ServerRedis', () => {
const id = '1';
const pattern = 'test';
const context = new RedisContext([] as any);
beforeEach(() => {
publisherSpy = sinon.spy();
pub = {
publish: publisherSpy,
};
publisher = server.getPublisher(pub, pattern, id);
publisher = server.getPublisher(pub, pattern, id, context);
});
it(`should return function`, () => {
expect(typeof server.getPublisher(null, null, id)).to.be.eql('function');
expect(typeof server.getPublisher(null, null, id, context)).to.be.eql(
'function',
);
});
it(`should call "publish" with expected arguments`, () => {
const respond = 'test';

View File

@@ -231,6 +231,8 @@ describe('ServerRMQ', () => {
});
describe('sendMessage', () => {
const context = new RmqContext([] as any);
let channel: any;
beforeEach(() => {
@@ -245,7 +247,7 @@ describe('ServerRMQ', () => {
const replyTo = 'test';
const correlationId = '0';
server.sendMessage(message, replyTo, correlationId);
server.sendMessage(message, replyTo, correlationId, context);
expect(
channel.sendToQueue.calledWith(
replyTo,

View File

@@ -19,6 +19,7 @@ describe('ExpressAdapter', () => {
.returns(urlencodedInstance);
const useSpy = sinon.spy(expressInstance, 'use');
const expressAdapter = new ExpressAdapter(expressInstance);
useSpy.resetHistory();
expressAdapter.registerParserMiddleware();
@@ -37,6 +38,7 @@ describe('ExpressAdapter', () => {
expressInstance.use(function urlencodedParser() {});
const useSpy = sinon.spy(expressInstance, 'use');
const expressAdapter = new ExpressAdapter(expressInstance);
useSpy.resetHistory();
expressAdapter.registerParserMiddleware();