@@ -5,20 +5,24 @@ const { resolve } = require('path');
55
66const successMessage = 'stats are successfully stored as json to stats.json' ;
77
8- describe ( 'json flag ' , ( ) => {
9- it ( 'should return valid json' , ( ) => {
10- const { stdout , exitCode } = run ( __dirname , [ '--json' ] ) ;
11- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
8+ describe ( 'json' , ( ) => {
9+ it ( 'should work and output json stats ' , ( ) => {
10+ const { exitCode , stderr , stdout } = run ( __dirname , [ '--json' ] ) ;
11+
1212 expect ( exitCode ) . toBe ( 0 ) ;
13+ expect ( stderr ) . toBeFalsy ( ) ;
14+ expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
1315 expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
1416 } ) ;
1517
16- it ( 'should store json to a file' , ( done ) => {
17- const { stdout , exitCode } = run ( __dirname , [ '--json' , 'stats.json' ] ) ;
18+ it ( 'should work and store json to a file' , ( done ) => {
19+ const { exitCode , stderr , stdout } = run ( __dirname , [ '--json' , 'stats.json' ] ) ;
1820
19- expect ( stdout ) . toContain ( successMessage ) ;
2021 expect ( exitCode ) . toBe ( 0 ) ;
22+ expect ( stderr ) . toContain ( successMessage ) ;
23+ expect ( stdout ) . toBeFalsy ( ) ;
2124 expect ( existsSync ( resolve ( __dirname , './stats.json' ) ) ) . toBeTruthy ( ) ;
25+
2226 readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
2327 expect ( err ) . toBe ( null ) ;
2428 expect ( JSON . parse ( data ) [ 'hash' ] ) . toBeTruthy ( ) ;
@@ -29,12 +33,31 @@ describe('json flag', () => {
2933 } ) ;
3034 } ) ;
3135
32- it ( 'should store json to a file and respect --color flag' , ( done ) => {
33- const { stdout , exitCode } = run ( __dirname , [ '--json' , 'stats.json' , '--color' ] ) ;
36+ it ( 'should work and store json to a file and respect --color flag' , ( done ) => {
37+ const { exitCode , stderr , stdout } = run ( __dirname , [ '--json' , 'stats.json' , '--color' ] ) ;
3438
35- expect ( stdout ) . toContain ( `\u001b[32m${ successMessage } ` ) ;
3639 expect ( exitCode ) . toBe ( 0 ) ;
40+ expect ( stderr ) . toContain ( `\u001b[32m${ successMessage } ` ) ;
41+ expect ( stdout ) . toBeFalsy ( ) ;
42+ expect ( existsSync ( resolve ( __dirname , './stats.json' ) ) ) . toBeTruthy ( ) ;
3743
44+ readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
45+ expect ( err ) . toBe ( null ) ;
46+ expect ( JSON . parse ( data ) [ 'hash' ] ) . toBeTruthy ( ) ;
47+ expect ( JSON . parse ( data ) [ 'version' ] ) . toBeTruthy ( ) ;
48+ expect ( JSON . parse ( data ) [ 'time' ] ) . toBeTruthy ( ) ;
49+ expect ( ( ) => JSON . parse ( data ) ) . not . toThrow ( ) ;
50+ done ( ) ;
51+ } ) ;
52+ } ) ;
53+
54+ it ( 'should work and store json to a file and respect --no-color' , ( done ) => {
55+ const { exitCode, stderr, stdout } = run ( __dirname , [ '--json' , 'stats.json' , '--no-color' ] ) ;
56+
57+ expect ( exitCode ) . toBe ( 0 ) ;
58+ expect ( stderr ) . not . toContain ( `\u001b[32m${ successMessage } ` ) ;
59+ expect ( stderr ) . toContain ( `${ successMessage } ` ) ;
60+ expect ( stdout ) . toBeFalsy ( ) ;
3861 expect ( existsSync ( resolve ( __dirname , './stats.json' ) ) ) . toBeTruthy ( ) ;
3962
4063 readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
@@ -47,13 +70,31 @@ describe('json flag', () => {
4770 } ) ;
4871 } ) ;
4972
50- it ( 'should store json to a file and respect --no-color ' , ( done ) => {
51- const { stdout , exitCode } = run ( __dirname , [ '--json' , 'stats.json' , '--no-color '] ) ;
73+ it ( 'should work using the "-j" option (alias) ' , ( ) => {
74+ const { exitCode , stderr , stdout } = run ( __dirname , [ '-j ' ] ) ;
5275
53- expect ( stdout ) . not . toContain ( `\u001b[32m${ successMessage } ` ) ;
54- expect ( stdout ) . toContain ( `${ successMessage } ` ) ;
5576 expect ( exitCode ) . toBe ( 0 ) ;
77+ expect ( stderr ) . toBeFalsy ( ) ;
78+ expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
79+ expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
80+ } ) ;
81+
82+ it ( 'should work and output json stats with the "--progress" option' , ( ) => {
83+ const { exitCode, stderr, stdout } = run ( __dirname , [ '--json' , '--progress' ] ) ;
5684
85+ expect ( exitCode ) . toBe ( 0 ) ;
86+ expect ( stderr ) . toContain ( 'webpack.Progress' ) ;
87+ expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
88+ expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
89+ } ) ;
90+
91+ it ( 'should work and store json to a file with the "--progress" option' , ( done ) => {
92+ const { exitCode, stderr, stdout } = run ( __dirname , [ '--json' , 'stats.json' , '--progress' ] ) ;
93+
94+ expect ( exitCode ) . toBe ( 0 ) ;
95+ expect ( stderr ) . toContain ( 'webpack.Progress' ) ;
96+ expect ( stderr ) . toContain ( successMessage ) ;
97+ expect ( stdout ) . toBeFalsy ( ) ;
5798 expect ( existsSync ( resolve ( __dirname , './stats.json' ) ) ) . toBeTruthy ( ) ;
5899
59100 readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
@@ -66,10 +107,33 @@ describe('json flag', () => {
66107 } ) ;
67108 } ) ;
68109
69- it ( 'should return valid json with -j alias ' , ( ) => {
70- const { stdout , exitCode } = run ( __dirname , [ '-j ' ] ) ;
71- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
110+ it ( 'should work and output json stats with cli logs ' , ( ) => {
111+ const { exitCode , stderr , stdout } = run ( __dirname , [ '--json' , '--config' , 'logging.config.js '] ) ;
112+
72113 expect ( exitCode ) . toBe ( 0 ) ;
114+ expect ( stderr ) . toContain ( 'Compilation starting' ) ;
115+ expect ( stderr ) . toContain ( 'Compilation finished' ) ;
116+ expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrow ( ) ;
73117 expect ( JSON . parse ( stdout ) [ 'hash' ] ) . toBeDefined ( ) ;
74118 } ) ;
119+
120+ it ( 'should work and store json to a file with cli logs' , ( done ) => {
121+ const { exitCode, stderr, stdout } = run ( __dirname , [ '--json' , 'stats.json' , '--config' , 'logging.config.js' ] ) ;
122+
123+ expect ( exitCode ) . toBe ( 0 ) ;
124+ expect ( stderr ) . toContain ( 'Compilation starting' ) ;
125+ expect ( stderr ) . toContain ( 'Compilation finished' ) ;
126+ expect ( stderr ) . toContain ( successMessage ) ;
127+ expect ( stdout ) . toBeFalsy ( ) ;
128+ expect ( existsSync ( resolve ( __dirname , './stats.json' ) ) ) . toBeTruthy ( ) ;
129+
130+ readFile ( resolve ( __dirname , 'stats.json' ) , 'utf-8' , ( err , data ) => {
131+ expect ( err ) . toBe ( null ) ;
132+ expect ( JSON . parse ( data ) [ 'hash' ] ) . toBeTruthy ( ) ;
133+ expect ( JSON . parse ( data ) [ 'version' ] ) . toBeTruthy ( ) ;
134+ expect ( JSON . parse ( data ) [ 'time' ] ) . toBeTruthy ( ) ;
135+ expect ( ( ) => JSON . parse ( data ) ) . not . toThrow ( ) ;
136+ done ( ) ;
137+ } ) ;
138+ } ) ;
75139} ) ;
0 commit comments