@@ -3,70 +3,101 @@ const webpack = require("webpack");
33const { readFileSync } = require ( "fs" ) ;
44const webpackConfig = require ( "./webpack.config" ) ;
55
6- const genSmpWebpackConfig = smp =>
7- smp . wrap (
8- Object . assign ( { } , webpackConfig , {
9- output : {
10- path : webpackConfig . output . path + "/_smp_" + new Date ( ) . getTime ( ) ,
11- } ,
12- } )
6+ const getStandardConf = conf => {
7+ if ( typeof conf === "function" ) conf = conf ( ) ;
8+ const arr = Array . isArray ( conf ) ? conf : [ conf ] ;
9+
10+ return arr . map (
11+ subConf => ( typeof subConf === "function" ? subConf ( ) : subConf )
1312 ) ;
13+ } ;
14+
15+ let i = 0 ;
16+ const prepareSmpWebpackConfig = conf => {
17+ if ( Array . isArray ( conf ) ) return conf . map ( prepareSmpWebpackConfig ) ;
18+ if ( typeof conf === "function" )
19+ return ( ...args ) => prepareSmpWebpackConfig ( conf ( ...args ) ) ;
20+
21+ return Object . assign ( { } , conf , {
22+ output : {
23+ path : conf . output . path + "/_smp_" + i ++ + "_" + new Date ( ) . getTime ( ) ,
24+ } ,
25+ } ) ;
26+ } ;
27+
28+ const genSmpWebpackConfig = smp =>
29+ smp . wrap ( prepareSmpWebpackConfig ( webpackConfig ) ) ;
1430
1531const runWebpack = config =>
1632 new Promise ( ( resolve , reject ) => {
17- webpack ( config , ( err , stats ) => {
33+ const standardConf = getStandardConf ( config ) ;
34+ webpack ( standardConf , ( err , stats ) => {
1835 if ( err || stats . hasErrors ( ) ) return reject ( err || stats ) ;
19- resolve ( readFileSync ( config . output . path + "/bundle.js" ) . toString ( ) ) ;
36+ const fileContent = standardConf . map ( conf =>
37+ readFileSync ( conf . output . path + "/bundle.js" ) . toString ( )
38+ ) ;
39+ resolve ( fileContent . join ( "\n///////// new file /////////\n" ) ) ;
2040 } ) ;
2141 } ) ;
2242
2343jest . setTimeout ( 20000 ) ;
2444
25- describe ( "smp - " + __dirname . split ( "/" ) . pop ( ) , ( ) => {
26- let distApp ;
27- beforeAll ( ( ) => runWebpack ( webpackConfig ) . then ( file => ( distApp = file ) ) ) ;
45+ const testRef = { } ;
2846
29- describe ( "vanilla config" , ( ) => {
30- let smpOutput ;
31- let smpDistApp ;
47+ describe ( "smp" , ( ) => {
48+ beforeAll ( ( ) =>
49+ runWebpack ( webpackConfig ) . then ( file => ( testRef . distApp = file ) )
50+ ) ;
51+
52+ describe ( __dirname . split ( "/" ) . pop ( ) , ( ) => {
3253 const smp = new SpeedMeasurePlugin ( {
33- outputTarget : output => ( smpOutput = output ) ,
54+ outputTarget : output => ( testRef . smpOutput = output ) ,
3455 } ) ;
3556 const smpWebpackConfig = genSmpWebpackConfig ( smp ) ;
3657
3758 beforeAll ( ( ) =>
38- runWebpack ( smpWebpackConfig ) . then ( file => ( smpDistApp = file ) )
59+ runWebpack ( smpWebpackConfig ) . then ( file => ( testRef . smpDistApp = file ) )
3960 ) ;
4061
4162 it ( "should generate the same app.js content" , ( ) => {
42- expect ( smpDistApp ) . toEqual ( distApp ) ;
63+ expect ( testRef . smpDistApp ) . toEqual ( testRef . distApp ) ;
4364 } ) ;
4465
4566 it ( "should generate the same app.js content after 2 runs" , ( ) => {
4667 const dupSmpWebpackConfig = genSmpWebpackConfig ( smp ) ;
4768
4869 return runWebpack ( dupSmpWebpackConfig ) . then ( dupSmpDistApp => {
49- expect ( dupSmpDistApp ) . toEqual ( smpDistApp ) ;
50- expect ( dupSmpDistApp ) . toEqual ( distApp ) ;
70+ expect ( dupSmpDistApp ) . toEqual ( testRef . smpDistApp ) ;
71+ expect ( dupSmpDistApp ) . toEqual ( testRef . distApp ) ;
5172 } ) ;
5273 } ) ;
5374
5475 it ( "should state the time taken overall" , ( ) => {
55- expect ( smpOutput ) . toMatch (
76+ expect ( testRef . smpOutput ) . toMatch (
5677 / G e n e r a l o u t p u t t i m e t o o k .* ( [ 0 - 9 ] + m i n s ? ) ? [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? s e c s /
5778 ) ;
5879 } ) ;
5980
6081 it ( "should state the time taken by the plugins" , ( ) => {
61- expect ( smpOutput ) . toMatch (
82+ expect ( testRef . smpOutput ) . toMatch (
6283 / D e f i n e P l u g i n .* t o o k .* ( [ 0 - 9 ] + m i n s ? ) ? [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? s e c s /
6384 ) ;
6485 } ) ;
6586
6687 it ( "should state the time taken by the loaders" , ( ) => {
67- expect ( smpOutput ) . toMatch (
88+ expect ( testRef . smpOutput ) . toMatch (
6889 / b a b e l - l o a d e r .* t o o k .* ( [ 0 - 9 ] + m i n s ? ) ? [ 0 - 9 ] + ( \. [ 0 - 9 ] + ) ? s e c s .* \n \s + m o d u l e c o u n t \s + = [ 0 - 9 ] + /
6990 ) ;
7091 } ) ;
92+
93+ let customTests ;
94+ try {
95+ customTests = require ( "./customTests" ) ;
96+ } catch ( _ ) {
97+ // do nothing, we expect `require` to fail if no tests exist
98+ }
99+ if ( customTests ) {
100+ describe ( "custom tests" , ( ) => customTests ( testRef ) ) ;
101+ }
71102 } ) ;
72103} ) ;
0 commit comments