-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathgoogleInvokeLocal.test.js
188 lines (160 loc) · 6.42 KB
/
googleInvokeLocal.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
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
'use strict';
const sinon = require('sinon');
const GoogleProvider = require('../provider/googleProvider');
const GoogleInvokeLocal = require('./googleInvokeLocal');
const Serverless = require('../test/serverless');
describe('GoogleInvokeLocal', () => {
let serverless;
const functionName = 'myFunction';
const rawOptions = {
f: functionName,
};
const processedOptions = {
function: functionName,
};
let googleInvokeLocal;
beforeAll(() => {
serverless = new Serverless();
serverless.setProvider('google', new GoogleProvider(serverless));
googleInvokeLocal = new GoogleInvokeLocal(serverless, rawOptions);
serverless.processedInput.options = processedOptions;
});
describe('#constructor()', () => {
it('should set the serverless instance', () => {
expect(googleInvokeLocal.serverless).toEqual(serverless);
});
it('should set the raw options if provided', () => {
expect(googleInvokeLocal.options).toEqual(rawOptions);
});
it('should make the provider accessible', () => {
expect(googleInvokeLocal.provider).toBeInstanceOf(GoogleProvider);
});
it.each`
method
${'validate'}
${'setDefaults'}
${'getDataAndContext'}
${'invokeLocalNodeJs'}
${'loadFileInOption'}
${'validateEventsProperty'}
${'addEnvironmentVariablesToProcessEnv'}
`('should declare $method method', ({ method }) => {
expect(googleInvokeLocal[method]).toBeDefined();
});
describe('hooks', () => {
let validateStub;
let setDefaultsStub;
let getDataAndContextStub;
let invokeLocalStub;
beforeAll(() => {
validateStub = sinon.stub(googleInvokeLocal, 'validate').resolves();
setDefaultsStub = sinon.stub(googleInvokeLocal, 'setDefaults').resolves();
getDataAndContextStub = sinon.stub(googleInvokeLocal, 'getDataAndContext').resolves();
invokeLocalStub = sinon.stub(googleInvokeLocal, 'invokeLocal').resolves();
});
afterEach(() => {
googleInvokeLocal.validate.resetHistory();
googleInvokeLocal.setDefaults.resetHistory();
googleInvokeLocal.getDataAndContext.resetHistory();
googleInvokeLocal.invokeLocal.resetHistory();
});
afterAll(() => {
googleInvokeLocal.validate.restore();
googleInvokeLocal.setDefaults.restore();
googleInvokeLocal.getDataAndContext.restore();
googleInvokeLocal.invokeLocal.restore();
});
it.each`
hook
${'initialize'}
${'before:invoke:local:invoke'}
${'invoke:local:invoke'}
`('should declare $hook hook', ({ hook }) => {
expect(googleInvokeLocal.hooks[hook]).toBeDefined();
});
describe('initialize hook', () => {
it('should override raw options with processed options', () => {
googleInvokeLocal.hooks.initialize();
expect(googleInvokeLocal.options).toEqual(processedOptions);
});
});
describe('before:invoke:local:invoke hook', () => {
it('should validate the configuration', async () => {
await googleInvokeLocal.hooks['before:invoke:local:invoke']();
expect(validateStub.calledOnce).toEqual(true);
});
it('should set the defaults values', async () => {
await googleInvokeLocal.hooks['before:invoke:local:invoke']();
expect(setDefaultsStub.calledOnce).toEqual(true);
});
it('should resolve the data and the context of the invocation', async () => {
await googleInvokeLocal.hooks['before:invoke:local:invoke']();
expect(getDataAndContextStub.calledOnce).toEqual(true);
});
});
describe('invoke:local:invoke hook', () => {
it('should invoke the function locally', () => {
googleInvokeLocal.hooks['invoke:local:invoke']();
expect(invokeLocalStub.calledOnce).toEqual(true);
});
});
});
});
describe('#invokeLocal()', () => {
const functionObj = Symbol('functionObj');
const data = Symbol('data');
const context = Symbol('context');
const runtime = 'nodejs14';
let getFunctionStub;
let validateEventsPropertyStub;
let getRuntimeStub;
let invokeLocalNodeJsStub;
beforeAll(() => {
googleInvokeLocal.options = {
...processedOptions, // invokeLocal is called after the initialize hook which override the options
data, // data and context are populated by getDataAndContext in before:invoke:local:invoke hook
context,
};
getFunctionStub = sinon.stub(serverless.service, 'getFunction').returns(functionObj);
validateEventsPropertyStub = sinon
.stub(googleInvokeLocal, 'validateEventsProperty')
.returns();
getRuntimeStub = sinon.stub(googleInvokeLocal.provider, 'getRuntime').returns(runtime);
invokeLocalNodeJsStub = sinon.stub(googleInvokeLocal, 'invokeLocalNodeJs').resolves();
});
afterEach(() => {
serverless.service.getFunction.resetHistory();
googleInvokeLocal.validateEventsProperty.resetHistory();
googleInvokeLocal.provider.getRuntime.resetHistory();
googleInvokeLocal.invokeLocalNodeJs.resetHistory();
});
afterAll(() => {
serverless.service.getFunction.restore();
googleInvokeLocal.validateEventsProperty.restore();
googleInvokeLocal.provider.getRuntime.restore();
googleInvokeLocal.invokeLocalNodeJs.restore();
});
it('should get the function configuration', async () => {
await googleInvokeLocal.invokeLocal();
expect(getFunctionStub.calledOnceWith(functionName)).toEqual(true);
});
it('should validate the function configuration', async () => {
await googleInvokeLocal.invokeLocal();
expect(validateEventsPropertyStub.calledOnceWith(functionObj, functionName)).toEqual(true);
});
it('should get the runtime', async () => {
await googleInvokeLocal.invokeLocal();
expect(getRuntimeStub.calledOnceWith(functionObj)).toEqual(true);
});
it('should invoke locally the function with node js', async () => {
await googleInvokeLocal.invokeLocal();
expect(invokeLocalNodeJsStub.calledOnceWith(functionObj, data, context)).toEqual(true);
});
it('should throw if the runtime is not node js', async () => {
getRuntimeStub.returns('python3');
await expect(googleInvokeLocal.invokeLocal()).rejects.toThrow(
'Local invocation with runtime python3 is not supported'
);
});
});
});