errors.test.js
2.19 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
const errors = require('../../lib/errors');
const expect = require('chai').expect;
describe('errors', () => {
it('should maintain stack trace with message', () => {
const errorsWithMessage = [
'BaseError', 'ValidationError', 'InstanceError',
'EmptyResultError', 'EagerLoadingError', 'AssociationError', 'QueryError'
];
errorsWithMessage.forEach(errorName => {
function throwError() {
throw new errors[errorName]('this is a message');
}
let err;
try {
throwError();
} catch (error) {
err = error;
}
expect(err).to.exist;
const stackParts = err.stack.split('\n');
const fullErrorName = `Sequelize${errorName}`;
expect(stackParts[0]).to.equal(`${fullErrorName}: this is a message`);
expect(stackParts[1]).to.match(/^ {4}at throwError \(.*errors.test.js:\d+:\d+\)$/);
});
});
it('should maintain stack trace without message', () => {
const errorsWithoutMessage = [
'ConnectionError', 'ConnectionRefusedError', 'ConnectionTimedOutError',
'AccessDeniedError', 'HostNotFoundError', 'HostNotReachableError', 'InvalidConnectionError'
];
errorsWithoutMessage.forEach(errorName => {
function throwError() {
throw new errors[errorName](null);
}
let err;
try {
throwError();
} catch (error) {
err = error;
}
expect(err).to.exist;
const stackParts = err.stack.split('\n');
const fullErrorName = `Sequelize${errorName}`;
expect(stackParts[0]).to.equal(fullErrorName);
expect(stackParts[1]).to.match(/^ {4}at throwError \(.*errors.test.js:\d+:\d+\)$/);
});
});
describe('AggregateError', () => {
it('get .message works', () => {
const { AggregateError } = errors;
expect(String(
new AggregateError([
new Error('foo'),
new Error('bar\nbaz'),
new AggregateError([
new Error('this\nis\na\ntest'),
new Error('qux')
])
])
)).to.equal(
`AggregateError of:
Error: foo
Error: bar
baz
AggregateError of:
Error: this
is
a
test
Error: qux
`);
});
});
});