Last active
May 6, 2025 09:09
-
-
Save Schniz/9e7935752b31fe0eda7ce88f76d67461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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