-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
handle-error.test.js
126 lines (109 loc) · 4.86 KB
/
handle-error.test.js
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
'use strict';
const chai = require('chai');
const sinon = require('sinon');
const observeOutput = require('@serverless/test/observe-output');
const handleError = require('../../../../lib/cli/handle-error');
const isStandaloneExecutable = require('../../../../lib/utils/isStandaloneExecutable');
const ServerlessError = require('../../../../lib/serverless-error');
const proxyquire = require('proxyquire');
chai.use(require('sinon-chai'));
const expect = chai.expect;
describe('test/unit/lib/cli/handle-error.test.js', () => {
it('should output environment information', async () => {
const output = await observeOutput(() => handleError(new ServerlessError('Test error')));
expect(output).to.have.string('Environment: ');
expect(output).to.have.string('node');
expect(output).to.have.string('framework');
expect(output).to.have.string('plugin');
expect(output).to.have.string('SDK');
});
if (isStandaloneExecutable) {
it('should report standalone installation', async () => {
const output = await observeOutput(() => handleError(new ServerlessError('Test error')));
expect(output).to.have.string('(standalone)');
});
}
it('should handle non-error objects', async () => {
const output = await observeOutput(() => handleError(handleError('NON-ERROR')));
expect(output).to.have.string('NON-ERROR');
});
describe('with mocked telemetry', () => {
const generateTelemetryPayloadStub = sinon.stub().returns({});
const storeTelemetryLocallyStub = sinon.stub();
const sendTelemetryStub = sinon.stub();
const resolveInputStub = sinon.stub().returns({ commandSchema: {} });
const handleErrorWithMocks = proxyquire('../../../../lib/cli/handle-error', {
'./resolve-input': resolveInputStub,
'../utils/telemetry/areDisabled': false,
'../utils/telemetry/generatePayload': generateTelemetryPayloadStub,
'../utils/telemetry/index': {
send: sendTelemetryStub,
storeLocally: storeTelemetryLocallyStub,
},
});
beforeEach(() => {
sinon.resetHistory();
});
it('should record telemetry only if `hasTelemetryBeenReported` is `false`', async () => {
await handleErrorWithMocks(new ServerlessError('Test error', 'ERR_CODE'), {
hasTelemetryBeenReported: false,
});
expect(generateTelemetryPayloadStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub).to.be.calledOnce;
expect(sendTelemetryStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub.getCall(0).args[0]).to.deep.equal({
outcome: 'failure',
failureReason: {
code: 'ERR_CODE',
kind: 'user',
},
});
});
it('should add `location` to `failureReason` in telemetry if error code missing', async () => {
await handleErrorWithMocks(new ServerlessError('Test error'), {
hasTelemetryBeenReported: false,
});
expect(generateTelemetryPayloadStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub).to.be.calledOnce;
expect(sendTelemetryStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub.getCall(0).args[0].failureReason).to.have.property(
'location'
);
});
it('should add `location` to `failureReason` in telemetry for non-user errors', async () => {
await handleErrorWithMocks(new Error('Test error'), {
hasTelemetryBeenReported: false,
});
expect(generateTelemetryPayloadStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub).to.be.calledOnce;
expect(sendTelemetryStub).to.be.calledOnce;
expect(storeTelemetryLocallyStub.getCall(0).args[0].failureReason).to.have.property(
'location'
);
});
it('should not record telemetry if `hasTelemetryBeenReported` is `true`', async () => {
await handleErrorWithMocks(new ServerlessError('Test error'), {
hasTelemetryBeenReported: true,
});
expect(generateTelemetryPayloadStub).not.to.be.called;
expect(storeTelemetryLocallyStub).not.to.be.called;
expect(sendTelemetryStub).not.to.be.called;
});
it('should not record telemetry if `hasTelemetryBeenReported` is not passed', async () => {
await handleErrorWithMocks(new ServerlessError('Test error'));
expect(generateTelemetryPayloadStub).not.to.be.called;
expect(storeTelemetryLocallyStub).not.to.be.called;
expect(sendTelemetryStub).not.to.be.called;
});
it('should not record telemetry if `commandSchema` was not resolved', async () => {
// Ensure that `commandSchema` is not included in result of `resolveInput`
resolveInputStub.returns({});
await handleErrorWithMocks(new ServerlessError('Test error'), {
hasTelemetryBeenReported: false,
});
expect(generateTelemetryPayloadStub).not.to.be.called;
expect(storeTelemetryLocallyStub).not.to.be.called;
expect(sendTelemetryStub).not.to.be.called;
});
});
});