-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
index.spec.js
368 lines (298 loc) · 10.9 KB
/
index.spec.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
'use strict'
const chai = require('chai')
const mockery = require('mockery')
const Promise = require('bluebird')
const sinon = require('sinon')
const expect = chai.expect
chai.use(require('sinon-chai'))
const webpack = sinon.stub()
mockery.enable({
warnOnUnregistered: false,
})
mockery.registerMock('webpack', webpack)
const preprocessor = require('../../index')
const typescriptOverrides = require('../../lib/typescript-overrides')
describe('webpack preprocessor', function () {
beforeEach(function () {
webpack.reset()
sinon.restore()
this.watchApi = {
close: sinon.spy(),
}
this.compilerApi = {
run: sinon.stub(),
watch: sinon.stub().returns(this.watchApi),
plugin: sinon.stub(),
}
webpack.returns(this.compilerApi)
this.statsApi = {
hasErrors () {
return false
},
toJson () {
return { warnings: [], errors: [] }
},
}
this.file = {
filePath: 'path/to/file.js',
outputPath: 'output/output.js',
shouldWatch: false,
on: sinon.stub(),
emit: sinon.spy(),
}
this.util = {
getOutputPath: sinon.stub().returns(this.outputPath),
fileUpdated: sinon.spy(),
onClose: sinon.stub(),
}
this.run = (options, file = this.file) => {
return preprocessor(options)(file)
}
})
describe('exported function', function () {
it('receives user options and returns a preprocessor function', function () {
expect(preprocessor(this.options)).to.be.a('function')
})
it('has defaultOptions attached to it', function () {
expect(preprocessor.defaultOptions).to.be.an('object')
expect(preprocessor.defaultOptions.webpackOptions.module.rules).to.be.an('array')
})
it('defaultOptions are deeply cloned, preserving regexes', () => {
expect(preprocessor.defaultOptions.webpackOptions.module.rules[0].test).to.be.an.instanceOf(RegExp)
})
})
describe('preprocessor function', function () {
afterEach(function () {
this.file.on.withArgs('close').yield() // resets the cached bundles
})
describe('when it finishes cleanly', function () {
beforeEach(function () {
this.compilerApi.run.yields(null, this.statsApi)
})
it('runs webpack', function () {
expect(preprocessor.__bundles()[this.file.filePath]).to.be.undefined
return this.run().then(() => {
expect(preprocessor.__bundles()[this.file.filePath].deferreds).to.be.empty
expect(preprocessor.__bundles()[this.file.filePath].promise).to.be.instanceOf(Promise)
expect(webpack).to.be.called
})
})
it('returns existing bundle if called again with same filePath', function () {
webpack.reset()
webpack.returns(this.compilerApi)
const run = preprocessor(this.options)
run(this.file)
run(this.file)
expect(webpack).to.be.calledOnce
})
it('specifies the entry file', function () {
return this.run().then(() => {
expect(webpack).to.be.calledWithMatch({
entry: [this.file.filePath],
})
})
})
it('includes additional entry files', function () {
return this.run({
additionalEntries: ['entry-1.js', 'entry-2.js'],
}).then(() => {
expect(webpack).to.be.calledWithMatch({
entry: [
this.file.filePath,
'entry-1.js',
'entry-2.js',
],
})
})
})
it('specifies output path and filename', function () {
return this.run().then(() => {
expect(webpack).to.be.calledWithMatch({
output: {
path: 'output',
filename: 'output.js',
},
})
})
})
it('adds .js extension to filename when the originating file had been no javascript file', function () {
this.file.outputPath = 'output/output.ts'
return this.run().then(() => {
expect(webpack.lastCall.args[0].output).to.eql({
path: 'output',
filename: 'output.ts.js',
})
})
})
describe('devtool', function () {
beforeEach((() => {
sinon.stub(typescriptOverrides, 'overrideSourceMaps')
}))
it('enables inline source maps', function () {
return this.run().then(() => {
expect(webpack).to.be.calledWithMatch({
devtool: 'inline-source-map',
})
expect(typescriptOverrides.overrideSourceMaps).to.be.calledWith(true)
})
})
it('does not enable inline source maps when devtool is false', function () {
const options = { webpackOptions: { devtool: false } }
return this.run(options).then(() => {
expect(webpack).to.be.calledWithMatch({
devtool: false,
})
expect(typescriptOverrides.overrideSourceMaps).to.be.calledWith(false)
})
})
it('always sets devtool even when mode is "production"', function () {
const options = { webpackOptions: { mode: 'production' } }
return this.run(options).then(() => {
expect(webpack).to.be.calledWithMatch({
devtool: 'inline-source-map',
})
expect(typescriptOverrides.overrideSourceMaps).to.be.calledWith(true)
})
})
})
describe('mode', function () {
it('sets mode to development by default', function () {
return this.run().then(() => {
expect(webpack).to.be.calledWithMatch({
mode: 'development',
})
})
})
it('follows user mode if present', function () {
const options = { webpackOptions: { mode: 'production' } }
return this.run(options).then(() => {
expect(webpack).to.be.calledWithMatch({
mode: 'production',
})
})
})
})
it('runs when shouldWatch is false', function () {
return this.run().then(() => {
expect(this.compilerApi.run).to.be.called
})
})
it('watches when shouldWatch is true', function () {
this.file.shouldWatch = true
this.compilerApi.watch.yields(null, this.statsApi)
return this.run().then(() => {
expect(this.compilerApi.watch).to.be.called
})
})
it('includes watchOptions if provided', function () {
this.file.shouldWatch = true
this.compilerApi.watch.yields(null, this.statsApi)
const options = { watchOptions: { poll: true } }
return this.run(options).then(() => {
expect(this.compilerApi.watch).to.be.calledWith({
poll: true,
})
})
})
it('resolves with the output path', function () {
return this.run().then((outputPath) => {
expect(outputPath).to.be.equal(this.file.outputPath)
})
})
it('adds .js extension and resolves with that output path when the originating file had been no javascript file', function () {
this.file.outputPath = 'output/output.ts'
return this.run().then((outputPath) => {
expect(outputPath).to.be.equal('output/output.ts.js')
})
})
it('emits "rerun" when shouldWatch is true after there is an update', function () {
this.file.shouldWatch = true
this.compilerApi.watch.yields(null, this.statsApi)
this.compilerApi.plugin.withArgs('compile').yields()
return this.run()
.then(() => {
expect(this.file.emit).not.to.be.calledWith('rerun')
this.compilerApi.plugin.withArgs('compile').yield()
this.compilerApi.watch.yield(null, this.statsApi)
return Promise.delay(11) // give assertion time till next tick
})
.then(() => {
expect(this.file.emit).to.be.calledWith('rerun')
})
})
it('does not emit "rerun" when shouldWatch is false', function () {
this.file.shouldWatch = false
this.compilerApi.plugin.withArgs('compile').yields()
return this.run().then(() => {
expect(this.file.emit).not.to.be.calledWith('rerun')
})
})
it('closes bundler when shouldWatch is true and `close` is emitted', function () {
this.file.shouldWatch = true
this.compilerApi.watch.yields(null, this.statsApi)
return this.run().then(() => {
this.file.on.withArgs('close').yield()
expect(this.watchApi.close).to.be.called
})
})
it('does not close bundler when shouldWatch is false and `close` is emitted', function () {
return this.run().then(() => {
this.file.on.withArgs('close').yield()
expect(this.watchApi.close).not.to.be.called
})
})
it('uses default webpack options when no user options', function () {
return this.run().then(() => {
expect(webpack.lastCall.args[0].module.rules[0].use).to.have.length(1)
expect(webpack.lastCall.args[0].module.rules[0].use[0].loader).to.be.a('string')
})
})
it('uses default options when no user webpack options', function () {
return this.run({}).then(() => {
expect(webpack.lastCall.args[0].module.rules[0].use).to.have.length(1)
expect(webpack.lastCall.args[0].module.rules[0].use[0].loader).to.be.a('string')
})
})
it('does not use default options when user options are non-default', function () {
const options = { webpackOptions: { module: { rules: [] } } }
return this.run(options).then(() => {
expect(webpack).to.be.calledWithMatch({
module: options.webpackOptions.module,
})
})
})
})
describe('when it errors', function () {
beforeEach(function () {
this.err = {
stack: 'Failed to preprocess...',
}
})
it('it rejects with error when an err', function () {
this.compilerApi.run.yields(this.err)
expect(preprocessor.__bundles()[this.file.filePath]).to.be.undefined
return this.run().catch((err) => {
expect(preprocessor.__bundles()[this.file.filePath].deferreds).to.be.empty
expect(preprocessor.__bundles()[this.file.filePath].promise).to.be.instanceOf(Promise)
expect(err.stack).to.equal(this.err.stack)
})
})
it('it rejects with joined errors when a stats err and strips stacktrace', function () {
const errs = ['foo\nat Object.foo', 'bar', 'baz']
const errsNoStack = ['foo', 'bar', 'baz']
this.statsApi = {
hasErrors () {
return true
},
toJson () {
return { warnings: [], errors: errs }
},
}
this.compilerApi.run.yields(null, this.statsApi)
return this.run().catch((err) => {
expect(err.message).to.equal(`Webpack Compilation Error\n${errsNoStack.join('\n\n')}`)
})
})
})
})
})