forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.test.js
More file actions
157 lines (125 loc) · 4.96 KB
/
Error.test.js
File metadata and controls
157 lines (125 loc) · 4.96 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const errorReporter = require('../utils/sentry').raven;
const ServerlessError = require('./Error').ServerlessError;
const logError = require('./Error').logError;
const logWarning = require('./Error').logWarning;
describe('ServerlessError', () => {
describe('#constructor()', () => {
it('should store message', () => {
const error = new ServerlessError('a message', 'a status code');
expect(error.message).to.be.equal('a message');
});
it('should store name', () => {
const error = new ServerlessError('a message', 'a status code');
expect(error.name).to.be.equal('ServerlessError');
});
it('should store status code', () => {
const error = new ServerlessError('a message', 'a status code');
expect(error.statusCode).to.be.equal('a status code');
});
it('should have stack trace', () => {
let expectedError;
function testStackFrame() {
expectedError = new ServerlessError('a message', 'a status code');
throw expectedError;
}
let thrownError;
try {
testStackFrame();
} catch (e) {
thrownError = e;
}
expect(thrownError).to.exist; // eslint-disable-line no-unused-expressions
expect(thrownError).to.deep.equal(expectedError);
expect(thrownError.stack).to.exist; // eslint-disable-line no-unused-expressions
expect(thrownError.stack).to.have.string('testStackFrame');
expect(thrownError.stack).to.not.have.string('new ServerlessError');
expect(thrownError.stack).to.not.have.string('Error.js');
});
});
});
describe('Error', () => {
let sandbox;
let consoleLogSpy;
let processExitStub;
let captureExceptionStub;
beforeEach(() => {
sandbox = sinon.sandbox.create();
consoleLogSpy = sandbox.spy(console, 'log');
// the following is used so that the process exiting never interrupts tests
processExitStub = sandbox.stub(process, 'exit').withArgs(1).returns();
captureExceptionStub = sandbox.stub(errorReporter, 'captureException').yields();
});
afterEach(() => {
sandbox.restore();
});
describe('#logError()', () => {
beforeEach(() => {
delete process.env.SLS_DEBUG;
});
it('should log error and exit', () => {
const error = new ServerlessError('a message', 'a status code');
logError(error);
// TODO @David Not sure how to make async test for this
// If tracking enabled, the process exits in a callback and is not defined yet
// expect(this.processExitCodes.length).to.be.equal(1);
// expect(this.processExitCodes).gt(0);
const message = consoleLogSpy.args.join('\n');
expect(consoleLogSpy.called).to.equal(true);
expect(message).to.have.string('Serverless Error');
expect(message).to.have.string('a message');
});
it('should capture the exception and exit the process with 1 if errorReporter is setup', () => {
const error = new Error('an unexpected error');
logError(error);
expect(captureExceptionStub.args[0][0]).to.deep.equal(error);
expect(processExitStub.called).to.equal(true);
expect(processExitStub.calledWithExactly(1)).to.equal(true);
});
it('should notify about SLS_DEBUG and ask report for unexpected errors', () => {
const error = new Error('an unexpected error');
logError(error);
const message = consoleLogSpy.args.join('\n');
expect(consoleLogSpy.called).to.equal(true);
expect(message).to.have.string('SLS_DEBUG=*');
});
it('should print stack trace with SLS_DEBUG', () => {
process.env.SLS_DEBUG = 1;
const error = new ServerlessError('a message');
logError(error);
const message = consoleLogSpy.args.join('\n');
expect(consoleLogSpy.called).to.equal(true);
expect(message).to.have.string('Stack Trace');
expect(message).to.have.string(error.stack);
});
it('should not print stack trace without SLS_DEBUG', () => {
const error = new ServerlessError('a message');
logError(error);
const message = consoleLogSpy.args.join('\n');
expect(consoleLogSpy.called).to.equal(true);
expect(message).to.not.have.string('Stack Trace');
expect(message).to.not.have.string(error.stack);
});
it('should re-throw error when handling raises an exception itself', () => {
const handlingError = new Error('an unexpected error');
let thrownError = null;
try {
logError('INVALID INPUT');
} catch (e) {
thrownError = e;
}
expect(thrownError).to.deep.equal(handlingError);
});
});
describe('#logWarning()', () => {
it('should log warning and proceed', () => {
logWarning('a message');
const message = consoleLogSpy.args.join('\n');
expect(consoleLogSpy.called).to.equal(true);
expect(message).to.have.string('Serverless Warning');
expect(message).to.have.string('a message');
});
});
});