Skip to content

Commit 8aff546

Browse files
tests(@nestjs/core) increase injector test-cov
1 parent 45ad3d1 commit 8aff546

4 files changed

Lines changed: 77 additions & 15 deletions

File tree

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
],
1616
"exclude": [
1717
"node_modules/",
18+
"src/**/*.spec.ts",
19+
"src/core/adapters/*.ts",
20+
"src/websockets/adapters/*.ts",
1821
"src/core/nest-application.ts",
1922
"src/core/nest-factory.ts",
2023
"src/core/nest-microservice.ts",
21-
"src/**/*.spec.ts",
2224
"src/common/services/logger.service.ts",
2325
"src/core/errors/exceptions",
2426
"src/microservices/exceptions/",
2527
"src/microservices/microservices-module.ts",
26-
"src/core/adapters/*.ts",
27-
"src/websockets/adapters/*.ts"
28+
"src/core/middlewares/middlewares-module.ts"
2829
],
2930
"extension": [
3031
".ts"

src/core/injector/injector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Injector {
8383
);
8484
} else {
8585
const factoryResult = currentMetatype.metatype(...instances);
86-
currentMetatype.instance = await this.resolveFactoryInstance(currentMetatype, factoryResult);
86+
currentMetatype.instance = await this.resolveFactoryInstance(factoryResult);
8787
}
8888
currentMetatype.isResolved = true;
8989
done();
@@ -216,7 +216,7 @@ export class Injector {
216216
return component;
217217
}
218218

219-
public async resolveFactoryInstance(currentMetatype, factoryResult): Promise<any> {
219+
public async resolveFactoryInstance(factoryResult): Promise<any> {
220220
if (!(factoryResult instanceof Promise)) {
221221
return factoryResult;
222222
}

src/core/test/injector/injector.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ describe('Injector', () => {
8181
).to.eventually.be.rejected;
8282
});
8383

84+
it('should await done$ when "isPending"', async () => {
85+
const value = 'test';
86+
const result = await injector.loadInstance({
87+
name: 'MainTest',
88+
metatype: MainTest,
89+
instance: Object.create(MainTest.prototype),
90+
isResolved: false,
91+
isPending: true,
92+
done$: Promise.resolve(value) as any,
93+
}, moduleDeps.components, moduleDeps);
94+
expect(result).to.be.eql(value);
95+
});
8496
});
8597

8698
describe('loadPrototypeOfInstance', () => {
@@ -112,6 +124,7 @@ describe('Injector', () => {
112124
injector.loadPrototypeOfInstance(test, moduleDeps.components);
113125
expect(moduleDeps.components.get('Test')).to.deep.equal(expectedResult);
114126
});
127+
115128
});
116129

117130
describe('resolveSingleParam', () => {
@@ -155,6 +168,53 @@ describe('Injector', () => {
155168
});
156169
});
157170

171+
describe('loadInstanceOfRoute', () => {
172+
let loadInstance: sinon.SinonSpy;
173+
174+
beforeEach(() => {
175+
loadInstance = sinon.spy();
176+
injector.loadInstance = loadInstance;
177+
});
178+
179+
it('should call "loadInstance" with expected arguments', async () => {
180+
const module = { routes: [] };
181+
const wrapper = { test: 'test' };
182+
183+
await injector.loadInstanceOfRoute(wrapper as any, module as any);
184+
expect(loadInstance.calledWith(wrapper, module.routes, module)).to.be.true;
185+
});
186+
});
187+
188+
describe('loadInstanceOfInjectable', () => {
189+
let loadInstance: sinon.SinonSpy;
190+
191+
beforeEach(() => {
192+
loadInstance = sinon.spy();
193+
injector.loadInstance = loadInstance;
194+
});
195+
196+
it('should call "loadInstance" with expected arguments', async () => {
197+
const module = { injectables: [] };
198+
const wrapper = { test: 'test' };
199+
200+
await injector.loadInstanceOfInjectable(wrapper as any, module as any);
201+
expect(loadInstance.calledWith(wrapper, module.injectables, module)).to.be.true;
202+
});
203+
});
204+
205+
describe('resolveFactoryInstance', () => {
206+
it('should resolve deffered value', async () => {
207+
const wrapper = { test: 'test' };
208+
const result = await injector.resolveFactoryInstance(Promise.resolve(wrapper));
209+
expect(result).to.be.eql(wrapper);
210+
});
211+
it('should return exact same value', async () => {
212+
const wrapper = { test: 'test' };
213+
const result = await injector.resolveFactoryInstance(wrapper);
214+
expect(result).to.be.eql(wrapper);
215+
});
216+
});
217+
158218
describe('scanForComponent', () => {
159219
let scanForComponentInRelatedModules: sinon.SinonStub;
160220
const metatype = { name: 'test', metatype: { name: 'test' }};

src/microservices/microservices-module.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ export class MicroservicesModule {
2020
private static listenersController: ListenersController;
2121

2222
public static setup(container, config) {
23+
const contextCreator = new RpcContextCreator(
24+
new RpcProxy(),
25+
new ExceptionFiltersContext(config),
26+
new PipesContextCreator(config),
27+
new PipesConsumer(),
28+
new GuardsContextCreator(container, config),
29+
new GuardsConsumer(),
30+
new InterceptorsContextCreator(container, config),
31+
new InterceptorsConsumer(),
32+
);
2333
this.listenersController = new ListenersController(
2434
MicroservicesModule.clientsContainer,
25-
new RpcContextCreator(
26-
new RpcProxy(),
27-
new ExceptionFiltersContext(config),
28-
new PipesContextCreator(config),
29-
new PipesConsumer(),
30-
new GuardsContextCreator(container, config),
31-
new GuardsConsumer(),
32-
new InterceptorsContextCreator(container, config),
33-
new InterceptorsConsumer(),
34-
),
35+
contextCreator,
3536
);
3637
}
3738

0 commit comments

Comments
 (0)