Skip to content

fix(microservices): support custom strategy in async usefactory config #15172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/microservices/nest-microservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,29 @@ export class NestMicroservice
public createServer(config: CompleteMicroserviceOptions) {
try {
if ('useFactory' in config) {
this.microserviceConfig = this.resolveAsyncOptions(config);
const resolvedConfig = this.resolveAsyncOptions(config);
this.microserviceConfig = resolvedConfig;

// Inject custom strategy
if ('strategy' in resolvedConfig) {
this.serverInstance = resolvedConfig.strategy as Server;
return;
}
} else {
this.microserviceConfig = {
transport: Transport.TCP,
...config,
} as MicroserviceOptions;
}

if ('strategy' in config) {
this.serverInstance = config.strategy as Server;
return;
} else {
this.serverInstance = ServerFactory.create(
this.microserviceConfig,
) as Server;
if ('strategy' in config) {
this.serverInstance = config.strategy as Server;
return;
}
}

this.serverInstance = ServerFactory.create(
this.microserviceConfig,
) as Server;
} catch (e) {
this.logger.error(e);
throw e;
Expand Down
154 changes: 154 additions & 0 deletions packages/microservices/test/nest-microservice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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 =>
({
insertOrphanedEnhancer: sinon.stub(),
}) as unknown as GraphInspector;

const createMockAppConfig = (): ApplicationConfig =>
({
useGlobalFilters: sinon.stub(),
useGlobalPipes: sinon.stub(),
useGlobalGuards: sinon.stub(),
useGlobalInterceptors: sinon.stub(),
setIoAdapter: sinon.stub(),
}) as unknown as ApplicationConfig;

const mockContainer = {
getModuleCompiler: sinon.stub(),
getModules: () => new Map(),
get: () => null,
getHttpAdapterHost: () => undefined,
} as any;

describe('NestMicroservice', () => {
let mockGraphInspector: GraphInspector;
let mockAppConfig: ApplicationConfig;

beforeEach(() => {
mockGraphInspector = createMockGraphInspector();
mockAppConfig = createMockAppConfig();
});

afterEach(() => {
sinon.restore();
});

it('should use ServerFactory if no strategy is provided', () => {
const instance = new NestMicroservice(
mockContainer,
{ transport: Transport.TCP },
mockGraphInspector,
mockAppConfig,
);

expect((instance as any).serverInstance).to.be.instanceOf(ServerTCP);
});

it('should use provided strategy if present in config', () => {
const strategy = new (class extends Server {
listen = sinon.spy();
close = sinon.spy();
on = sinon.stub();
unwrap = sinon.stub();
})();

const instance = new NestMicroservice(
mockContainer,
{ strategy },
mockGraphInspector,
mockAppConfig,
);

expect((instance as any).serverInstance).to.equal(strategy);
});

it('should use strategy resolved from useFactory config', () => {
const strategy = new (class extends Server {
listen = sinon.spy();
close = sinon.spy();
on = sinon.stub();
unwrap = sinon.stub();
})();
const asyncConfig: AsyncMicroserviceOptions = {
useFactory: () => ({ strategy }),
inject: [],
};

const instance = new NestMicroservice(
mockContainer,
asyncConfig,
mockGraphInspector,
mockAppConfig,
);

expect((instance as any).serverInstance).to.equal(strategy);
});

it('should call listen() on server when listen() is called', async () => {
const listenSpy = sinon.spy((cb: () => void) => cb());
const strategy = new (class extends Server {
listen = listenSpy;
close = sinon.spy();
on = sinon.stub();
unwrap = sinon.stub();
})();

const instance = new NestMicroservice(
mockContainer,
{ strategy },
mockGraphInspector,
mockAppConfig,
);

await instance.listen();
expect(listenSpy.calledOnce).to.be.true;
});

it('should delegate unwrap() to server', () => {
const unwrapStub = sinon.stub().returns('core');
const strategy = new (class extends Server {
listen = sinon.spy();
close = sinon.spy();
on = sinon.stub();
unwrap = unwrapStub;
})();

const instance = new NestMicroservice(
mockContainer,
{ strategy },
mockGraphInspector,
mockAppConfig,
);

expect(instance.unwrap()).to.equal('core');
});

it('should delegate on() to server', () => {
const onStub = sinon.stub();
const strategy = new (class extends Server {
listen = sinon.spy();
close = sinon.spy();
on = onStub;
unwrap = sinon.stub();
})();

const instance = new NestMicroservice(
mockContainer,
{ strategy },
mockGraphInspector,
mockAppConfig,
);

const cb = () => {};
instance.on('test:event', cb);
expect(onStub.calledWith('test:event', cb)).to.be.true;
});
});
Loading