1+ import { promisify } from "util" ;
12import test from "ava" ;
3+ import rimRaf from "rimraf" ;
24import colors from "colors/safe" ;
35import filesize from "../src/index.js" ;
46
7+ const rimraf = promisify ( rimRaf ) ;
8+
59const x = filesize ( { } , "test" ) ;
610
711const bundle = {
812 fileName : "test/fixtures/bundled-file.js" ,
913 code : "function add(first, second) { return first + second; }" ,
1014} ;
1115
16+ async function removeCacheDir ( ) {
17+ return await rimraf ( ".cache" ) ;
18+ }
19+
1220async function getStdout ( cb ) {
1321 const originalStdoutWrite = process . stdout . write . bind ( process . stdout ) ;
1422
@@ -31,6 +39,10 @@ test("fileSize should return a string", async (t) => {
3139 t . is ( typeof ( await x ( { file : "abc.js" } , bundle ) ) , "string" ) ;
3240} ) ;
3341
42+ test ( "fileSize should return a string (with deprecated `dest`)" , async ( t ) => {
43+ t . is ( typeof ( await x ( { dest : "abc.js" } , bundle ) ) , "string" ) ;
44+ } ) ;
45+
3446test ( "fileSize should allow custom reporter" , async ( t ) => {
3547 const getLoggingData = filesize (
3648 {
@@ -44,7 +56,7 @@ test("fileSize should allow custom reporter", async (t) => {
4456 format : { } ,
4557 theme : "dark" ,
4658 // reporter: [AsyncFunction: customReporter],
47- showBeforeSizes : false ,
59+ showBeforeSizes : "none" ,
4860 showGzippedSize : true ,
4961 showBrotliSize : false ,
5062 showMinifiedSize : true ,
@@ -146,30 +158,80 @@ test("fileSize should generate a bundle", async (t) => {
146158} ) ;
147159
148160test ( "fileSize should apply `showBeforeSizes` option" , async ( t ) => {
149- const getLoggingData = filesize ( { showBeforeSizes : true } , "test" ) ;
150- const val = await getLoggingData (
151- { file : "./test/fixtures/sample.js" } ,
152- bundle
153- ) ;
161+ const getLoggingData = filesize ( { showBeforeSizes : "release" } , "test" ) ;
162+ const val = await getLoggingData ( { file : "./dist/index.js" } , bundle ) ;
154163 if ( colors . supportsColor ( ) ) {
155164 // eslint-disable-next-line no-control-regex
156- t . regex ( val , / \( w a s \u001b \[ 3 3 m 2 1 B / ) ;
165+ t . regex ( val , / \( w a s \u001b \[ 3 3 m [ \d . ] + K B / ) ;
157166 } else {
158- t . regex ( val , / \( w a s 2 1 B / ) ;
167+ t . regex ( val , / \( w a s [ \d . ] + K B / ) ;
168+ }
169+ } ) ;
170+
171+ test ( 'fileSize should apply `showBeforeSizes` option as "build"' , async ( t ) => {
172+ await removeCacheDir ( ) ;
173+
174+ const getLoggingData = filesize ( { showBeforeSizes : "build" } , "test" ) ;
175+ const val = await getLoggingData ( { file : "./dist/index.js" } , bundle ) ;
176+ if ( colors . supportsColor ( ) ) {
177+ // eslint-disable-next-line no-control-regex
178+ t . regex ( val , / \( w a s \u001b \[ 3 3 m [ \d . ] + K ? B / ) ;
179+ } else {
180+ t . regex ( val , / \( w a s [ \d . ] + K ? B / ) ;
181+ }
182+ } ) ;
183+
184+ test ( 'fileSize should ignore before sizes with package-missing file and `showBeforeSizes: "release"`' , async ( t ) => {
185+ await removeCacheDir ( ) ;
186+
187+ let getLoggingData = filesize ( { showBeforeSizes : "release" } , "test" ) ;
188+ let val = await getLoggingData ( { file : "./test/fixtures/sample.js" } , bundle ) ;
189+ if ( colors . supportsColor ( ) ) {
190+ // eslint-disable-next-line no-control-regex
191+ t . notRegex ( val , / \( w a s / ) ;
192+ } else {
193+ t . notRegex ( val , / \( w a s / ) ;
194+ }
195+
196+ // Run again (without deleting cache directory) to get coverage of cache
197+ getLoggingData = filesize ( { showBeforeSizes : "release" } , "test" ) ;
198+ val = await getLoggingData ( { file : "./test/fixtures/sample.js" } , bundle ) ;
199+ if ( colors . supportsColor ( ) ) {
200+ // eslint-disable-next-line no-control-regex
201+ t . notRegex ( val , / \( w a s / ) ;
202+ } else {
203+ t . notRegex ( val , / \( w a s / ) ;
204+ }
205+ } ) ;
206+
207+ test ( "fileSize should ignore before sizes with bad package" , async ( t ) => {
208+ await removeCacheDir ( ) ;
209+
210+ const oldCwd = process . cwd ( ) ;
211+ const getLoggingData = filesize ( { showBeforeSizes : "release" } , "test" ) ;
212+
213+ // Change directory without giving a chance for other tests to use
214+ process . chdir ( "./test/fixtures" ) ;
215+ const prom = getLoggingData ( { file : "./sample.js" } , bundle ) ;
216+ process . chdir ( oldCwd ) ;
217+ const val = await prom ;
218+
219+ if ( colors . supportsColor ( ) ) {
220+ // eslint-disable-next-line no-control-regex
221+ t . notRegex ( val , / \( w a s / ) ;
222+ } else {
223+ t . notRegex ( val , / \( w a s / ) ;
159224 }
160225} ) ;
161226
162227test ( "fileSize should apply `showBeforeSizes` option (with deprecated `dest`)" , async ( t ) => {
163- const getLoggingData = filesize ( { showBeforeSizes : true } , "test" ) ;
164- const val = await getLoggingData (
165- { dest : "./test/fixtures/sample.js" } ,
166- bundle
167- ) ;
228+ const getLoggingData = filesize ( { showBeforeSizes : "release" } , "test" ) ;
229+ const val = await getLoggingData ( { dest : "./dist/index.js" } , bundle ) ;
168230 if ( colors . supportsColor ( ) ) {
169231 // eslint-disable-next-line no-control-regex
170- t . regex ( val , / \( w a s \u001b \[ 3 3 m 2 1 B / ) ;
232+ t . regex ( val , / \( w a s \u001b \[ 3 3 m [ \d . ] + K B / ) ;
171233 } else {
172- t . regex ( val , / \( w a s 2 1 B / ) ;
234+ t . regex ( val , / \( w a s [ \d . ] + K B / ) ;
173235 }
174236} ) ;
175237
@@ -232,7 +294,7 @@ test("fileSize should show Brotli size when configured", async (t) => {
232294test ( "fileSize should show before Brotli size when configured" , async ( t ) => {
233295 let getLoggingData = filesize (
234296 {
235- showBeforeSizes : true ,
297+ showBeforeSizes : "build" ,
236298 showBrotliSize : true ,
237299 } ,
238300 "test"
@@ -251,7 +313,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
251313
252314 getLoggingData = filesize (
253315 {
254- showBeforeSizes : true ,
316+ showBeforeSizes : "build" ,
255317 showBrotliSize : true ,
256318 showMinifiedSize : false ,
257319 showGzippedSize : false ,
@@ -271,7 +333,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
271333
272334 getLoggingData = filesize (
273335 {
274- showBeforeSizes : true ,
336+ showBeforeSizes : "build" ,
275337 showBrotliSize : true ,
276338 showMinifiedSize : true ,
277339 showGzippedSize : false ,
@@ -292,7 +354,7 @@ test("fileSize should show before Brotli size when configured", async (t) => {
292354
293355 getLoggingData = filesize (
294356 {
295- showBeforeSizes : true ,
357+ showBeforeSizes : "build" ,
296358 showBrotliSize : true ,
297359 showMinifiedSize : false ,
298360 showGzippedSize : true ,
0 commit comments