99
1010'use strict' ;
1111
12- const chai = require ( 'chai' ) ;
12+ import { describe , it , expect , beforeEach , afterEach , chai } from 'vitest' ;
13+ import getPort from "get-port" ;
1314chai . use ( require ( 'chai-fs' ) ) ;
14- const expect = chai . expect ;
1515const path = require ( 'path' ) ;
1616const testSetup = require ( '../helpers/setup' ) ;
1717const fs = require ( 'fs-extra' ) ;
18+ const { promisify } = require ( 'util' ) ;
1819const { exec, execSync, spawn } = require ( 'child_process' ) ;
19-
20+ const execAsync = promisify ( exec ) ;
2021const projectDir = path . resolve ( __dirname , '../' , '../' ) ;
2122
22- describe ( 'bin/encore.js' , function ( ) {
23- // being functional tests, these can take quite long
24- this . timeout ( 10000 ) ;
25-
26- it ( 'Basic smoke test' , ( done ) => {
27- testSetup . emptyTmpDir ( ) ;
23+ describe ( 'bin/encore.js' , { sequential : true , timeout : 10000 } , function ( ) {
24+ it ( 'Basic smoke test' , async ( ) => {
2825 const testDir = testSetup . createTestAppDir ( ) ;
2926
3027 fs . writeFileSync (
@@ -52,23 +49,19 @@ module.exports = Encore.getWebpackConfig();
5249 ) ;
5350
5451 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
55- exec ( `node ${ binPath } dev --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
56- if ( err ) {
57- throw new Error ( `Error executing encore: ${ err } ${ stderr } ${ stdout } ` ) ;
58- }
52+ try {
53+ const { stdout, stderr } = await execAsync ( `node ${ binPath } dev --context=${ testDir } ` , { cwd : testDir } ) ;
5954
6055 expect ( stdout ) . to . contain ( 'Compiled successfully' ) ;
61-
6256 expect ( stdout ) . not . to . contain ( 'Hash: ' ) ;
6357 expect ( stdout ) . not . to . contain ( 'Version: ' ) ;
6458 expect ( stdout ) . not . to . contain ( 'Time: ' ) ;
65-
66- done ( ) ;
67- } ) ;
59+ } catch ( err ) {
60+ throw new Error ( `Error executing encore: ${ err } ${ err . stderr } ${ err . stdout } ` ) ;
61+ }
6862 } ) ;
6963
70- it ( 'Smoke test using the --json option' , ( done ) => {
71- testSetup . emptyTmpDir ( ) ;
64+ it ( 'Smoke test using the --json option' , async ( ) => {
7265 const testDir = testSetup . createTestAppDir ( ) ;
7366
7467 fs . writeFileSync (
@@ -87,10 +80,8 @@ module.exports = Encore.getWebpackConfig();
8780 ) ;
8881
8982 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
90- exec ( `node ${ binPath } dev --json --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
91- if ( err ) {
92- throw new Error ( `Error executing encore: ${ err } ${ stderr } ${ stdout } ` ) ;
93- }
83+ try {
84+ const { stdout, stderr } = await execAsync ( `node ${ binPath } dev --json --context=${ testDir } ` , { cwd : testDir } ) ;
9485
9586 let parsedOutput = null ;
9687 try {
@@ -101,21 +92,18 @@ module.exports = Encore.getWebpackConfig();
10192
10293 expect ( parsedOutput ) . to . be . an ( 'object' ) ;
10394 expect ( parsedOutput . modules ) . to . be . an ( 'array' ) ;
104-
10595 // We expect 4 modules there:
10696 // - webpack/runtime/chunk loaded
10797 // - webpack/runtime/jsonp chunk loading
10898 // - webpack/runtime/hasOwnProperty shorthand
10999 // - ./js/no_require.js
110100 expect ( parsedOutput . modules . length ) . to . equal ( 4 ) ;
111-
112-
113- done ( ) ;
114- } ) ;
101+ } catch ( err ) {
102+ throw new Error ( `Error executing encore: ${ err } ${ err . stderr } ${ err . stdout } ` ) ;
103+ }
115104 } ) ;
116105
117- it ( 'Smoke test using the --profile option' , ( done ) => {
118- testSetup . emptyTmpDir ( ) ;
106+ it ( 'Smoke test using the --profile option' , async ( ) => {
119107 const testDir = testSetup . createTestAppDir ( ) ;
120108
121109 fs . writeFileSync (
@@ -134,22 +122,19 @@ module.exports = Encore.getWebpackConfig();
134122 ) ;
135123
136124 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
137- exec ( `node ${ binPath } dev --profile --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
138- if ( err ) {
139- throw new Error ( `Error executing encore: ${ err } ${ stderr } ${ stdout } ` ) ;
140- }
125+ try {
126+ const { stdout, stderr } = await execAsync ( `node ${ binPath } dev --profile --context=${ testDir } ` , { cwd : testDir } ) ;
141127
142128 expect ( stdout ) . to . contain ( 'resolving: ' ) ;
143129 expect ( stdout ) . to . contain ( 'restoring: ' ) ;
144130 expect ( stdout ) . to . contain ( 'integration: ' ) ;
145131 expect ( stdout ) . to . contain ( 'building: ' ) ;
146-
147- done ( ) ;
148- } ) ;
132+ } catch ( err ) {
133+ throw new Error ( `Error executing encore: ${ err } ${ err . stderr } ${ err . stdout } ` ) ;
134+ }
149135 } ) ;
150136
151- it ( 'Smoke test using the --keep-public-path option' , ( done ) => {
152- testSetup . emptyTmpDir ( ) ;
137+ it ( 'Smoke test using the --keep-public-path option' , async ( ) => {
153138 const testDir = testSetup . createTestAppDir ( ) ;
154139
155140 fs . writeFileSync (
@@ -168,17 +153,14 @@ module.exports = Encore.getWebpackConfig();
168153 ) ;
169154
170155 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
171- exec ( `node ${ binPath } dev --keep-public-path --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
172- if ( err ) {
173- throw new Error ( `Error executing encore: ${ err } ${ stderr } ${ stdout } ` ) ;
174- }
175-
176- done ( ) ;
177- } ) ;
156+ try {
157+ await execAsync ( `node ${ binPath } dev --keep-public-path --context=${ testDir } ` , { cwd : testDir } ) ;
158+ } catch ( err ) {
159+ throw new Error ( `Error executing encore: ${ err } ${ err . stderr } ${ err . stdout } ` ) ;
160+ }
178161 } ) ;
179162
180- it ( 'Display an error when calling an unknown method' , ( done ) => {
181- testSetup . emptyTmpDir ( ) ;
163+ it ( 'Display an error when calling an unknown method' , async ( ) => {
182164 const testDir = testSetup . createTestAppDir ( ) ;
183165
184166 fs . writeFileSync (
@@ -207,17 +189,18 @@ module.exports = Encore.getWebpackConfig();
207189 ) ;
208190
209191 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
210- exec ( `node ${ binPath } dev --context=${ testDir } ` , { cwd : testDir } , ( err , stdout , stderr ) => {
192+ try {
193+ await execAsync ( `node ${ binPath } dev --context=${ testDir } ` , { cwd : testDir } ) ;
194+ throw new Error ( 'Expected command to fail.' ) ;
195+ } catch ( err ) {
211196 expect ( err ) . not . to . be . null ;
212- expect ( stdout ) . to . contain ( 'is not a recognized property' ) ;
213- expect ( stdout ) . to . contain ( 'or method' ) ;
214- expect ( stdout ) . to . contain ( 'Did you mean' ) ;
215- done ( ) ;
216- } ) ;
197+ expect ( err . stdout ) . to . contain ( 'is not a recognized property' ) ;
198+ expect ( err . stdout ) . to . contain ( 'or method' ) ;
199+ expect ( err . stdout ) . to . contain ( 'Did you mean' ) ;
200+ }
217201 } ) ;
218202
219- it ( 'Run the webpack-dev-server successfully' , ( done ) => {
220- testSetup . emptyTmpDir ( ) ;
203+ it ( 'Run the webpack-dev-server successfully' , { timeout : 10000 } , async ( ) => {
221204 const testDir = testSetup . createTestAppDir ( ) ;
222205
223206 fs . writeFileSync (
@@ -246,57 +229,58 @@ module.exports = Encore.getWebpackConfig();
246229
247230 const binPath = path . resolve ( __dirname , '../' , '../' , 'bin' , 'encore.js' ) ;
248231 const abortController = new AbortController ( ) ;
249- const node = spawn ( 'node' , [ binPath , 'dev-server' , `--context=${ testDir } ` ] , {
250- cwd : testDir ,
251- env : Object . assign ( { } , process . env , { NO_COLOR : '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- }
232+ const port = await getPort ( ) ;
233+ await new Promise ( async ( resolve , reject ) => {
234+ const node = spawn ( 'node' , [ binPath , 'dev-server' , `--context=${ testDir } ` , `--port=${ port } ` ] , {
235+ cwd : testDir ,
236+ env : Object . assign ( { } , process . env , { NO_COLOR : 'true' , FORCE_COLOR : 'false' } ) ,
237+ signal : abortController . signal
238+ } ) ;
239+
240+ let stdout = '' ;
241+ let stderr = '' ;
242+
243+ node . stdout . on ( 'data' , ( data ) => {
244+ stdout += data . toString ( ) ;
245+ } ) ;
246+
247+ node . stderr . on ( 'data' , ( data ) => {
248+ stderr += data . toString ( ) ;
249+ } ) ;
250+
251+ node . on ( 'error' , ( error ) => {
252+ if ( error . name !== 'AbortError' ) {
253+ reject ( new Error ( 'Error executing encore' , { cause : error } ) ) ;
254+ }
270255
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' ) ;
256+ expect ( stdout ) . to . contain ( 'Running webpack-dev-server ...' ) ;
257+ expect ( stdout ) . to . contain ( 'Compiled successfully in' ) ;
258+ expect ( stdout ) . to . contain ( 'webpack compiled successfully' ) ;
274259
275- expect ( stderr ) . to . contain ( '[webpack-dev-server] Project is running at:' ) ;
276- expect ( stderr ) . to . contain ( ' [webpack-dev-server] Loopback: http://localhost:8080/' ) ;
277- expect ( stderr ) . to . contain ( '[webpack-dev-server] Content not from webpack is served from' ) ;
260+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Project is running at:' ) ;
261+ expect ( stderr ) . to . contain ( ` [webpack-dev-server] Loopback: http://localhost:${ port } /` ) ;
262+ expect ( stderr ) . to . contain ( '[webpack-dev-server] Content not from webpack is served from' ) ;
278263
279- done ( ) ;
280- } ) ;
264+ resolve ( ) ;
265+ } ) ;
281266
282- setTimeout ( ( ) => {
267+ await new Promise ( r => setTimeout ( r , 9000 ) ) ;
283268 abortController . abort ( ) ;
284- } , 5000 ) ;
269+ } )
285270 } ) ;
286271
287272 describe ( 'Without webpack-dev-server installed' , ( ) => {
288- before ( ( ) => {
273+ beforeEach ( ( ) => {
289274 execSync ( 'yarn remove webpack-dev-server --dev' , { cwd : projectDir } ) ;
290275 } ) ;
291276
292- after ( ( ) => {
277+ afterEach ( ( ) => {
293278 // Re-install webpack-dev-server and ensure the project is in a clean state
294- execSync ( 'git checkout package.json' , { cwd : projectDir } ) ;
279+ execSync ( 'git checkout package.json yarn.lock ' , { cwd : projectDir } ) ;
295280 execSync ( 'yarn install' , { cwd : projectDir } ) ;
296281 } ) ;
297282
298- it ( 'Throw an error when trying to use the webpack-dev-server if not installed' , done => {
299- testSetup . emptyTmpDir ( ) ;
283+ it ( 'Throw an error when trying to use the webpack-dev-server if not installed' , async ( ) => {
300284 const testDir = testSetup . createTestAppDir ( ) ;
301285
302286 fs . writeFileSync (
@@ -324,23 +308,26 @@ module.exports = Encore.getWebpackConfig();
324308 ) ;
325309
326310 const binPath = path . resolve ( projectDir , 'bin' , 'encore.js' ) ;
327- exec (
328- `node ${ binPath } dev-server --context=${ testDir } ` ,
329- {
311+ try {
312+ await execAsync ( `node ${ binPath } dev-server --context=${ testDir } ` , {
330313 cwd : testDir ,
331314 env : Object . assign ( { } , process . env , { NO_COLOR : 'true' } )
332- } ,
333- ( err , stdout , stderr ) => {
334- expect ( stdout ) . to . contain ( 'Install webpack-dev-server to use the webpack Development Server' ) ;
335- expect ( stdout ) . to . contain ( 'npm install webpack-dev-server --save-dev' ) ;
336- expect ( stderr ) . to . equal ( '' ) ;
315+ } ) ;
337316
338- expect ( stdout ) . not . to . contain ( 'Running webpack-dev-server ...' ) ;
339- expect ( stdout ) . not . to . contain ( 'Compiled successfully in' ) ;
340- expect ( stdout ) . not . to . contain ( 'webpack compiled successfully' ) ;
317+ throw new Error ( 'Expected command to fail.' ) ;
318+ } catch ( err ) {
319+ if ( ! 'stdout' in err || ! 'stderr' in err ) {
320+ throw new Error ( `Error executing encore: ${ err } ` ) ;
321+ }
341322
342- done ( ) ;
343- } ) ;
323+ expect ( err . stdout ) . to . contain ( 'Install webpack-dev-server to use the webpack Development Server' ) ;
324+ expect ( err . stdout ) . to . contain ( 'npm install webpack-dev-server --save-dev' ) ;
325+ expect ( err . stderr ) . to . equal ( '' ) ;
326+
327+ expect ( err . stdout ) . not . to . contain ( 'Running webpack-dev-server ...' ) ;
328+ expect ( err . stdout ) . not . to . contain ( 'Compiled successfully in' ) ;
329+ expect ( err . stdout ) . not . to . contain ( 'webpack compiled successfully' ) ;
330+ }
344331 } ) ;
345332 } ) ;
346333} ) ;
0 commit comments