@@ -14,171 +14,6 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
1414const port = portsMap . api ;
1515
1616describe ( "API" , ( ) => {
17- it ( "should work with plugin API" , async ( t ) => {
18- const compiler = webpack ( config ) ;
19- const server = new Server ( { port } ) ;
20-
21- server . apply ( compiler ) ;
22-
23- await compile ( compiler , port ) ;
24-
25- const { page, browser } = await runBrowser ( ) ;
26-
27- const pageErrors = [ ] ;
28- const consoleMessages = [ ] ;
29-
30- page
31- . on ( "console" , ( message ) => {
32- consoleMessages . push ( message ) ;
33- } )
34- . on ( "pageerror" , ( error ) => {
35- pageErrors . push ( error ) ;
36- } ) ;
37-
38- await page . goto ( `http://127.0.0.1:${ port } /` , {
39- waitUntil : "networkidle0" ,
40- } ) ;
41-
42- t . assert . snapshot ( consoleMessages . map ( ( message ) => message . text ( ) ) ) ;
43-
44- t . assert . snapshot ( pageErrors ) ;
45-
46- await browser . close ( ) ;
47- await new Promise ( ( resolve ) => {
48- compiler . close ( resolve ) ;
49- } ) ;
50- } ) ;
51-
52- it ( "should not start the server multiple times on recompilation" , async ( ) => {
53- const compiler = webpack ( config ) ;
54- const server = new Server ( { port } ) ;
55- const setupSpy = jest . spyOn ( server , "setup" ) ;
56- const listenSpy = jest . spyOn ( server , "listen" ) ;
57-
58- server . apply ( compiler ) ;
59-
60- const { watching } = await compile ( compiler , port ) ;
61-
62- // Trigger a recompilation by invalidating
63- await new Promise ( ( resolve ) => {
64- watching . invalidate ( ( ) => {
65- resolve ( ) ;
66- } ) ;
67- } ) ;
68-
69- // Wait for the recompilation to finish
70- await new Promise ( ( resolve ) => {
71- setTimeout ( resolve , 2000 ) ;
72- } ) ;
73-
74- expect ( setupSpy ) . toHaveBeenCalledTimes ( 1 ) ;
75- expect ( listenSpy ) . toHaveBeenCalledTimes ( 1 ) ;
76-
77- setupSpy . mockRestore ( ) ;
78- listenSpy . mockRestore ( ) ;
79- await new Promise ( ( resolve ) => {
80- compiler . close ( resolve ) ;
81- } ) ;
82- } ) ;
83-
84- it ( "should stop the server cleanly via compiler.close()" , async ( ) => {
85- const compiler = webpack ( config ) ;
86- const server = new Server ( { port } ) ;
87- const stopSpy = jest . spyOn ( server , "stop" ) ;
88-
89- server . apply ( compiler ) ;
90-
91- await compile ( compiler , port ) ;
92-
93- await new Promise ( ( resolve ) => {
94- compiler . close ( resolve ) ;
95- } ) ;
96-
97- expect ( stopSpy ) . toHaveBeenCalledTimes ( 1 ) ;
98- stopSpy . mockRestore ( ) ;
99- } ) ;
100-
101- describe ( "plugin in webpack config" , ( ) => {
102- it ( "should work when added to webpack config plugins array" , async ( t ) => {
103- const server = new Server ( { port } ) ;
104- const compiler = webpack ( {
105- ...config ,
106- plugins : [ ...config . plugins , server ] ,
107- } ) ;
108-
109- await compile ( compiler , port ) ;
110-
111- const { page, browser } = await runBrowser ( ) ;
112-
113- try {
114- const pageErrors = [ ] ;
115- const consoleMessages = [ ] ;
116-
117- page
118- . on ( "console" , ( message ) => {
119- consoleMessages . push ( message ) ;
120- } )
121- . on ( "pageerror" , ( error ) => {
122- pageErrors . push ( error ) ;
123- } ) ;
124-
125- await page . goto ( `http://127.0.0.1:${ port } /` , {
126- waitUntil : "networkidle0" ,
127- } ) ;
128-
129- t . assert . snapshot ( consoleMessages . map ( ( message ) => message . text ( ) ) ) ;
130- t . assert . snapshot ( pageErrors ) ;
131- } finally {
132- await browser . close ( ) ;
133- await new Promise ( ( resolve ) => {
134- compiler . close ( resolve ) ;
135- } ) ;
136- }
137- } ) ;
138-
139- it ( "should work with output.clean: true" , async ( t ) => {
140- const server = new Server ( { port } ) ;
141- const compiler = webpack ( {
142- ...config ,
143- output : {
144- ...config . output ,
145- clean : true ,
146- } ,
147- plugins : [ ...config . plugins , server ] ,
148- } ) ;
149-
150- await compile ( compiler , port ) ;
151-
152- const { page, browser } = await runBrowser ( ) ;
153-
154- try {
155- const pageErrors = [ ] ;
156- const consoleMessages = [ ] ;
157-
158- page
159- . on ( "console" , ( message ) => {
160- consoleMessages . push ( message ) ;
161- } )
162- . on ( "pageerror" , ( error ) => {
163- pageErrors . push ( error ) ;
164- } ) ;
165-
166- const response = await page . goto ( `http://127.0.0.1:${ port } /` , {
167- waitUntil : "networkidle0" ,
168- } ) ;
169-
170- expect ( response . status ( ) ) . toBe ( 200 ) ;
171- t . assert . snapshot ( consoleMessages . map ( ( message ) => message . text ( ) ) ) ;
172- t . assert . snapshot ( pageErrors ) ;
173- } finally {
174- await browser . close ( ) ;
175- await new Promise ( ( resolve ) => {
176- compiler . close ( resolve ) ;
177- } ) ;
178- }
179- } ) ;
180- } ) ;
181-
18217 describe ( "WEBPACK_SERVE environment variable" , ( ) => {
18318 const OLD_ENV = process . env ;
18419 let server ;
0 commit comments