1
1
"use strict" ;
2
2
3
- const { runWatch, extractSummary } = require ( "../../../testUtils" ) ;
4
- test ( "single-config-watch-opt" , ( ) => {
5
- runWatch ( __dirname , [
3
+ jest . setTimeout ( 10E6 ) ;
4
+ /* eslint-disable node/no-unsupported-features */
5
+ /* eslint-disable node/no-unsupported-features/es-syntax */
6
+
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
+ } ) ;
34
+
35
+ test ( "single-config-watch-opt" , async done => {
36
+ const webpackProc = runAndGetWatchProc ( __dirname , [
6
37
"--entry" ,
7
38
"./index.js" ,
8
39
"--config" ,
@@ -14,18 +45,53 @@ test("single-config-watch-opt", () => {
14
45
"--target" ,
15
46
"async-node" ,
16
47
"--watch"
17
- ] ) . then ( result => {
18
- const { stdout, stderr } = result ;
48
+ ] ) ;
19
49
20
- const summary = extractSummary ( stdout ) ;
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 ;
21
58
22
- expect ( summary ) . toEqual ( expect . anything ( ) ) ;
23
- expect ( summary ) . toContain ( "" ) ;
24
- expect ( summary ) . toContain ( "webpack is watching the files…" ) ;
59
+ webpackProc . stdout . on ( "data" , data => {
60
+ data = data . toString ( ) ;
61
+ chunkNumber ++ ;
25
62
26
- expect ( stderr ) . toHaveLength ( 0 ) ;
63
+ switch ( chunkNumber ) {
64
+ case 1 :
65
+ expect ( data ) . toContain ( "webpack is watching the files" ) ;
66
+ break ;
67
+ case 2 :
68
+ expect ( extractSummary ( data ) ) . toMatchSnapshot ( ) ;
27
69
28
- expect ( summary ) . toMatchSnapshot ( ) ;
29
- return ;
70
+ hash1 = extractHash ( data ) ;
71
+
72
+ // We get webpack output after running test
73
+ // Since we are running the webpack in watch mode, changing file will generate additional output
74
+ // First time output will be validated fully
75
+ // Hash of the The subsequent output will be tested against that of first time output
76
+ appendDataIfFileExists ( __dirname , fileToChange , "//junk-comment" ) ;
77
+
78
+ break ;
79
+ case 3 :
80
+ hash2 = extractHash ( data ) ;
81
+
82
+ expect ( hash2 . hash ) . not . toBe ( hash1 . hash ) ;
83
+
84
+ webpackProc . kill ( ) ;
85
+ done ( ) ;
86
+ break ;
87
+ default :
88
+ break ;
89
+ }
90
+ } ) ;
91
+
92
+ webpackProc . stderr . on ( "data" , error => {
93
+ // fail test case if there is any error
94
+ done ( error . toString ( ) ) ;
30
95
} ) ;
31
96
} ) ;
97
+
0 commit comments