mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 19:08:33 +01:00
Stabilize Vitest route suite isolation
This commit is contained in:
1 parent
8f1cd0474f
commit
9fa2ae1e81
49 files changed
+1417
-1498
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { createRequire } from "node:module";
|
||||
import type { AddressInfo, Server as NetServer } from "node:net";
|
||||
import { Server as TlsServer } from "node:tls";
|
||||
|
||||
type SupertestServer = NetServer & {
|
||||
address(): ReturnType<NetServer["address"]>;
|
||||
listen(port: number): NetServer;
|
||||
};
|
||||
|
||||
type SupertestTestInstance = {
|
||||
_server?: SupertestServer;
|
||||
};
|
||||
|
||||
type SupertestTestConstructor = {
|
||||
prototype: {
|
||||
serverAddress(this: SupertestTestInstance, app: SupertestServer, path: string): string;
|
||||
__paperclipLoopbackPatched?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const SupertestTest = require("supertest/lib/test.js") as SupertestTestConstructor;
|
||||
|
||||
if (!SupertestTest.prototype.__paperclipLoopbackPatched) {
|
||||
SupertestTest.prototype.serverAddress = function serverAddress(app, path) {
|
||||
const addr = app.address();
|
||||
|
||||
if (!addr) {
|
||||
this._server = app.listen(0) as SupertestServer;
|
||||
}
|
||||
|
||||
const listeningAddress = app.address() as AddressInfo | string | null;
|
||||
if (!listeningAddress || typeof listeningAddress === "string") {
|
||||
throw new Error("Expected Supertest server to listen on a TCP port");
|
||||
}
|
||||
|
||||
const host = listeningAddress.address === "::"
|
||||
? "[::1]"
|
||||
: listeningAddress.address === "0.0.0.0"
|
||||
? "127.0.0.1"
|
||||
: listeningAddress.address;
|
||||
const protocol = app instanceof TlsServer ? "https" : "http";
|
||||
return `${protocol}://${host}:${listeningAddress.port}${path}`;
|
||||
};
|
||||
|
||||
SupertestTest.prototype.__paperclipLoopbackPatched = true;
|
||||
}
|
||||
Reference in new issue
Block a user