forked from Speedup-lib/nodejs-standard-error-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_error.spec.ts
More file actions
65 lines (51 loc) · 2.11 KB
/
validation_error.spec.ts
File metadata and controls
65 lines (51 loc) · 2.11 KB
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
import { ApplicationError } from './application_error';
import { BadRequestError } from './bad_request_error';
import { ValidationError } from './validation_error';
describe('ValidationError', () => {
it('check inheritance tree', () => {
const err = new ValidationError(undefined);
expect(err instanceof Error).toBeTruthy();
expect(err instanceof ApplicationError).toBeTruthy();
expect(err instanceof BadRequestError).toBeTruthy();
expect(err instanceof ValidationError).toBeTruthy();
});
it('should set default error message', () => {
const err = new ValidationError(undefined);
expect(err).toHaveProperty('message');
expect(typeof err.message).toBe('string');
expect(err.message).toBe('ValidationError');
});
it('should set custom error message', () => {
const err = new ValidationError(undefined, 'MyErrorMessage');
expect(err).toHaveProperty('message');
expect(typeof err.message).toBe('string');
expect(err.message).toBe('MyErrorMessage');
});
it('should set default error code', () => {
const err = new ValidationError(undefined);
expect(err).toHaveProperty('code');
expect(typeof err.code).toBe('string');
expect(err.code).toBe('E_VALIDATION_FAILED');
});
it('should set custom error code', () => {
const err = new ValidationError(undefined, undefined, {
code: 'E_CUSTOM_ERROR',
});
expect(err).toHaveProperty('code');
expect(typeof err.code).toBe('string');
expect(err.code).toBe('E_CUSTOM_ERROR');
});
it('should set default cause (undefined)', () => {
const err = new ValidationError(undefined);
expect(err).toHaveProperty('cause');
expect(typeof err.cause).toBe('undefined');
expect(err.cause).toBe(undefined);
});
it('should set custom cause (another application error)', () => {
const cause = new BadRequestError(undefined, { code: 'E_CAUSE' });
const err = new ValidationError(undefined, undefined, { cause });
expect(err).toHaveProperty('cause');
expect(err.cause instanceof BadRequestError).toBeTruthy();
expect((err.cause as BadRequestError).code).toBe('E_CAUSE');
});
});