import execa from 'execa' ; import express from 'express' ; import http from 'http' ; import * as multiparty from 'multiparty' ; import cors from 'cors' ; const port = 7172 ; async function listen ( app , port ) { return new Promise ( ( resolve , reject ) => { const server = app . listen ( port , ( err ? ) => { if ( err ) { reject ( err ) ; } resolve ( server ) ; } ) ; } ) ; } async function main ( ) { const app = express ( ) ; app . use ( cors ( ) ) ; app . post ( '/path' , ( req , res ) => { if ( req . header ( 'origin' ) ) { res . set ( 'Access-Control-Allow-Origin' , req . header ( 'origin' ) ) ; } const form = new multiparty . Form ( { autoFields : true } ) ; form . parse ( req , ( err , fields ) => { if ( err ) { res . status ( 500 ) . send ( { message : err . message } ) ; } else { res . status ( 200 ) . send ( fields . null ) ; } } ) ; } ) ; app . get ( '/path' , ( req , res ) => { if ( req . header ( 'origin' ) ) { res . set ( 'Access-Control-Allow-Origin' , req . header ( 'origin' ) ) ; } res . send ( 'response' ) ; } ) ; app . get ( '/querystring' , ( req , res ) => { if ( req . header ( 'origin' ) ) { res . set ( 'Access-Control-Allow-Origin' , req . header ( 'origin' ) ) ; } const query = req . query . query ; res . send ( query || '' ) ; } ) ; const server = await listen ( app , port ) ; console . log ( `[http server] I'm listening on port ${ port }! Starting karma.` ) ; const result = await execa ( 'karma' , [ 'start' ] , { stdio : 'inherit' } ) ; server . close ( ) ; console . log ( `[http server] Karma has finished! I'm no longer listening on port ${ port }!` ) ; if ( result . failed ) { throw new Error ( 'Tests failed.' ) ; } } main ( ) . catch ( err => { console . log ( 'Error:' , err ) ; } ) ;