@@ -8,6 +8,8 @@ const getGetPort = () => import("get-port");
88
99const entryPath = resolve ( __dirname , "./src/index.js" ) ;
1010const originalEntry = readFileSync ( entryPath , "utf8" ) ;
11+ const workerPath = resolve ( __dirname , "./src/worker.js" ) ;
12+ const originalWorker = readFileSync ( workerPath , "utf8" ) ;
1113
1214describe ( "serve recompilation" , ( ) => {
1315 let port ;
@@ -18,6 +20,7 @@ describe("serve recompilation", () => {
1820
1921 afterEach ( ( ) => {
2022 writeFileSync ( entryPath , originalEntry ) ;
23+ writeFileSync ( workerPath , originalWorker ) ;
2124 } ) ;
2225
2326 it ( "should recompile upon file change and log the stats again" , async ( ) => {
@@ -84,18 +87,68 @@ describe("serve recompilation", () => {
8487 expect ( updatedBody ) . toContain ( "serve rebuild test updated" ) ;
8588 } ) ;
8689
87- it ( "should watch every compiler of a multi compiler with its own watch options" , async ( ) => {
88- const { stderr, stdout } = await runWatch (
89- __dirname ,
90- [ "serve" , "--config" , "multi.config.js" , "--port" , port ] ,
91- {
92- stdoutKillStr : / c o m p i l e d s u c c e s s f u l l y / ,
93- stderrKillStr : / P r o j e c t i s r u n n i n g a t : / ,
94- } ,
95- ) ;
96-
97- expect ( stdout ) . toContain ( "app:" ) ;
98- expect ( stdout ) . toContain ( "worker:" ) ;
99- expect ( stderr ) . toContain ( "Project is running at:" ) ;
100- } ) ;
90+ it . each ( [ "app" , "worker" ] ) (
91+ "should rebuild and serve changes to the %s compiler" ,
92+ async ( name ) => {
93+ let updatedBody ;
94+ let requestError ;
95+ const { stdout } = await runWatch (
96+ __dirname ,
97+ [ "serve" , "--config" , "multi.config.js" , "--port" , port ] ,
98+ {
99+ handler : ( proc ) => {
100+ let output = "" ;
101+ let serverOutput = "" ;
102+ let changed = false ;
103+ let fetching = false ;
104+ const check = ( ) => {
105+ if (
106+ ! changed &&
107+ output . includes ( "Built app" ) &&
108+ output . includes ( "Built worker" ) &&
109+ serverOutput . includes ( "Project is running at:" )
110+ ) {
111+ changed = true ;
112+ writeFileSync (
113+ name === "app" ? entryPath : workerPath ,
114+ `console.log('updated ${ name } bundle');\n` ,
115+ ) ;
116+ }
117+
118+ if ( ! fetching && output . split ( `Built ${ name } ` ) . length === 3 ) {
119+ fetching = true ;
120+ fetch ( `http://127.0.0.1:${ port } /${ name } .js` )
121+ . then ( ( response ) => response . text ( ) )
122+ . then ( ( body ) => {
123+ updatedBody = body ;
124+ } )
125+ . catch ( ( error ) => {
126+ requestError = error ;
127+ } )
128+ . finally ( ( ) => {
129+ processKill ( proc ) ;
130+ } ) ;
131+ }
132+ } ;
133+
134+ proc . stdout . on ( "data" , ( chunk ) => {
135+ output += chunk . toString ( ) ;
136+ check ( ) ;
137+ } ) ;
138+ proc . stderr . on ( "data" , ( chunk ) => {
139+ serverOutput += chunk . toString ( ) ;
140+ check ( ) ;
141+ } ) ;
142+ } ,
143+ } ,
144+ ) ;
145+
146+ expect ( requestError ) . toBeUndefined ( ) ;
147+ expect ( updatedBody ) . toContain ( `updated ${ name } bundle` ) ;
148+ expect ( stdout ) . toContain ( "Watch app: 10" ) ;
149+ expect ( stdout ) . toContain ( "Watch worker: 30" ) ;
150+ expect ( stdout . match ( / C l o s e d a p p / g) ) . toHaveLength ( 1 ) ;
151+ expect ( stdout . match ( / C l o s e d w o r k e r / g) ) . toHaveLength ( 1 ) ;
152+ } ,
153+ ) ;
101154} ) ;
0 commit comments