-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathwpwatch.test.js
287 lines (243 loc) · 9.02 KB
/
wpwatch.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
const Serverless = require('serverless');
jest.mock('webpack');
jest.mock('../lib/utils');
describe('wpwatch', () => {
let serverless;
let module;
let spawnStub;
let webpackMock;
let baseModule;
beforeEach(() => {
jest.resetModules();
webpackMock = require('webpack');
baseModule = require('../lib/wpwatch');
serverless = new Serverless({ commands: ['print'], options: {}, serviceDir: null });
serverless.cli = {
log: jest.fn(),
consoleLog: jest.fn()
};
module = _.assign(
{
serverless,
options: {}
},
baseModule
);
spawnStub = jest.fn();
serverless.pluginManager.spawn = spawnStub;
const webpackConfig = {
stats: 'minimal'
};
_.set(module, 'webpackConfig', webpackConfig);
});
it('should reject if webpack watch fails', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(new Error('Failed')));
return expect(wpwatch()).rejects.toThrow('Failed');
});
it('should spawn compile if watch is disabled', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
spawnStub.mockResolvedValue();
_.set(module.options, 'webpack-no-watch', true);
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledWith('webpack:compile'),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(0)
)
);
});
it('should enter watch mode and return after first compile', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
spawnStub.mockResolvedValue();
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledTimes(0),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(1)
)
);
});
it('should still enter watch mode and return if lastHash is the same as previous', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, { hash: null }));
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledTimes(0),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(1),
expect(spawnStub).toHaveBeenCalledTimes(0)
)
);
});
it('should work if no stats are returned', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb());
spawnStub.mockResolvedValue();
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledTimes(0),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(1)
)
);
});
it('should enable polling with command line switch', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb());
spawnStub.mockResolvedValue();
_.set(module.options, 'webpack-use-polling', true);
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledTimes(0),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(1),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledWith(
{
poll: 3000
},
expect.any(Function)
)
)
);
});
it('should set specific polling interval if given with switch', () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.watch.mockReset();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb());
spawnStub.mockResolvedValue();
_.set(module.options, 'webpack-use-polling', 5000);
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(spawnStub).toHaveBeenCalledTimes(0),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledTimes(1),
expect(webpackMock.compilerMock.watch).toHaveBeenCalledWith(
{
poll: 5000
},
expect.any(Function)
)
)
);
});
it('should spawn webpack:compile:watch on subsequent runs', () => {
const wpwatch = module.wpwatch.bind(module);
let watchCallbackCount = 0;
let beforeCompileCallback;
spawnStub.mockResolvedValue();
webpackMock.compilerMock.hooks.beforeCompile.tapPromise.mockImplementation((options, cb) => {
beforeCompileCallback = cb;
});
webpackMock.compilerMock.watch.mockImplementationOnce((options, cb) => {
cb(null, { call: 1, hash: '1' });
watchCallbackCount++;
cb(null, { call: 2, hash: '2' });
watchCallbackCount++;
// We only call this once, to simulate that promises that might take longer to resolve
// don't cause a re-emit to avoid race-conditions.
beforeCompileCallback();
cb(null, { call: 3, hash: '3' });
watchCallbackCount++;
cb(null, { call: 3, hash: '4' });
watchCallbackCount++;
});
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() =>
BbPromise.join(
expect(watchCallbackCount).toBe(4),
expect(spawnStub).toHaveBeenCalledTimes(1),
expect(spawnStub).toHaveBeenCalledWith('webpack:compile:watch')
)
);
});
it('should spawn more webpack:compile:watch when previous is resolved', () => {
const wpwatch = module.wpwatch.bind(module);
let watchCallbackCount = 0;
let beforeCompileCallback;
let beforeCompileCallbackPromise;
spawnStub.mockResolvedValue();
webpackMock.compilerMock.hooks.beforeCompile.tapPromise.mockImplementation((options, cb) => {
beforeCompileCallback = cb;
});
webpackMock.compilerMock.watch.mockImplementationOnce((options, cb) => {
cb(null, { call: 1, hash: '1' });
watchCallbackCount++;
cb(null, { call: 2, hash: '2' });
watchCallbackCount++;
// eslint-disable-next-line promise/always-return
beforeCompileCallbackPromise = beforeCompileCallback().then(() => {
// eslint-disable-next-line promise/no-callback-in-promise
cb(null, { call: 3, hash: '3' });
watchCallbackCount++;
});
});
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() => beforeCompileCallbackPromise)
.then(() =>
BbPromise.join(
expect(watchCallbackCount).toBe(3),
expect(spawnStub).toHaveBeenCalledTimes(2),
expect(spawnStub).toHaveBeenCalledWith('webpack:compile:watch')
)
);
});
it("should use plugins for webpack:compile:watch if hooks doesn't exist", () => {
const wpwatch = module.wpwatch.bind(module);
webpackMock.compilerMock.hooks = false;
webpackMock.compilerMock.plugin.mockImplementation((name, cb) => cb(null, _.noop));
webpackMock.compilerMock.watch.mockImplementation((options, cb) => cb(null, {}));
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() => expect(webpackMock.compilerMock.plugin).toHaveBeenCalledTimes(1));
});
it('should not resolve before compile if it has an error', () => {
const wpwatch = module.wpwatch.bind(module);
spawnStub.mockReturnValue(BbPromise.reject(new Error('actual error')));
let beforeCompileCallback;
webpackMock.compilerMock.hooks.beforeCompile.tapPromise.mockImplementation((options, cb) => {
beforeCompileCallback = cb;
});
let doesResolve = false;
webpackMock.compilerMock.watch.mockImplementationOnce((options, cb) => {
cb(null, { call: 1, hash: '1' });
cb(null, { call: 2, hash: '2' });
// eslint-disable-next-line promise/catch-or-return,promise/always-return
beforeCompileCallback().then(() => {
// We don't expect this to be set to true
doesResolve = true;
});
});
return expect(wpwatch())
.resolves.toBeUndefined()
.then(() => expect(doesResolve).toBe(false));
});
it('should throw if compile fails on subsequent runs', () => {
const wpwatch = module.wpwatch.bind(module);
spawnStub.mockResolvedValue();
webpackMock.compilerMock.watch.mockImplementation((options, cb) => {
cb(null, { call: 3, hash: '3' });
cb(new Error('Compile failed'));
});
return expect(wpwatch()).resolves.toBeUndefined();
});
});