1
1
"use strict" ;
2
2
3
+ jest . setTimeout ( 10E6 ) ;
3
4
/* eslint-disable node/no-unsupported-features */
4
5
/* eslint-disable node/no-unsupported-features/es-syntax */
5
6
6
- jest . setTimeout ( 10E6 ) ;
7
+ const fs = require ( "fs" ) ;
8
+ const path = require ( "path" ) ;
9
+ const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require ( "../../../testUtils" ) ;
7
10
8
- const { runWatch, extractSummary } = require ( "../../../testUtils" ) ;
11
+ const fileToChange = "index.js" ;
12
+ const copyFile = "index_copy.js" ;
13
+ const fileToChangePath = path . resolve ( __dirname , fileToChange ) ;
14
+ const copyFilePath = path . resolve ( __dirname , copyFile ) ;
15
+
16
+ // create copy of "index.js" => "index_copy.js"
17
+ beforeEach ( ( ) => {
18
+ // fs.copyFileSync was added in Added in: v8.5.0
19
+ // We should refactor the below code once our minimal supported version is v8.5.0
20
+ fs . createReadStream ( fileToChangePath ) . pipe ( fs . createWriteStream ( copyFilePath ) ) ;
21
+ } ) ;
22
+
23
+ afterEach ( ( ) => {
24
+ try {
25
+ // subsequent test-case runs won't pass as snapshot is not matched
26
+ // hence, deleting the file as it is modified by the test
27
+ fs . unlinkSync ( fileToChangePath ) ;
28
+ } catch ( e ) {
29
+ console . warn ( "could not remove the file:" + fileToChangePath + "\n" + e . message ) ;
30
+ } finally {
31
+ fs . renameSync ( copyFilePath , fileToChangePath ) ;
32
+ }
33
+ } ) ;
9
34
10
35
test ( "multi-config-watch-opt" , async done => {
11
- const result = await runWatch ( __dirname , [
36
+ const webpackProc = runAndGetWatchProc ( __dirname , [
12
37
"--entry" ,
13
38
"./index.js" ,
14
39
"--config" ,
@@ -22,16 +47,53 @@ test("multi-config-watch-opt", async done => {
22
47
"--watch"
23
48
] ) ;
24
49
25
- const { stdout, stderr } = result ;
50
+ // info-verbosity is set to info by default
51
+ // It does not spit the output in one go.
52
+ // So we need to keep a track of chunks output order
53
+ // 1. webpack is watching the files...
54
+ // 2. Hash and other info
55
+ // 3. (file changed) Hash and other info
56
+ var chunkNumber = 0 ;
57
+ var hash1 , hash2 ;
58
+
59
+ webpackProc . stdout . on ( "data" , data => {
60
+ data = data . toString ( ) ;
61
+ chunkNumber ++ ;
26
62
27
- const summary = extractSummary ( stdout ) ;
63
+ console . log ( data ) ;
28
64
29
- expect ( summary ) . toContain ( "" ) ;
30
- expect ( summary ) . toEqual ( expect . anything ( ) ) ;
31
- expect ( summary ) . toContain ( "" ) ;
32
- expect ( summary ) . toContain ( "webpack is watching the files…" ) ;
65
+ switch ( chunkNumber ) {
66
+ case 1 :
67
+ expect ( data ) . toContain ( "webpack is watching the files" ) ;
68
+ break ;
69
+ case 2 :
70
+ expect ( extractSummary ( data ) ) . toMatchSnapshot ( ) ;
33
71
34
- expect ( stderr ) . toHaveLength ( 0 ) ;
35
- expect ( summary ) . toMatchSnapshot ( ) ;
36
- done ( ) ;
72
+ hash1 = extractHash ( data ) ;
73
+
74
+ // We get webpack output after running test
75
+ // Since we are running the webpack in watch mode, changing file will generate additional output
76
+ // First time output will be validated fully
77
+ // Hash of the The subsequent output will be tested against that of first time output
78
+ appendDataIfFileExists ( __dirname , fileToChange , "//junk-comment" ) ;
79
+
80
+ break ;
81
+ case 3 :
82
+ hash2 = extractHash ( data ) ;
83
+
84
+ expect ( hash2 . hash ) . not . toBe ( hash1 . hash ) ;
85
+
86
+ webpackProc . kill ( ) ;
87
+ done ( ) ;
88
+ break ;
89
+ default :
90
+ break ;
91
+ }
92
+ } ) ;
93
+
94
+ webpackProc . stderr . on ( "data" , error => {
95
+ // fail test case if there is any error
96
+ done ( error . toString ( ) ) ;
97
+ } ) ;
37
98
} ) ;
99
+
0 commit comments