Skip to content

Commit cec9ac3

Browse files
committed
Fix flaky WordPress server startup test
1 parent a858486 commit cec9ac3

1 file changed

Lines changed: 36 additions & 49 deletions

File tree

apps/cli/lib/tests/wordpress-server-manager.test.ts

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ describe( 'WordPress Server Manager', () => {
9797
} );
9898
}
9999

100+
function mockStartProcessExit( {
101+
beforeExit,
102+
stderrTail,
103+
}: {
104+
beforeExit?: () => void;
105+
stderrTail?: string;
106+
} = {} ): void {
107+
vi.mocked( daemonClient.startProcess ).mockImplementation( async () => {
108+
setImmediate( () => {
109+
beforeExit?.();
110+
mockBus.emit( 'process-event', {
111+
process: {
112+
name: mockProcessDescription.name,
113+
pm_id: mockProcessDescription.pmId,
114+
},
115+
event: 'exit',
116+
...( stderrTail ? { stderrTail } : {} ),
117+
} );
118+
} );
119+
return mockProcessDescription;
120+
} );
121+
}
122+
100123
describe( 'isServerRunning', () => {
101124
it( 'should check if process is running with correct process name', async () => {
102125
const mockProcess = {
@@ -264,15 +287,7 @@ describe( 'WordPress Server Manager', () => {
264287

265288
it( 'should surface an error when the child process exits before becoming ready', async () => {
266289
// Do not emit `ready`; instead emit an `exit` event to simulate a crash during startup.
267-
setTimeout( () => {
268-
mockBus.emit( 'process-event', {
269-
process: {
270-
name: mockProcessDescription.name,
271-
pm_id: mockProcessDescription.pmId,
272-
},
273-
event: 'exit',
274-
} );
275-
}, 10 );
290+
mockStartProcessExit();
276291

277292
await expect( startWordPressServer( mockSiteData, mockLogger ) ).rejects.toThrow(
278293
/exited before becoming ready/
@@ -281,17 +296,7 @@ describe( 'WordPress Server Manager', () => {
281296

282297
it( 'should include the child stderr tail in the error when the daemon provides it', async () => {
283298
const stderrTail = 'SyntaxError: The requested module did not provide an export named X';
284-
285-
setTimeout( () => {
286-
mockBus.emit( 'process-event', {
287-
process: {
288-
name: mockProcessDescription.name,
289-
pm_id: mockProcessDescription.pmId,
290-
},
291-
event: 'exit',
292-
stderrTail,
293-
} );
294-
}, 10 );
299+
mockStartProcessExit( { stderrTail } );
295300

296301
await expect( startWordPressServer( mockSiteData, mockLogger ) ).rejects.toThrow(
297302
new RegExp( stderrTail.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) )
@@ -327,16 +332,11 @@ describe( 'WordPress Server Manager', () => {
327332
await fs.promises.mkdir( path.join( sitePath, 'wp-content' ), { recursive: true } );
328333
const logPath = path.join( sitePath, 'wp-content', STUDIO_ERROR_LOG_FILENAME );
329334

330-
setTimeout( () => {
331-
fs.writeFileSync( logPath, 'PHP Fatal error: Uncaught Error: Failed opening required' );
332-
mockBus.emit( 'process-event', {
333-
process: {
334-
name: mockProcessDescription.name,
335-
pm_id: mockProcessDescription.pmId,
336-
},
337-
event: 'exit',
338-
} );
339-
}, 10 );
335+
mockStartProcessExit( {
336+
beforeExit: () => {
337+
fs.writeFileSync( logPath, 'PHP Fatal error: Uncaught Error: Failed opening required' );
338+
},
339+
} );
340340

341341
await expect(
342342
startWordPressServer( { ...mockSiteData, path: sitePath }, mockLogger )
@@ -350,15 +350,7 @@ describe( 'WordPress Server Manager', () => {
350350
const logPath = path.join( sitePath, 'wp-content', 'debug.log' );
351351
fs.writeFileSync( logPath, 'PHP Fatal error: from a previous run' );
352352

353-
setTimeout( () => {
354-
mockBus.emit( 'process-event', {
355-
process: {
356-
name: mockProcessDescription.name,
357-
pm_id: mockProcessDescription.pmId,
358-
},
359-
event: 'exit',
360-
} );
361-
}, 10 );
353+
mockStartProcessExit();
362354

363355
const error: Error = await startWordPressServer(
364356
{ ...mockSiteData, path: sitePath, enableDebugLog: true },
@@ -378,16 +370,11 @@ describe( 'WordPress Server Manager', () => {
378370
await fs.promises.mkdir( path.join( sitePath, 'wp-content' ), { recursive: true } );
379371
const logPath = path.join( sitePath, 'wp-content', 'debug.log' );
380372

381-
setTimeout( () => {
382-
fs.writeFileSync( logPath, 'PHP Fatal error: Uncaught Error: Failed opening required' );
383-
mockBus.emit( 'process-event', {
384-
process: {
385-
name: mockProcessDescription.name,
386-
pm_id: mockProcessDescription.pmId,
387-
},
388-
event: 'exit',
389-
} );
390-
}, 10 );
373+
mockStartProcessExit( {
374+
beforeExit: () => {
375+
fs.writeFileSync( logPath, 'PHP Fatal error: Uncaught Error: Failed opening required' );
376+
},
377+
} );
391378

392379
await expect(
393380
startWordPressServer(

0 commit comments

Comments
 (0)