-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathrun.test.js
184 lines (155 loc) · 5.81 KB
/
run.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
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
const sinon = require('sinon');
const Serverless = require('serverless');
const baseModule = require('../lib/run');
const webpackMock = require('webpack');
jest.mock('webpack');
jest.mock('../lib/utils', () => {
return {
guid: jest.fn().mockReturnValue('testguid'),
purgeCache: jest.fn()
};
});
describe('run', () => {
let sandbox;
let serverless;
let module;
let chdirStub;
beforeEach(() => {
sandbox = sinon.createSandbox();
serverless = new Serverless({ commands: ['print'], options: {}, serviceDir: null });
serverless.cli = {
log: jest.fn(),
consoleLog: jest.fn()
};
module = _.assign(
{
serverless,
options: {}
},
baseModule
);
chdirStub = sandbox.stub(process, 'chdir');
});
afterEach(() => {
// This will reset the mocks too
sandbox.restore();
});
describe('watch', () => {
let spawnStub;
beforeEach(() => {
spawnStub = jest.fn();
serverless.pluginManager.spawn = spawnStub;
});
it('should throw if webpack watch fails', () => {
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch = jest.fn().mockImplementation(() => {
throw new Error('Failed');
});
expect(watch()).rejects.toThrow('Failed');
});
it('should not spawn invoke local on first run', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
_.set(module, 'options.function', 'myFunction');
watch('invoke:local');
expect(spawnStub).toHaveBeenCalledTimes(0);
expect(module.isWatching).toBe(true);
});
it('should not spawn on watch first run', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
_.set(module, 'options.function', 'myFunction');
watch('compile:watch:compile');
expect(spawnStub).toHaveBeenCalledTimes(0);
expect(module.isWatching).toBe(true);
});
it('should spawn on watch second run', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
_.set(module, 'options.function', 'myFunction');
watch('compile:watch:compile');
watch('compile:watch:compile');
expect(spawnStub).toHaveBeenCalledTimes(1);
expect(module.isWatching).toBe(true);
});
it('should spawn invoke local on subsequent runs', () => {
module.isWatching = true;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
watch('invoke:local');
expect(spawnStub).toHaveBeenCalledTimes(1);
expect(spawnStub).toHaveBeenCalledWith('invoke:local');
expect(module.isWatching).toBe(true);
});
it('should not call given handler function on first run', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
const watchHandler = jest.fn().mockReturnValue(BbPromise.resolve());
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
watch(watchHandler);
expect(spawnStub).toHaveBeenCalledTimes(0);
expect(watchHandler).toHaveBeenCalledTimes(0);
expect(module.isWatching).toBe(true);
});
it('should call given handler function on subsequent runs', () => {
module.isWatching = true;
const watch = module.watch.bind(module);
const watchHandler = jest.fn().mockReturnValue(BbPromise.resolve());
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
watch(watchHandler);
expect(spawnStub).toHaveBeenCalledTimes(0);
expect(watchHandler).toHaveBeenCalledTimes(1);
expect(module.isWatching).toBe(true);
});
it('should reset the service path', () => {
module.isWatching = true;
module.originalServicePath = 'originalPath';
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
watch();
expect(serverless.config.servicePath).toEqual('originalPath');
expect(chdirStub.args).toEqual([['originalPath']]);
});
it('should turn on polling and set the default poll interval', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
module.options['webpack-use-polling'] = true;
watch();
expect(webpackMock.compilerMock.watch).toHaveBeenCalledWith(
{
poll: 3000
},
expect.any(Function)
);
});
it('should turn on polling and set the specified poll interval', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
const interval = (module.options['webpack-use-polling'] = _.now() % 10000);
watch();
expect(webpackMock.compilerMock.watch).toHaveBeenCalledWith(
{
poll: interval
},
expect.any(Function)
);
});
});
});