@@ -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,128 @@ 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 } ` ] , { cwd : testDir , signal : abortController . signal } ) ;
250
+
251
+ let stdout = '' ;
252
+ let stderr = '' ;
253
+
254
+ node . stdout . on ( 'data' , ( data ) => {
255
+ stdout += data . toString ( ) ;
256
+ } ) ;
257
+
258
+ node . stderr . on ( 'data' , ( data ) => {
259
+ stderr += data . toString ( ) ;
260
+ } ) ;
261
+
262
+ node . on ( 'error' , ( error ) => {
263
+ if ( error . name !== 'AbortError' ) {
264
+ throw new Error ( 'Error executing encore' , { cause : error } ) ;
265
+ }
266
+
267
+ expect ( stdout ) . to . contain ( 'Running webpack-dev-server ...' ) ;
268
+ expect ( stdout ) . to . contain ( 'Compiled successfully in' ) ;
269
+ expect ( stdout ) . to . contain ( 'webpack compiled successfully' ) ;
270
+
271
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Project is running at:' ) ;
272
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Loopback: http://localhost:8080/, http://127.0.0.1:8080/' ) ;
273
+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Content not from webpack is served from' ) ;
274
+
275
+ done ( ) ;
276
+ } ) ;
277
+
278
+ setTimeout ( ( ) => {
279
+ abortController . abort ( ) ;
280
+ } , 2500 ) ;
281
+ } ) ;
282
+
283
+ describe ( 'Without webpack-dev-server installed' , ( ) => {
284
+ const webpackDevServerConstraint = require ( '../../package.json' ) . devDependencies [ 'webpack-dev-server' ] || null ;
285
+ if ( ! webpackDevServerConstraint ) {
286
+ throw new Error ( 'Missing "webpack-dev-server" as dev dependency in package.json.' ) ;
287
+ }
288
+
289
+ before ( ( ) => {
290
+ execSync ( 'yarn remove webpack-dev-server --dev' , { cwd : projectDir } ) ;
291
+ } ) ;
292
+
293
+ after ( ( ) => {
294
+ // Re-install webpack-dev-server and ensure the project is in a clean state
295
+ execSync ( `yarn add webpack-dev-server@${ webpackDevServerConstraint } --dev` , { cwd : projectDir } ) ;
296
+ execSync ( 'git checkout yarn.lock' , { cwd : projectDir } ) ;
297
+ execSync ( 'yarn install' , { cwd : projectDir } ) ;
298
+ } ) ;
299
+
300
+ it ( 'Throw an error when trying to use the webpack-dev-server if not installed' , done => {
301
+ testSetup . emptyTmpDir ( ) ;
302
+ const testDir = testSetup . createTestAppDir ( ) ;
303
+
304
+ fs . writeFileSync (
305
+ path . join ( testDir , 'package.json' ) ,
306
+ `{
307
+ "devDependencies": {
308
+ "@symfony/webpack-encore": "*"
309
+ }
310
+ }`
311
+ ) ;
312
+
313
+ fs . writeFileSync (
314
+ path . join ( testDir , 'webpack.config.js' ) ,
315
+ `
316
+ const Encore = require('../../index.js');
317
+ Encore
318
+ .enableSingleRuntimeChunk()
319
+ .setOutputPath('build/')
320
+ .setPublicPath('/build')
321
+ .addEntry('main', './js/no_require')
322
+ ;
323
+
324
+ module.exports = Encore.getWebpackConfig();
325
+ `
326
+ ) ;
327
+
328
+ const binPath = path . resolve ( projectDir , 'bin' , 'encore.js' ) ;
329
+ exec ( `node ${ binPath } dev-server --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
330
+ expect ( stdout ) . to . contain ( 'Install webpack-dev-server to use the webpack Development Server' ) ;
331
+ expect ( stdout ) . to . contain ( 'npm install webpack-dev-server --save-dev' ) ;
332
+ expect ( stderr ) . to . equal ( '' ) ;
333
+
334
+ expect ( stdout ) . not . to . contain ( 'Running webpack-dev-server ...' ) ;
335
+ expect ( stdout ) . not . to . contain ( 'Compiled successfully in' ) ;
336
+ expect ( stdout ) . not . to . contain ( 'webpack compiled successfully' ) ;
337
+
338
+ done ( ) ;
339
+ } ) ;
340
+ } ) ;
341
+ } ) ;
216
342
} ) ;
0 commit comments