@@ -15,7 +15,9 @@ const expect = chai.expect;
15
15
const path = require ( 'path' ) ;
16
16
const testSetup = require ( '../helpers/setup' ) ;
17
17
const fs = require ( 'fs-extra' ) ;
18
- const exec = require ( 'child_process' ) . exec ;
18
+ const { exec, execSync, spawn } = require ( 'child_process' ) ;
19
+
20
+ const projectDir = path . resolve ( __dirname , '../' , '../' ) ;
19
21
20
22
describe ( 'bin/encore.js' , function ( ) {
21
23
// being functional tests, these can take quite long
@@ -213,4 +215,138 @@ module.exports = Encore.getWebpackConfig();
213
215
done ( ) ;
214
216
} ) ;
215
217
} ) ;
218
+
219
+ it ( 'Run the webpack-dev-server successfully' , ( done ) => {
220
+ testSetup . emptyTmpDir ( ) ;
221
+ const testDir = testSetup . createTestAppDir ( ) ;
222
+
223
+ fs . writeFileSync (
224
+ path . join ( testDir , 'package.json' ) ,
225
+ `{
226
+ "devDependencies": {
227
+ "@symfony/webpack-encore": "*"
228
+ }
229
+ }`
230
+ ) ;
231
+
232
+ fs . writeFileSync (
233
+ path . join ( testDir , 'webpack.config.js' ) ,
234
+ `
235
+ const Encore = require('../../index.js');
236
+ Encore
237
+ .enableSingleRuntimeChunk()
238
+ .setOutputPath('build/')
239
+ .setPublicPath('/build')
240
+ .addEntry('main', './js/no_require')
241
+ ;
242
+
243
+ module.exports = Encore.getWebpackConfig();
244
+ `
245
+ ) ;
246
+
247
+ const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
248
+ const abortController = new AbortController ( ) ;
249
+ const node = spawn ( 'node' , [ binPath , 'dev-server' , `--context=${ testDir } ` ] , {
250
+ cwd : testDir ,
251
+ env : Object . assign ( { } , process . env , { NO_COLORS : 'true' } ) ,
252
+ signal : abortController . signal
253
+ } ) ;
254
+
255
+ let stdout = '' ;
256
+ let stderr = '' ;
257
+
258
+ node . stdout . on ( 'data' , ( data ) => {
259
+ stdout += data . toString ( ) ;
260
+ } ) ;
261
+
262
+ node . stderr . on ( 'data' , ( data ) => {
263
+ stderr += data . toString ( ) ;
264
+ } ) ;
265
+
266
+ node . on ( 'error' , ( error ) => {
267
+ if ( error . name !== 'AbortError' ) {
268
+ throw new Error ( 'Error executing encore' , { cause : error } ) ;
269
+ }
270
+
271
+ expect ( stdout ) . to . contain ( 'Running webpack-dev-server ...' ) ;
272
+ expect ( stdout ) . to . contain ( 'Compiled successfully in' ) ;
273
+ expect ( stdout ) . to . contain ( 'webpack compiled successfully' ) ;
274
+
275
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Project is running at:' ) ;
276
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Loopback: http://localhost:8080/, http://127.0.0.1:8080/' ) ;
277
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Content not from webpack is served from' ) ;
278
+
279
+ done ( ) ;
280
+ } ) ;
281
+
282
+ setTimeout ( ( ) => {
283
+ abortController . abort ( ) ;
284
+ } , 5000 ) ;
285
+ } ) ;
286
+
287
+ describe ( 'Without webpack-dev-server installed' , ( ) => {
288
+ const webpackDevServerConstraint = require ( '../../package.json' ) . devDependencies [ 'webpack-dev-server' ] || null ;
289
+ if ( ! webpackDevServerConstraint ) {
290
+ throw new Error ( 'Missing "webpack-dev-server" as dev dependency in package.json.' ) ;
291
+ }
292
+
293
+ before ( ( ) => {
294
+ execSync ( 'yarn remove webpack-dev-server --dev' , { cwd : projectDir } ) ;
295
+ } ) ;
296
+
297
+ after ( ( ) => {
298
+ // Re-install webpack-dev-server and ensure the project is in a clean state
299
+ execSync ( `yarn add webpack-dev-server@${ webpackDevServerConstraint } --dev` , { cwd : projectDir } ) ;
300
+ execSync ( 'git checkout yarn.lock' , { cwd : projectDir } ) ;
301
+ execSync ( 'yarn install' , { cwd : projectDir } ) ;
302
+ } ) ;
303
+
304
+ it ( 'Throw an error when trying to use the webpack-dev-server if not installed' , done => {
305
+ testSetup . emptyTmpDir ( ) ;
306
+ const testDir = testSetup . createTestAppDir ( ) ;
307
+
308
+ fs . writeFileSync (
309
+ path . join ( testDir , 'package.json' ) ,
310
+ `{
311
+ "devDependencies": {
312
+ "@symfony/webpack-encore": "*"
313
+ }
314
+ }`
315
+ ) ;
316
+
317
+ fs . writeFileSync (
318
+ path . join ( testDir , 'webpack.config.js' ) ,
319
+ `
320
+ const Encore = require('../../index.js');
321
+ Encore
322
+ .enableSingleRuntimeChunk()
323
+ .setOutputPath('build/')
324
+ .setPublicPath('/build')
325
+ .addEntry('main', './js/no_require')
326
+ ;
327
+
328
+ module.exports = Encore.getWebpackConfig();
329
+ `
330
+ ) ;
331
+
332
+ const binPath = path . resolve ( projectDir , 'bin' , 'encore.js' ) ;
333
+ exec (
334
+ `node ${ binPath } dev-server --context=${ testDir } ` ,
335
+ {
336
+ cwd : testDir ,
337
+ env : Object . assign ( { } , process . env , { NO_COLORS : 'true' } )
338
+ } ,
339
+ ( err , stdout , stderr ) => {
340
+ expect ( stdout ) . to . contain ( 'Install webpack-dev-server to use the webpack Development Server' ) ;
341
+ expect ( stdout ) . to . contain ( 'npm install webpack-dev-server --save-dev' ) ;
342
+ expect ( stderr ) . to . equal ( '' ) ;
343
+
344
+ expect ( stdout ) . not . to . contain ( 'Running webpack-dev-server ...' ) ;
345
+ expect ( stdout ) . not . to . contain ( 'Compiled successfully in' ) ;
346
+ expect ( stdout ) . not . to . contain ( 'webpack compiled successfully' ) ;
347
+
348
+ done ( ) ;
349
+ } ) ;
350
+ } ) ;
351
+ } ) ;
216
352
} ) ;
0 commit comments