Skip to content

Commit 30f03e5

Browse files
committed
test: fix test to stop server when test fails
1 parent 795f600 commit 30f03e5

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/theme-wizard-server/src/server.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
22
import { join, dirname } from 'node:path';
33
import { fileURLToPath } from 'node:url';
4-
import { it, expect } from 'vitest';
4+
import { it, expect, afterEach } from 'vitest';
55

66
const __dirname = dirname(fileURLToPath(import.meta.url));
77
const packageDir = join(__dirname, '..');
@@ -64,8 +64,11 @@ const stopServer = async (testProcess: ChildProcessWithoutNullStreams): Promise<
6464
});
6565
};
6666

67+
let runningProcess: ChildProcessWithoutNullStreams | null = null;
68+
6769
it('should execute as a standalone file and start the server on custom port', async () => {
6870
const { output, process: testProcess, started } = await startServer(9999);
71+
runningProcess = testProcess;
6972

7073
expect(started).toBe(true);
7174
expect(output).toContain('Starting Theme Wizard server...');
@@ -76,10 +79,17 @@ it('should execute as a standalone file and start the server on custom port', as
7679

7780
it('should execute with default port 8080 when PORT env is not set', async () => {
7881
const { output, process: testProcess, started } = await startServer();
82+
runningProcess = testProcess;
7983

8084
expect(started).toBe(true);
8185
expect(output).toContain('Starting Theme Wizard server...');
8286
expect(output).toContain('http://[::]:8080/');
8387

8488
await stopServer(testProcess);
8589
});
90+
91+
afterEach(() => {
92+
if (runningProcess) {
93+
stopServer(runningProcess);
94+
}
95+
});

0 commit comments

Comments
 (0)