Repository navigation
Expand file tree
/
Copy pathrun.test.js
More file actions
233 lines (204 loc) · 7.07 KB
/
Copy pathrun.test.js
File metadata and controls
233 lines (204 loc) · 7.07 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
const BbPromise = require('bluebird');
const chai = require('chai');
const sinon = require('sinon');
const mockery = require('mockery');
const Serverless = require('serverless');
const makeWebpackMock = require('./webpack.mock');
const makeUtilsMock = require('./utils.mock');
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
const expect = chai.expect;
describe('run', () => {
let sandbox;
let webpackMock;
let utilsMock;
let baseModule;
let serverless;
let module;
before(() => {
sandbox = sinon.sandbox.create();
sandbox.usingPromise(BbPromise);
webpackMock = makeWebpackMock(sandbox);
utilsMock = makeUtilsMock();
mockery.enable({ warnOnUnregistered: false });
mockery.registerMock('webpack', webpackMock);
mockery.registerMock('./utils', utilsMock);
baseModule = require('../lib/run');
Object.freeze(baseModule);
});
after(() => {
mockery.disable();
mockery.deregisterAll();
});
beforeEach(() => {
serverless = new Serverless();
serverless.cli = {
log: sandbox.stub(),
consoleLog: sandbox.stub()
};
module = Object.assign({
serverless,
options: {},
}, baseModule);
});
afterEach(() => {
// This will reset the mocks too
sandbox.restore();
});
describe('utils', () => {
it('should expose utils methods', () => {
expect(module.loadHandler).to.be.a('function');
expect(module.getEvent).to.be.a('function');
expect(module.getContext).to.be.a('function');
});
describe('loadHandler', () => {
const testFunctionId = 'testFunctionId';
const testHandlerModuleName = 'testHandlerModule';
const testHandlerFunctionName = 'testHandlerFunction';
const testFunctionsConfig = {
[testFunctionId]: {
handler: `${testHandlerModuleName}.${testHandlerFunctionName}`,
}
};
const testPath = '/testpath';
const testFilename = `${testHandlerModuleName}.js`;
const testModuleFileName = `${testPath}/${testFilename}`;
const testStats = {
compilation: {
options: {
output: {
path: testPath,
filename: testFilename,
},
},
},
};
const testHandlerFunction = sinon.spy();
const testModule = {
[testHandlerFunctionName]: testHandlerFunction,
};
before(() => {
mockery.registerMock(testModuleFileName, testModule);
});
after(() => {
mockery.deregisterMock(testModuleFileName);
});
beforeEach(() => {
serverless.service.functions = testFunctionsConfig;
});
it('should require the handler module', () => {
const res = module.loadHandler(testStats, testFunctionId);
expect(res).to.equal(testHandlerFunction);
expect(utilsMock.purgeCache).to.have.not.been.called;
});
it('should purge the modules cache if required', () => {
const res = module.loadHandler(testStats, testFunctionId, true);
expect(utilsMock.purgeCache).to.have.been.calledWith(testModuleFileName);
});
});
it('should return a default event with `getEvent` and no option path', () => {
module.options.path = null;
const res = module.getEvent();
expect(res).to.equal(null);
});
it('should load an event object from disk with `getEvent`', () => {
const testPath = 'testPath';
module.options.path = testPath;
const testExampleObject = {};
module.serverless.utils.readFileSync = sinon.stub().returns(testExampleObject);
const res = module.getEvent();
expect(res).to.equal(testExampleObject);
});
it('should return an context object with `getContext`', () => {
const testFunctionName = 'testFunctionName';
const res = module.getContext(testFunctionName);
expect(res).to.eql({
awsRequestId: 'testguid',
functionName: testFunctionName,
functionVersion: '$LATEST',
invokeid: 'testguid',
isDefaultFunctionVersion: true,
logGroupName: `/aws/lambda/${testFunctionName}`,
logStreamName: '2016/02/14/[HEAD]13370a84ca4ed8b77c427af260',
memoryLimitInMB: '1024',
});
});
});
describe('run', () => {
const testEvent = {};
const testContext = {};
const testStats = {};
const testFunctionId = 'testFunctionId';
const testFunctionResult = 'testFunctionResult';
beforeEach(() => {
module.options['function'] = testFunctionId;
module.getEvent = sinon.stub().returns(testEvent);
module.getContext = sinon.stub().returns(testContext);
});
it('should execute the given function handler', () => {
const testHandlerFunc = sinon.spy((e, c, cb) => cb(null, testFunctionResult));
module.loadHandler = sinon.stub().returns(testHandlerFunc);
return module
.run(testStats)
.then((res) => {
expect(res).to.equal(testFunctionResult);
expect(testHandlerFunc).to.have.been.calledWith(
testEvent,
testContext
);
});
});
it('should fail if the function handler returns an error', () => {
const testError = 'testError';
const testHandlerFunc = sinon.spy((e, c, cb) => cb(testError));
module.loadHandler = sinon.stub().returns(testHandlerFunc);
return module
.run(testStats)
.catch((res) => {
expect(res).to.equal(testError);
});
});
});
describe('watch', () => {
const testEvent = {};
const testContext = {};
const testStats = {};
const testFunctionId = 'testFunctionId';
const testFunctionResult = 'testFunctionResult';
beforeEach(() => {
module.options['function'] = testFunctionId;
module.getEvent = sinon.stub().returns(testEvent);
module.getContext = sinon.stub().returns(testContext);
});
it('should throw if webpack watch fails', () => {
const testError = 'testError';
webpackMock.compilerMock.watch = sinon.spy((opt, cb) => cb(testError));
expect(module.watch.bind(module)).to.throw(testError);
});
it('should throw if function handler fails', () => {
const testError = 'testHandlerError';
const testHandlerFunc = sinon.spy((e, c, cb) => cb(testError));
module.loadHandler = sinon.stub().returns(testHandlerFunc);
let testCb;
webpackMock.compilerMock.watch = sinon.spy((opt, cb) => {
testCb = cb;
cb(null, webpackMock.statsMock);
});
expect(module.watch.bind(module)).to.throw(testError);
});
it('should call the handler every time a compilation occurs', () => {
const testHandlerFunc = sinon.spy((e, c, cb) => cb(null, testFunctionResult));
module.loadHandler = sinon.stub().returns(testHandlerFunc);
let testCb;
webpackMock.compilerMock.watch = sinon.spy((opt, cb) => {
testCb = cb;
cb(null, webpackMock.statsMock);
});
module.watch();
expect(testHandlerFunc).to.have.callCount(1);
testCb(null, webpackMock.statsMock);
expect(testHandlerFunc).to.have.callCount(2);
});
});
});