Skip to content

Commit 41fcfd8

Browse files
tests(@nestjs) increase test-cov, fix guards issue
1 parent 0c441d9 commit 41fcfd8

10 files changed

Lines changed: 281 additions & 107 deletions

File tree

src/core/guards/guards-context-creator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class GuardsContextCreator extends ContextCreator {
2727
return [] as R;
2828
}
2929
const isGlobalMetadata = metadata === this.getGlobalMetadata();
30-
return isGlobalMetadata ?
30+
return isGlobalMetadata ?
3131
this.createGlobalMetadataContext<T, R>(metadata) :
3232
iterate(metadata).filter((metatype: any) => metatype && metatype.name)
3333
.map((metatype) => this.getInstanceByMetatype(metatype))
@@ -53,6 +53,9 @@ export class GuardsContextCreator extends ContextCreator {
5353
}
5454

5555
public getGlobalMetadata<T extends any[]>(): T {
56-
return [] as T;
56+
if (!this.config) {
57+
return [] as T;
58+
}
59+
return this.config.getGlobalGuards() as T;
5760
}
5861
}

src/core/injector/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class Module {
7575
}
7676

7777
public addModuleRef() {
78-
const moduleRef = this.getModuleRefMetatype(this._components);
78+
const moduleRef = this.createModuleRefMetatype(this._components);
7979
this._components.set(ModuleRef.name, {
8080
name: ModuleRef.name,
8181
metatype: ModuleRef as any,
@@ -224,9 +224,9 @@ export class Module {
224224
});
225225
}
226226

227-
private getModuleRefMetatype(components) {
227+
public createModuleRefMetatype(components) {
228228
return class {
229-
private readonly components = components;
229+
public readonly components = components;
230230

231231
public get<T>(type: OpaqueToken): T {
232232
const name = isFunction(type) ? (type as Metatype<any>).name : type;

src/core/interceptors/interceptors-consumer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ export class InterceptorsConsumer {
2121
return await next();
2222
}
2323
const context = this.createContext(instance, callback);
24-
const start$ = Observable.defer(() => {
25-
const res = next();
26-
const isDeffered = res instanceof Promise || res instanceof Observable;
27-
return isDeffered ? res : Promise.resolve(res);
28-
});
24+
const start$ = Observable.defer(() => this.transformDeffered(next));
2925
const result$ = await interceptors.reduce(
3026
async (stream$, interceptor) => await interceptor.intercept(dataOrRequest, context, await stream$),
3127
Promise.resolve(start$),
@@ -39,4 +35,10 @@ export class InterceptorsConsumer {
3935
handler: callback,
4036
};
4137
}
38+
39+
public transformDeffered(next: () => any): Promise<any> | Observable<any> {
40+
const res = next();
41+
const isDeffered = res instanceof Promise || res instanceof Observable;
42+
return isDeffered ? res : Promise.resolve(res);
43+
}
4244
}

src/core/router/router-execution-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ export class RouterExecutionContext {
3939
const metadata = this.reflectCallbackMetadata(instance, callback) || {};
4040
const keys = Object.keys(metadata);
4141
const argsLength = this.getArgumentsLength(keys, metadata);
42-
const args = this.createNullArray(argsLength);
4342
const pipes = this.pipesContextCreator.create(instance, callback);
4443
const paramtypes = this.reflectCallbackParamtypes(instance, callback);
4544
const guards = this.guardsContextCreator.create(instance, callback, module);
4645
const interceptors = this.interceptorsContextCreator.create(instance, callback, module);
4746
const httpCode = this.reflectHttpStatusCode(callback);
4847

4948
return async (req, res, next) => {
49+
const args = this.createNullArray(argsLength);
5050
const paramProperties = this.exchangeKeysForValues(keys, metadata, { req, res, next });
5151
const canActivate = await this.guardsConsumer.tryActivate(guards, req, instance, callback);
5252
if (!canActivate) {
@@ -68,7 +68,7 @@ export class RouterExecutionContext {
6868
const result = await this.interceptorsConsumer.intercept(
6969
interceptors, req, instance, callback, handler,
7070
);
71-
return !isResponseObj ?
71+
return !isResponseObj ?
7272
this.responseController.apply(result, res, requestMethod, httpCode) :
7373
undefined;
7474
};

src/core/scanner.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class DependenciesScanner {
1818
this.scanModulesForDependencies();
1919
}
2020

21-
private scanForModules(module: NestModuleMetatype, scope: NestModuleMetatype[] = []) {
21+
public scanForModules(module: NestModuleMetatype, scope: NestModuleMetatype[] = []) {
2222
this.storeModule(module, scope);
2323

2424
const importedModules = this.reflectMetadata(module, metadata.MODULES);
@@ -27,11 +27,11 @@ export class DependenciesScanner {
2727
});
2828
}
2929

30-
private storeModule(module: NestModuleMetatype, scope: NestModuleMetatype[]) {
30+
public storeModule(module: NestModuleMetatype, scope: NestModuleMetatype[]) {
3131
this.container.addModule(module, scope);
3232
}
3333

34-
private scanModulesForDependencies() {
34+
public scanModulesForDependencies() {
3535
const modules = this.container.getModules();
3636

3737
modules.forEach(({ metatype }, token) => {
@@ -42,12 +42,12 @@ export class DependenciesScanner {
4242
});
4343
}
4444

45-
private reflectRelatedModules(module: NestModuleMetatype, token: string) {
45+
public reflectRelatedModules(module: NestModuleMetatype, token: string) {
4646
const modules = this.reflectMetadata(module, metadata.MODULES);
4747
modules.map((related) => this.storeRelatedModule(related, token));
4848
}
4949

50-
private reflectComponents(module: NestModuleMetatype, token: string) {
50+
public reflectComponents(module: NestModuleMetatype, token: string) {
5151
const components = this.reflectMetadata(module, metadata.COMPONENTS);
5252
components.map((component) => {
5353
this.storeComponent(component, token);
@@ -56,80 +56,79 @@ export class DependenciesScanner {
5656
});
5757
}
5858

59-
private reflectComponentMetadata(component: Metatype<Injectable>, token: string) {
59+
public reflectComponentMetadata(component: Metatype<Injectable>, token: string) {
6060
this.reflectGatewaysMiddlewares(component, token);
6161
}
6262

63-
private reflectControllers(module: NestModuleMetatype, token: string) {
63+
public reflectControllers(module: NestModuleMetatype, token: string) {
6464
const routes = this.reflectMetadata(module, metadata.CONTROLLERS);
6565
routes.map((route) => {
6666
this.storeRoute(route, token);
6767
this.reflectDynamicMetadata(route, token);
6868
});
6969
}
7070

71-
private reflectDynamicMetadata(obj: Metatype<Injectable>, token: string) {
71+
public reflectDynamicMetadata(obj: Metatype<Injectable>, token: string) {
7272
if (!obj.prototype) { return; }
7373

7474
this.reflectGuards(obj, token);
7575
this.reflectInterceptors(obj, token);
7676
}
7777

78-
private reflectExports(module: NestModuleMetatype, token: string) {
78+
public reflectExports(module: NestModuleMetatype, token: string) {
7979
const exports = this.reflectMetadata(module, metadata.EXPORTS);
8080
exports.map((exportedComponent) => this.storeExportedComponent(exportedComponent, token));
8181
}
8282

83-
private reflectGatewaysMiddlewares(component: Metatype<Injectable>, token: string) {
83+
public reflectGatewaysMiddlewares(component: Metatype<Injectable>, token: string) {
8484
const middlewares = this.reflectMetadata(component, GATEWAY_MIDDLEWARES);
8585
middlewares.map((middleware) => this.storeComponent(middleware, token));
8686
}
8787

88-
private reflectGuards(component: Metatype<Injectable>, token: string) {
88+
public reflectGuards(component: Metatype<Injectable>, token: string) {
8989
const controllerGuards = this.reflectMetadata(component, GUARDS_METADATA);
90-
const methodsGuards = this.metadataScanner.scanFromPrototype(null, component.prototype,
91-
(method: string) => {
92-
const descriptor = Reflect.getOwnPropertyDescriptor(component.prototype, method);
93-
return descriptor ? Reflect.getMetadata(GUARDS_METADATA, descriptor.value) : undefined;
94-
},
90+
const methodsGuards = this.metadataScanner.scanFromPrototype(
91+
null, component.prototype, this.reflectKeyMetadata.bind(this, component, GUARDS_METADATA),
9592
);
96-
const flattenMethodsGuards = methodsGuards.reduce((a, b) => a.concat(b), []);
93+
const flattenMethodsGuards = methodsGuards.reduce<any[]>((a: any[], b) => a.concat(b), []);
9794
[...controllerGuards, ...flattenMethodsGuards].map((guard) => this.storeInjectable(guard, token));
9895
}
9996

100-
private reflectInterceptors(component: Metatype<Injectable>, token: string) {
97+
public reflectInterceptors(component: Metatype<Injectable>, token: string) {
10198
const controllerInterceptors = this.reflectMetadata(component, INTERCEPTORS_METADATA);
102-
const methodsInterceptors = this.metadataScanner.scanFromPrototype(null, component.prototype,
103-
(method: string) => {
104-
const descriptor = Reflect.getOwnPropertyDescriptor(component.prototype, method);
105-
return descriptor ? Reflect.getMetadata(INTERCEPTORS_METADATA, descriptor.value) : undefined;
106-
},
99+
const methodsInterceptors = this.metadataScanner.scanFromPrototype(
100+
null, component.prototype, this.reflectKeyMetadata.bind(this, component, INTERCEPTORS_METADATA),
107101
);
108-
const flattenMethodsInterceptors = methodsInterceptors.reduce((a, b) => a.concat(b), []);
102+
const flattenMethodsInterceptors = methodsInterceptors.reduce<any[]>((a: any[], b) => a.concat(b), []);
109103
[...controllerInterceptors, ...flattenMethodsInterceptors].map((guard) => this.storeInjectable(guard, token));
110104
}
111105

112-
private storeRelatedModule(related: NestModuleMetatype, token: string) {
106+
public reflectKeyMetadata(component: Metatype<Injectable>, key: string, method: string) {
107+
const descriptor = Reflect.getOwnPropertyDescriptor(component.prototype, method);
108+
return descriptor ? Reflect.getMetadata(key, descriptor.value) : undefined;
109+
}
110+
111+
public storeRelatedModule(related: NestModuleMetatype, token: string) {
113112
this.container.addRelatedModule(related, token);
114113
}
115114

116-
private storeComponent(component: Metatype<Injectable>, token: string) {
115+
public storeComponent(component: Metatype<Injectable>, token: string) {
117116
this.container.addComponent(component, token);
118117
}
119118

120-
private storeInjectable(component: Metatype<Injectable>, token: string) {
119+
public storeInjectable(component: Metatype<Injectable>, token: string) {
121120
this.container.addInjectable(component, token);
122121
}
123122

124-
private storeExportedComponent(exportedComponent: Metatype<Injectable>, token: string) {
123+
public storeExportedComponent(exportedComponent: Metatype<Injectable>, token: string) {
125124
this.container.addExportedComponent(exportedComponent, token);
126125
}
127126

128-
private storeRoute(route: Metatype<Controller>, token: string) {
127+
public storeRoute(route: Metatype<Controller>, token: string) {
129128
this.container.addController(route, token);
130129
}
131130

132-
private reflectMetadata(metatype, metadata: string) {
131+
public reflectMetadata(metatype, metadata: string) {
133132
return Reflect.getMetadata(metadata, metatype) || [];
134133
}
135134

src/core/test/application-config.spec.ts

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,60 @@ import { expect } from 'chai';
22
import { ApplicationConfig } from '../application-config';
33

44
describe('ApplicationConfig', () => {
5-
let appConfig: ApplicationConfig;
5+
let appConfig: ApplicationConfig;
66

7-
beforeEach(() => {
8-
appConfig = new ApplicationConfig();
7+
beforeEach(() => {
8+
appConfig = new ApplicationConfig();
9+
});
10+
describe('globalPath', () => {
11+
it('should set global path', () => {
12+
const path = 'test';
13+
appConfig.setGlobalPrefix(path);
14+
15+
expect(appConfig.getGlobalPrefix()).to.be.eql(path);
16+
});
17+
it('should has empty string as a global path by default', () => {
18+
expect(appConfig.getGlobalPrefix()).to.be.eql('');
919
});
10-
describe('globalPath', () => {
11-
it('should set global path', () => {
12-
const path = 'test';
13-
appConfig.setGlobalPrefix(path);
14-
15-
expect(appConfig.getGlobalPrefix()).to.be.eql(path);
16-
});
17-
it('should has empty string as a global path by default', () => {
18-
expect(appConfig.getGlobalPrefix()).to.be.eql('');
19-
});
20+
});
21+
describe('IOAdapter', () => {
22+
it('should set io adapter', () => {
23+
const ioAdapter = { test: 0 };
24+
appConfig.setIoAdapter(ioAdapter as any);
25+
26+
expect(appConfig.getIoAdapter()).to.be.eql(ioAdapter);
27+
});
28+
});
29+
describe('Pipes', () => {
30+
it('should set global pipes', () => {
31+
const pipes = ['test', 'test2'];
32+
appConfig.useGlobalPipes(...pipes as any);
33+
34+
expect(appConfig.getGlobalPipes()).to.be.eql(pipes);
2035
});
21-
describe('IOAdapter', () => {
22-
it('should set io adapter', () => {
23-
const ioAdapter = { test: 0 };
24-
appConfig.setIoAdapter(ioAdapter as any);
36+
});
37+
describe('Filters', () => {
38+
it('should set global filters', () => {
39+
const filters = ['test', 'test2'];
40+
appConfig.useGlobalFilters(...filters as any);
2541

26-
expect(appConfig.getIoAdapter()).to.be.eql(ioAdapter);
27-
});
42+
expect(appConfig.getGlobalFilters()).to.be.eql(filters);
2843
});
29-
describe('Pipes', () => {
30-
it('should set global pipes', () => {
31-
const pipes = ['test', 'test2'];
32-
appConfig.useGlobalPipes(...pipes as any);
44+
});
45+
describe('Guards', () => {
46+
it('should set global guards', () => {
47+
const guards = ['test', 'test2'];
48+
appConfig.useGlobalGuards(...guards as any);
3349

34-
expect(appConfig.getGlobalPipes()).to.be.eql(pipes);
35-
});
50+
expect(appConfig.getGlobalGuards()).to.be.eql(guards);
3651
});
37-
describe('Filters', () => {
38-
it('should set global filters', () => {
39-
const filters = ['test', 'test2'];
40-
appConfig.useGlobalFilters(...filters as any);
52+
});
53+
describe('Interceptors', () => {
54+
it('should set global interceptors', () => {
55+
const interceptors = ['test', 'test2'];
56+
appConfig.useGlobalInterceptors(...interceptors as any);
4157

42-
expect(appConfig.getGlobalFilters()).to.be.eql(filters);
43-
});
58+
expect(appConfig.getGlobalInterceptors()).to.be.eql(interceptors);
4459
});
60+
});
4561
});

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,61 @@ describe('Module', () => {
219219
});
220220
});
221221

222+
describe('relatedModules', () => {
223+
it('should return relatedModules', () => {
224+
const test = ['test'];
225+
(module as any)._relatedModules = test;
226+
expect(module.relatedModules).to.be.eql(test);
227+
});
228+
});
229+
230+
describe('injectables', () => {
231+
it('should return injectables', () => {
232+
const test = ['test'];
233+
(module as any)._injectables = test;
234+
expect(module.injectables).to.be.eql(test);
235+
});
236+
});
237+
238+
describe('routes', () => {
239+
it('should return routes', () => {
240+
const test = ['test'];
241+
(module as any)._routes = test;
242+
expect(module.routes).to.be.eql(test);
243+
});
244+
});
245+
246+
describe('exports', () => {
247+
it('should return exports', () => {
248+
const test = ['test'];
249+
(module as any)._exports = test;
250+
expect(module.exports).to.be.eql(test);
251+
});
252+
});
253+
254+
describe('createModuleRefMetatype', () => {
255+
let components: Map<string, any>;
256+
let moduleRef;
257+
258+
beforeEach(() => {
259+
components = new Map();
260+
261+
const Class = module.createModuleRefMetatype(components);
262+
moduleRef = new Class();
263+
});
264+
265+
it('should return metatype with "get" method', () => {
266+
expect(!!moduleRef.get).to.be.true;
267+
});
268+
describe('get', () => {
269+
it('should return component if exists', () => {
270+
const comp = { instance: [] };
271+
components.set('comp', comp);
272+
expect(moduleRef.get('comp')).to.be.eql(comp.instance);
273+
});
274+
it('should return null if not exists', () => {
275+
expect(moduleRef.get('fail')).to.be.null;
276+
});
277+
});
278+
});
222279
});

0 commit comments

Comments
 (0)