Skip to content

Instantly share code, notes, and snippets.

@Schniz
Last active May 6, 2025 09:09
Show Gist options
  • Save Schniz/9e7935752b31fe0eda7ce88f76d67461 to your computer and use it in GitHub Desktop.
Save Schniz/9e7935752b31fe0eda7ce88f76d67461 to your computer and use it in GitHub Desktop.
import http from 'node:http';
import { onTestFinished } from 'vitest';
export function createHttpServer(handler: http.RequestListener) {
const server = http.createServer(handler);
const address = server.listen(0).address();
onTestFinished(() => {
server.close();
});
if (!address || typeof address !== 'object') {
throw new Error('address is not an object???');
}
return { url: `http://localhost:${address.port}` };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment