Skip to content

Commit 6c70f9c

Browse files
test(@nestjs/common) cover multer, FileInterceptor unit tests
1 parent 2ebfca2 commit 6c70f9c

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/common/test/interceptors/file.interceptor.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,17 @@ describe('FileInterceptor', () => {
2626
expect(singleSpy.called).to.be.true;
2727
expect(singleSpy.calledWith(fieldName)).to.be.true;
2828
});
29+
it('should transform exception', async () => {
30+
const fieldName = 'file';
31+
const target = new (FileInterceptor(fieldName));
32+
const err = {};
33+
const callback = (req, res, next) => next(err);
34+
35+
(target as any).upload = {
36+
single: () => callback,
37+
};
38+
const req = {};
39+
expect(target.intercept(req, null, stream$)).to.eventually.throw();
40+
});
2941
});
3042
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as sinon from 'sinon';
2+
import { expect } from 'chai';
3+
import { transformException } from './../../../interceptors/multer/multer.utils';
4+
import { HttpException } from '../../../../../lib/common/exceptions/http.exception';
5+
import { multerExceptions } from '../../../interceptors/multer/multer.constants';
6+
import { PayloadTooLargeException, BadRequestException } from '../../../exceptions';
7+
8+
describe('transformException', () => {
9+
describe('if error does not exist', () => {
10+
it('behave as identity', () => {
11+
const err = undefined;
12+
expect(
13+
transformException(err)
14+
).to.be.eq(err);
15+
});
16+
});
17+
describe('if error is instance of HttpException', () => {
18+
it('behave as identity', () => {
19+
const err = new HttpException('response', 500);
20+
expect(
21+
transformException(err)
22+
).to.be.eq(err);
23+
});
24+
});
25+
describe('if error exists and is not instance of HttpException', () => {
26+
describe('and is LIMIT_FILE_SIZE exception', () => {
27+
it('should return "PayloadTooLargeException"', () => {
28+
const err = { message: multerExceptions.LIMIT_FILE_SIZE };
29+
expect(
30+
transformException(err as any)
31+
).to.be.instanceof(PayloadTooLargeException);
32+
});
33+
});
34+
describe('and is multer exception but not a LIMIT_FILE_SIZE', () => {
35+
it('should return "BadRequestException"', () => {
36+
const err = { message: multerExceptions.LIMIT_FIELD_KEY };
37+
expect(
38+
transformException(err as any)
39+
).to.be.instanceof(BadRequestException);
40+
});
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)