1+ const {
2+ beforeAll,
3+ describe,
4+ expect,
5+ it,
6+ setDefaultTimeout,
7+ } = require ( "bun:test" ) ;
18const SpeedMeasurePlugin = require ( "../../.." ) ;
29const webpack = require ( "webpack" ) ;
310const { readFileSync } = require ( "fs" ) ;
411const webpackConfig = require ( "./webpack.config" ) ;
512
6- const getStandardConf = conf => {
13+ const getStandardConf = ( conf ) => {
714 if ( typeof conf === "function" ) conf = conf ( ) ;
815 const arr = Array . isArray ( conf ) ? conf : [ conf ] ;
916
10- return arr . map (
11- subConf => ( typeof subConf === "function" ? subConf ( ) : subConf )
17+ return arr . map ( ( subConf ) =>
18+ typeof subConf === "function" ? subConf ( ) : subConf
1219 ) ;
1320} ;
1421
1522let i = 0 ;
16- const prepareSmpWebpackConfig = conf => {
23+ const prepareSmpWebpackConfig = ( conf ) => {
1724 if ( Array . isArray ( conf ) ) return conf . map ( prepareSmpWebpackConfig ) ;
1825 if ( typeof conf === "function" )
1926 return ( ...args ) => prepareSmpWebpackConfig ( conf ( ...args ) ) ;
@@ -25,38 +32,89 @@ const prepareSmpWebpackConfig = conf => {
2532 } ) ;
2633} ;
2734
28- const genSmpWebpackConfig = smp =>
35+ const genSmpWebpackConfig = ( smp ) =>
2936 smp . wrap ( prepareSmpWebpackConfig ( webpackConfig ) ) ;
3037
31- const runWebpack = config =>
38+ const withCapturedOutput = ( fn ) => {
39+ const output = [ ] ;
40+ const writes = {
41+ stdout : process . stdout . write ,
42+ stderr : process . stderr . write ,
43+ } ;
44+ const emitWarning = process . emitWarning ;
45+ const capture = ( chunk , encoding , callback ) => {
46+ output . push ( Buffer . isBuffer ( chunk ) ? chunk . toString ( ) : chunk ) ;
47+ if ( typeof encoding === "function" ) encoding ( ) ;
48+ if ( typeof callback === "function" ) callback ( ) ;
49+ return true ;
50+ } ;
51+
52+ process . stdout . write = capture ;
53+ process . stderr . write = capture ;
54+ process . emitWarning = ( ...args ) => {
55+ const [ warning ] = args ;
56+ output . push (
57+ warning instanceof Error
58+ ? `${ warning . name } : ${ warning . message } \n`
59+ : `${ warning } \n`
60+ ) ;
61+ } ;
62+
63+ const restore = ( ) => {
64+ process . stdout . write = writes . stdout ;
65+ process . stderr . write = writes . stderr ;
66+ process . emitWarning = emitWarning ;
67+ } ;
68+ const flush = ( ) => {
69+ restore ( ) ;
70+ const replay = output . join ( "" ) ;
71+ if ( replay ) {
72+ writes . stderr . call ( process . stderr , replay ) ;
73+ }
74+ } ;
75+
76+ return fn ( {
77+ restore,
78+ flush,
79+ } ) ;
80+ } ;
81+
82+ const runWebpack = ( config ) =>
3283 new Promise ( ( resolve , reject ) => {
3384 const standardConf = getStandardConf ( config ) ;
34- webpack ( standardConf , ( err , stats ) => {
35- if ( err || stats . hasErrors ( ) ) return reject ( err || stats ) ;
36- const fileContent = standardConf . map ( conf =>
37- readFileSync ( conf . output . path + "/bundle.js" ) . toString ( )
38- ) ;
39- resolve ( fileContent . join ( "\n///////// new file /////////\n" ) ) ;
85+ withCapturedOutput ( ( { restore, flush } ) => {
86+ webpack ( standardConf , ( err , stats ) => {
87+ if ( err || stats . hasErrors ( ) ) {
88+ flush ( ) ;
89+ return reject ( err || stats ) ;
90+ }
91+
92+ restore ( ) ;
93+ const fileContent = standardConf . map ( ( conf ) =>
94+ readFileSync ( conf . output . path + "/bundle.js" ) . toString ( )
95+ ) ;
96+ resolve ( fileContent . join ( "\n///////// new file /////////\n" ) ) ;
97+ } ) ;
4098 } ) ;
4199 } ) ;
42100
43- jest . setTimeout ( 20000 ) ;
101+ setDefaultTimeout ( 20000 ) ;
44102
45103const testRef = { } ;
46104
47105describe ( "smp" , ( ) => {
48106 beforeAll ( ( ) =>
49- runWebpack ( webpackConfig ) . then ( file => ( testRef . distApp = file ) )
107+ runWebpack ( webpackConfig ) . then ( ( file ) => ( testRef . distApp = file ) )
50108 ) ;
51109
52110 describe ( __dirname . split ( "/" ) . pop ( ) , ( ) => {
53111 const smp = new SpeedMeasurePlugin ( {
54- outputTarget : output => ( testRef . smpOutput = output ) ,
112+ outputTarget : ( output ) => ( testRef . smpOutput = output ) ,
55113 } ) ;
56114 const smpWebpackConfig = genSmpWebpackConfig ( smp ) ;
57115
58116 beforeAll ( ( ) =>
59- runWebpack ( smpWebpackConfig ) . then ( file => ( testRef . smpDistApp = file ) )
117+ runWebpack ( smpWebpackConfig ) . then ( ( file ) => ( testRef . smpDistApp = file ) )
60118 ) ;
61119
62120 it ( "should generate the same app.js content" , ( ) => {
@@ -66,7 +124,7 @@ describe("smp", () => {
66124 it ( "should generate the same app.js content after 2 runs" , ( ) => {
67125 const dupSmpWebpackConfig = genSmpWebpackConfig ( smp ) ;
68126
69- return runWebpack ( dupSmpWebpackConfig ) . then ( dupSmpDistApp => {
127+ return runWebpack ( dupSmpWebpackConfig ) . then ( ( dupSmpDistApp ) => {
70128 expect ( dupSmpDistApp ) . toEqual ( testRef . smpDistApp ) ;
71129 expect ( dupSmpDistApp ) . toEqual ( testRef . distApp ) ;
72130 } ) ;
0 commit comments