forked from nestjs/nest
/
external-exceptions-handler.spec.ts
95 lines (90 loc) · 3.46 KB
/
external-exceptions-handler.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
import { expect } from 'chai';
import { of } from 'rxjs';
import * as sinon from 'sinon';
import { ExternalExceptionsHandler } from '../../exceptions/external-exceptions-handler';
import { ExternalExceptionFilter } from '../../exceptions/external-exception-filter';
describe('ExternalExceptionsHandler', () => {
let handler: ExternalExceptionsHandler;
beforeEach(() => {
handler = new ExternalExceptionsHandler();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore The 'logger' property is private but we want to avoid showing useless error logs
ExternalExceptionFilter.logger.error = () => {};
});
describe('next', () => {
it('should method returns expected stream with message when exception is unknown', () => {
const error = new Error();
expect(() => handler.next(error, null)).to.throw(error);
});
describe('when "invokeCustomFilters" returns value', () => {
const observable$ = of(true);
beforeEach(() => {
sinon.stub(handler, 'invokeCustomFilters').returns(observable$ as any);
});
it('should return observable', () => {
const result = handler.next(new Error(), null);
expect(result).to.be.eql(observable$);
});
});
});
describe('setCustomFilters', () => {
const filters = ['test', 'test2'];
it('should set custom filters', () => {
handler.setCustomFilters(filters as any);
expect((handler as any).filters).to.be.eql(filters);
});
it('should throws exception when passed argument is not an array', () => {
expect(() => handler.setCustomFilters(null)).to.throw();
});
});
describe('invokeCustomFilters', () => {
describe('when filters array is empty', () => {
it('should return identity', () => {
expect(handler.invokeCustomFilters(null, null)).to.be.null;
});
});
describe('when filters array is not empty', () => {
let filters, funcSpy;
class TestException {}
class AnotherTestException {}
beforeEach(() => {
funcSpy = sinon.spy();
});
describe('when filter exists in filters array', () => {
beforeEach(() => {
filters = [{ exceptionMetatypes: [TestException], func: funcSpy }];
(handler as any).filters = filters;
});
it('should call funcSpy', () => {
handler.invokeCustomFilters(new TestException(), null);
expect(funcSpy.notCalled).to.be.false;
});
it('should call funcSpy with exception and response passed as an arguments', () => {
const exception = new TestException();
handler.invokeCustomFilters(exception, null);
expect(funcSpy.calledWith(exception)).to.be.true;
});
it('should return stream', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
.not.null;
});
});
describe('when filter does not exists in filters array', () => {
beforeEach(() => {
filters = [
{ exceptionMetatypes: [AnotherTestException], func: funcSpy },
];
(handler as any).filters = filters;
});
it('should not call funcSpy', () => {
handler.invokeCustomFilters(new TestException(), null);
expect(funcSpy.notCalled).to.be.true;
});
it('should return null', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
.null;
});
});
});
});
});