Skip to content

Commit 4cfda07

Browse files
tests(@nestjs/core) fix failing unit tests
1 parent c9184ac commit 4cfda07

8 files changed

Lines changed: 152 additions & 78 deletions

File tree

packages/core/middleware/builder.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
4242
this.includedRoutes = filterMiddleware(middleware);
4343
}
4444

45+
public getExcludedRoutes(): RouteInfo[] {
46+
return this.excludedRoutes;
47+
}
48+
4549
public with(...args): MiddlewareConfigProxy {
4650
this.contextParameters = args;
4751
return this;
@@ -85,13 +89,13 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
8589
}
8690

8791
private isRouteExcluded(routeInfo: RouteInfo): boolean {
88-
return this.excludedRoutes.some(excluded => {
89-
const pathLastIndex = routeInfo.path.length - 1;
90-
const validatedRoutePath =
91-
routeInfo.path[pathLastIndex] === '/'
92-
? routeInfo.path.slice(0, pathLastIndex)
93-
: routeInfo.path;
92+
const pathLastIndex = routeInfo.path.length - 1;
93+
const validatedRoutePath =
94+
routeInfo.path[pathLastIndex] === '/'
95+
? routeInfo.path.slice(0, pathLastIndex)
96+
: routeInfo.path;
9497

98+
return this.excludedRoutes.some(excluded => {
9599
const isPathEqual = validatedRoutePath === excluded.path;
96100
if (!isPathEqual) {
97101
return false;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { RequestMethod } from '@nestjs/common';
2+
import { expect } from 'chai';
3+
import { Controller, Get } from '../../../common';
4+
import { NestContainer } from '../../injector/container';
5+
import { MiddlewareBuilder } from '../../middleware/builder';
6+
import { RoutesMapper } from '../../middleware/routes-mapper';
7+
8+
describe('MiddlewareBuilder', () => {
9+
let builder: MiddlewareBuilder;
10+
11+
beforeEach(() => {
12+
builder = new MiddlewareBuilder(new RoutesMapper(new NestContainer()));
13+
});
14+
describe('apply', () => {
15+
let configProxy;
16+
beforeEach(() => {
17+
configProxy = builder.apply([]);
18+
});
19+
it('should return configuration proxy', () => {
20+
const metatype = (MiddlewareBuilder as any).ConfigProxy;
21+
expect(configProxy instanceof metatype).to.be.true;
22+
});
23+
describe('configuration proxy', () => {
24+
it('should returns itself on "with()" call', () => {
25+
expect(configProxy.with()).to.be.eq(configProxy);
26+
});
27+
describe('when "forRoutes()" called', () => {
28+
@Controller('path')
29+
class Test {
30+
@Get('route')
31+
public getAll() {}
32+
}
33+
const route = { path: '/test', method: 0 };
34+
it('should store configuration passed as argument', () => {
35+
configProxy.forRoutes(route, Test);
36+
37+
expect(builder.build()).to.deep.equal([
38+
{
39+
middleware: [],
40+
forRoutes: [
41+
{
42+
method: 0,
43+
path: route.path,
44+
},
45+
{
46+
method: 0,
47+
path: '/path/route',
48+
},
49+
],
50+
},
51+
]);
52+
});
53+
});
54+
});
55+
});
56+
57+
describe('exclude', () => {
58+
it('should map string to RouteInfo', () => {
59+
const path = '/test';
60+
const proxy: any = builder.apply().exclude(path);
61+
62+
expect(proxy.getExcludedRoutes()).to.be.eql([
63+
{
64+
path,
65+
method: RequestMethod.ALL,
66+
},
67+
]);
68+
});
69+
});
70+
71+
describe('isRouteExcluded', () => {
72+
const routeInfo = { path: '/test', method: RequestMethod.POST };
73+
let proxy: any;
74+
75+
beforeEach(() => {
76+
proxy = builder.apply();
77+
});
78+
describe('when path is equal', () => {
79+
describe('when method is ALL', () => {
80+
it('should return true', () => {
81+
proxy.exclude(routeInfo.path);
82+
83+
expect(proxy.isRouteExcluded(routeInfo)).to.be.true;
84+
});
85+
});
86+
describe('when method is equal', () => {
87+
it('should return true', () => {
88+
proxy.exclude({
89+
path: routeInfo.path,
90+
method: RequestMethod.POST,
91+
});
92+
93+
expect(proxy.isRouteExcluded(routeInfo)).to.be.true;
94+
});
95+
});
96+
describe('when path has / at the end', () => {
97+
it('should return true', () => {
98+
proxy.exclude({
99+
path: 'test',
100+
method: RequestMethod.POST,
101+
});
102+
103+
expect(proxy.isRouteExcluded({
104+
...routeInfo,
105+
path: '/test/',
106+
})).to.be.true;
107+
});
108+
});
109+
describe('when method is not equal', () => {
110+
it('should return false', () => {
111+
proxy.exclude({
112+
path: routeInfo.path,
113+
method: RequestMethod.GET,
114+
});
115+
116+
expect(proxy.isRouteExcluded(routeInfo)).to.be.false;
117+
});
118+
});
119+
});
120+
describe('when path is not equal', () => {
121+
it('should return false', () => {
122+
proxy.exclude({
123+
path: 'testx',
124+
method: RequestMethod.POST,
125+
});
126+
127+
expect(proxy.isRouteExcluded(routeInfo)).to.be.false;
128+
});
129+
});
130+
});
131+
});
File renamed without changes.

packages/core/test/middlewares/middlewares-module.spec.ts renamed to packages/core/test/middleware/middlewares-module.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ describe('MiddlewareModule', () => {
126126
).to.be.rejectedWith(InvalidMiddlewareException);
127127
});
128128

129-
it('should store middleware when middleware is stored in container', () => {
129+
it('should mount middleware when is stored in container', () => {
130130
const route = 'testPath';
131131
const configuration = {
132132
middleware: [TestMiddleware],
133133
forRoutes: ['test', AnotherRoute, TestRoute],
134134
};
135135

136-
const useSpy = sinon.spy();
136+
const createMiddlewareFactorySpy = sinon.spy();
137137
const app = {
138-
use: useSpy,
138+
createMiddlewareFactory: createMiddlewareFactorySpy,
139139
};
140140
const container = new MiddlewareContainer();
141141
const moduleKey = 'Test' as any;
@@ -149,12 +149,12 @@ describe('MiddlewareModule', () => {
149149

150150
middlewareModule.registerRouteMiddleware(
151151
container,
152-
route,
152+
{ path: route, method: RequestMethod.ALL },
153153
configuration,
154154
moduleKey,
155155
app as any,
156156
);
157-
expect(useSpy.calledOnce).to.be.true;
157+
expect(createMiddlewareFactorySpy.calledOnce).to.be.true;
158158
});
159159
});
160160
});
File renamed without changes.
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { expect } from 'chai';
2-
import { RoutesMapper } from '../../middleware/routes-mapper';
32
import { Controller } from '../../../common/decorators/core/controller.decorator';
43
import { RequestMapping } from '../../../common/decorators/http/request-mapping.decorator';
54
import { RequestMethod } from '../../../common/enums/request-method.enum';
6-
import { UnknownRequestMappingException } from '../../errors/exceptions/unknown-request-mapping.exception';
75
import { NestContainer } from '../../injector/container';
6+
import { RoutesMapper } from '../../middleware/routes-mapper';
87

98
describe('RoutesMapper', () => {
109
@Controller('test')
@@ -27,22 +26,12 @@ describe('RoutesMapper', () => {
2726
forRoutes: [{ path: 'test', method: RequestMethod.GET }, TestRoute],
2827
};
2928

30-
expect(mapper.mapRouteToRouteProps(config.forRoutes[0])).to.deep.equal([
31-
'/test',
29+
expect(mapper.mapRouteToRouteInfo(config.forRoutes[0])).to.deep.equal([
30+
{ path: '/test', method: RequestMethod.GET },
3231
]);
33-
expect(mapper.mapRouteToRouteProps(config.forRoutes[1])).to.deep.equal([
34-
'/test/test',
35-
'/test/another',
32+
expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([
33+
{ path: '/test/test', method: RequestMethod.GET },
34+
{ path: '/test/another', method: RequestMethod.DELETE },
3635
]);
3736
});
38-
39-
it('should throw exception when invalid object was passed as route', () => {
40-
const config = {
41-
middleware: 'Test',
42-
forRoutes: [{ method: RequestMethod.GET }],
43-
};
44-
expect(
45-
mapper.mapRouteToRouteProps.bind(mapper, config.forRoutes[0]),
46-
).throws(UnknownRequestMappingException);
47-
});
4837
});
File renamed without changes.

packages/core/test/middlewares/builder.spec.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)