forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external-exception-filter-context.spec.ts
117 lines (109 loc) · 4.07 KB
/
external-exception-filter-context.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ExceptionFilter } from '../../../common';
import { Catch } from '../../../common/decorators/core/catch.decorator';
import { UseFilters } from '../../../common/decorators/core/exception-filters.decorator';
import { ApplicationConfig } from '../../application-config';
import { ExternalExceptionFilterContext } from '../../exceptions/external-exception-filter-context';
import { NestContainer } from '../../injector/container';
import { InstanceWrapper } from '../../injector/instance-wrapper';
describe('ExternalExceptionFilterContext', () => {
let applicationConfig: ApplicationConfig;
let exceptionFilter: ExternalExceptionFilterContext;
class CustomException {}
@Catch(CustomException)
class ExceptionFilter implements ExceptionFilter {
public catch(exc, res) {}
}
class ClassWithNoMetadata implements ExceptionFilter {
public catch(exc, res) {}
}
beforeEach(() => {
applicationConfig = new ApplicationConfig();
exceptionFilter = new ExternalExceptionFilterContext(
new NestContainer(),
applicationConfig,
);
});
describe('create', () => {
describe('when filters metadata is empty', () => {
class EmptyMetadata {}
beforeEach(() => {
sinon.stub(exceptionFilter, 'createContext').returns([]);
});
it('should return plain ExceptionHandler object', () => {
const filter = exceptionFilter.create(
new EmptyMetadata(),
() => ({} as any),
undefined,
);
expect((filter as any).filters).to.be.empty;
});
});
describe('when filters metadata is not empty', () => {
@UseFilters(new ExceptionFilter())
class WithMetadata {}
it('should return ExceptionHandler object with exception filters', () => {
const filter = exceptionFilter.create(
new WithMetadata(),
() => ({} as any),
undefined,
);
expect((filter as any).filters).to.not.be.empty;
});
});
});
describe('reflectCatchExceptions', () => {
it('should return FILTER_CATCH_EXCEPTIONS metadata', () => {
expect(
exceptionFilter.reflectCatchExceptions(new ExceptionFilter()),
).to.be.eql([CustomException]);
});
it('should return an empty array when metadata was found', () => {
expect(
exceptionFilter.reflectCatchExceptions(new ClassWithNoMetadata()),
).to.be.eql([]);
});
});
describe('createConcreteContext', () => {
class InvalidFilter {}
const filters = [new ExceptionFilter(), new InvalidFilter(), 'test'];
it('should return expected exception filters metadata', () => {
const resolved = exceptionFilter.createConcreteContext(filters as any);
expect(resolved).to.have.length(1);
expect(resolved[0].exceptionMetatypes).to.be.deep.equal([
CustomException,
]);
expect(resolved[0].func).to.be.a('function');
});
});
describe('getGlobalMetadata', () => {
describe('when contextId is static and inquirerId is nil', () => {
it('should return global filters', () => {
const expectedResult = applicationConfig.getGlobalFilters();
expect(exceptionFilter.getGlobalMetadata()).to.be.equal(expectedResult);
});
});
describe('otherwise', () => {
it('should merge static global with request/transient scoped filters', () => {
const globalFilters: any = ['test'];
const instanceWrapper = new InstanceWrapper();
const instance = 'request-scoped';
const scopedFilterWrappers = [instanceWrapper];
sinon
.stub(applicationConfig, 'getGlobalFilters')
.callsFake(() => globalFilters);
sinon
.stub(applicationConfig, 'getGlobalRequestFilters')
.callsFake(() => scopedFilterWrappers);
sinon
.stub(instanceWrapper, 'getInstanceByContextId')
.callsFake(() => ({ instance } as any));
expect(exceptionFilter.getGlobalMetadata({ id: 3 })).to.contains(
instance,
...globalFilters,
);
});
});
});
});