@@ -4,10 +4,36 @@ jest.setTimeout(10E6);
4
4
/* eslint-disable node/no-unsupported-features */
5
5
/* eslint-disable node/no-unsupported-features/es-syntax */
6
6
7
- const { runWatch, extractSummary } = require ( "../../../testUtils" ) ;
7
+ const fs = require ( "fs" ) ;
8
+ const path = require ( "path" ) ;
9
+ const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require ( "../../../testUtils" ) ;
10
+
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
+ } ) ;
8
34
9
35
test ( "info-verbosity-verbose" , async done => {
10
- const result = await runWatch ( __dirname , [
36
+ const webpackProc = runAndGetWatchProc ( __dirname , [
11
37
"--entry " ,
12
38
"./index.js" ,
13
39
"--config" ,
@@ -22,15 +48,62 @@ test("info-verbosity-verbose", async done => {
22
48
"--info-verbosity" ,
23
49
"verbose"
24
50
] ) ;
25
- const { stdout, stderr } = result ;
26
51
27
- const summary = extractSummary ( stdout ) ;
52
+ // Watch mode with --info-verbosity verbose does not spit the output in one go.
53
+ // So we need to keep a track of chunks output order
54
+ // 1. Compilation starting...
55
+ // 2. webpack is watching the files...
56
+ // 3. Compilation finished
57
+ // 4. Hash and other info
58
+ // 5. Compilation starting...
59
+ // 6. Compilation finished
60
+ // 7. Hash and other info
61
+ var chunkNumber = 0 ;
62
+ var hash1 , hash2 ;
63
+
64
+ webpackProc . stdout . on ( "data" , data => {
65
+ data = data . toString ( ) ;
66
+ chunkNumber ++ ;
67
+
68
+ switch ( chunkNumber ) {
69
+ case 1 :
70
+ case 5 :
71
+ expect ( data ) . toContain ( "Compilation starting" ) ;
72
+ break ;
73
+ case 2 :
74
+ expect ( data ) . toContain ( "webpack is watching the files" ) ;
75
+ break ;
76
+ case 3 :
77
+ case 6 :
78
+ expect ( data ) . toContain ( "Compilation finished" ) ;
79
+ break ;
80
+ case 4 :
81
+ expect ( extractSummary ( data ) ) . toMatchSnapshot ( ) ;
82
+
83
+ hash1 = extractHash ( data ) ;
84
+
85
+ // We get webpack output after running test
86
+ // Since we are running the webpack in watch mode, changing file will generate additional output
87
+ // First time output will be validated fully
88
+ // Hash of the The subsequent output will be tested against that of first time output
89
+ appendDataIfFileExists ( __dirname , fileToChange , "//junk-comment" ) ;
90
+
91
+ break ;
92
+ case 7 :
93
+ hash2 = extractHash ( data ) ;
94
+
95
+ expect ( hash2 . hash ) . not . toBe ( hash1 . hash ) ;
28
96
29
- expect ( summary ) . toEqual ( expect . anything ( ) ) ;
30
- expect ( summary ) . toContain ( "Compilation" ) ;
31
- expect ( summary ) . toContain ( "webpack is watching the files…" ) ;
97
+ webpackProc . kill ( ) ;
98
+ done ( ) ;
99
+ break ;
100
+ default :
101
+ break ;
102
+ }
103
+ } ) ;
32
104
33
- expect ( stderr ) . toHaveLength ( 0 ) ;
34
- expect ( summary ) . toMatchSnapshot ( ) ;
35
- done ( ) ;
105
+ webpackProc . stderr . on ( "data" , error => {
106
+ // fail test case if there is any error
107
+ done ( error . toString ( ) ) ;
108
+ } ) ;
36
109
} ) ;
0 commit comments