@@ -3,20 +3,20 @@ const { run } = require('../utils/test-utils');
3
3
const { stat, readFile } = require ( 'fs' ) ;
4
4
const { resolve } = require ( 'path' ) ;
5
5
6
+ const successMessage = 'stats are successfully stored as json to stats.json' ;
7
+
6
8
describe ( 'json flag' , ( ) => {
7
9
it ( 'should return valid json' , ( ) => {
8
10
const { stdout, exitCode } = run ( __dirname , [ '--json' ] ) ;
9
-
10
11
expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
11
12
expect ( exitCode ) . toBe ( 0 ) ;
12
-
13
13
expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
14
14
} ) ;
15
15
16
16
it ( 'should store json to a file' , ( done ) => {
17
17
const { stdout, exitCode } = run ( __dirname , [ '--json' , 'stats.json' ] ) ;
18
18
19
- expect ( stdout ) . toContain ( 'stats are successfully stored as json to stats.json' ) ;
19
+ expect ( stdout ) . toContain ( successMessage ) ;
20
20
expect ( exitCode ) . toBe ( 0 ) ;
21
21
22
22
stat ( resolve ( __dirname , './stats.json' ) , ( err , stats ) => {
@@ -34,11 +34,52 @@ describe('json flag', () => {
34
34
} ) ;
35
35
} ) ;
36
36
37
+ it ( 'should store json to a file and respect --color flag' , ( done ) => {
38
+ const { stdout, exitCode } = run ( __dirname , [ '--json' , 'stats.json' , '--color' ] ) ;
39
+
40
+ expect ( stdout ) . toContain ( `[webpack-cli] \u001b[32m${ successMessage } ` ) ;
41
+ expect ( exitCode ) . toBe ( 0 ) ;
42
+
43
+ stat ( resolve ( __dirname , './stats.json' ) , ( err , stats ) => {
44
+ expect ( err ) . toBe ( null ) ;
45
+ expect ( stats . isFile ( ) ) . toBe ( true ) ;
46
+
47
+ readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
48
+ expect ( err ) . toBe ( null ) ;
49
+ expect ( JSON . parse ( data ) [ 'hash' ] ) . toBeTruthy ( ) ;
50
+ expect ( JSON . parse ( data ) [ 'version' ] ) . toBeTruthy ( ) ;
51
+ expect ( JSON . parse ( data ) [ 'time' ] ) . toBeTruthy ( ) ;
52
+ expect ( ( ) => JSON . parse ( data ) ) . not . toThrow ( ) ;
53
+ done ( ) ;
54
+ } ) ;
55
+ } ) ;
56
+ } ) ;
57
+
58
+ it ( 'should store json to a file and respect --no-color' , ( done ) => {
59
+ const { stdout, exitCode } = run ( __dirname , [ '--json' , 'stats.json' , '--no-color' ] ) ;
60
+
61
+ expect ( stdout ) . not . toContain ( `[webpack-cli] \u001b[32m${ successMessage } ` ) ;
62
+ expect ( stdout ) . toContain ( `[webpack-cli] ${ successMessage } ` ) ;
63
+ expect ( exitCode ) . toBe ( 0 ) ;
64
+
65
+ stat ( resolve ( __dirname , './stats.json' ) , ( err , stats ) => {
66
+ expect ( err ) . toBe ( null ) ;
67
+ expect ( stats . isFile ( ) ) . toBe ( true ) ;
68
+ readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
69
+ expect ( err ) . toBe ( null ) ;
70
+ expect ( JSON . parse ( data ) [ 'hash' ] ) . toBeTruthy ( ) ;
71
+ expect ( JSON . parse ( data ) [ 'version' ] ) . toBeTruthy ( ) ;
72
+ expect ( JSON . parse ( data ) [ 'time' ] ) . toBeTruthy ( ) ;
73
+ expect ( ( ) => JSON . parse ( data ) ) . not . toThrow ( ) ;
74
+ done ( ) ;
75
+ } ) ;
76
+ } ) ;
77
+ } ) ;
78
+
37
79
it ( 'should return valid json with -j alias' , ( ) => {
38
80
const { stdout, exitCode } = run ( __dirname , [ '-j' ] ) ;
39
81
expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
40
82
expect ( exitCode ) . toBe ( 0 ) ;
41
-
42
83
expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
43
84
} ) ;
44
85
} ) ;
0 commit comments