11import { URL , fileURLToPath } from 'node:url' ;
2- import { createServer } from 'node:net ' ;
2+ import { createServer } from 'node:http ' ;
33
44import { strictEqual , ok } from 'node:assert' ;
55
@@ -16,7 +16,6 @@ export const state = async () => {
1616export const source = ( testState ) => {
1717 let port = testState ?. port ? ':' + testState . port : '' ;
1818 const url = FETCH_URL + port ;
19- console . error ( '[test] fetch.js visiting URL' , url ) ;
2019 return `
2120 export async function run () {
2221 const res = await fetch('${ url } ');
@@ -38,22 +37,22 @@ export async function test(run, testState) {
3837 throw new Error ( 'missing port on test state' ) ;
3938 }
4039
40+ const url = FETCH_URL + ( port ? ':' + port : '' ) ;
41+
4142 // Run a local server on some port
42- console . error ( '[test] starting local server...' ) ;
4343 const server = createServer ( async ( req , res ) => {
4444 res . writeHead ( 200 , { 'Content-Type' : 'application/json; charset=utf-8' } ) ;
4545 res . write (
4646 JSON . stringify ( {
4747 status : 'ok' ,
48+ url,
4849 } ) ,
4950 ) ;
5051 res . end ( ) ;
5152 } ) . listen ( port ) ;
5253
5354 // Wait until the server is ready
54- console . error ( '[test] waiting for server to start...' ) ;
5555 let ready = false ;
56- const url = FETCH_URL + ( port ? ':' + port : '' ) ;
5756 while ( ! ready ) {
5857 try {
5958 const res = await fetch ( url ) ;
@@ -63,8 +62,9 @@ export async function test(run, testState) {
6362 }
6463 }
6564
66- console . error ( '[test] server started, running...' ) ;
6765 const { stdout, stderr } = await run ( ) ;
6866 strictEqual ( stderr , '' ) ;
69- strictEqual ( stdout . trim ( ) , FETCH_URL ) ;
67+ strictEqual ( stdout . trim ( ) , url ) ;
68+
69+ server . close ( ) ;
7070}
0 commit comments