Fixed hanging test servers
This commit is contained in:
@@ -29,20 +29,39 @@ exports.launch = (project, args=['--in-memory?']) => {
|
||||
|
||||
const cds = require('@sap/cds')
|
||||
|
||||
// Supporting .launch (<package name>)
|
||||
if (!cds.utils.existsSync(project)) try {
|
||||
project = require('path').dirname (require.resolve(project+'/package.json'))
|
||||
} catch(e) {
|
||||
throw cds.error (`Cannot resolve project folder for '${project}'`)
|
||||
}
|
||||
|
||||
// Setting up test server
|
||||
const console = global.console, logs=[]
|
||||
const axios = require('axios').default
|
||||
let baseURL //... be filled in below
|
||||
const test = {
|
||||
|
||||
GET: (path) => axios.get (test.url+path) .catch(_error),
|
||||
PUT: (path,data) => axios.put (test.url+path,data) .catch(_error),
|
||||
POST: (path,data) => axios.post (test.url+path,data) .catch(_error),
|
||||
PATCH: (path,data) => axios.patch (test.url+path,data) .catch(_error),
|
||||
DELETE: (path) => axios.delete (test.url+path) .catch(_error),
|
||||
|
||||
get: (path) => axios.get (test.url+path) .catch(_error),
|
||||
put: (path,data) => axios.put (test.url+path,data) .catch(_error),
|
||||
post: (path,data) => axios.post (test.url+path,data) .catch(_error),
|
||||
patch: (path,data) => axios.patch (test.url+path,data) .catch(_error),
|
||||
delete: (path) => axios.delete (test.url+path) .catch(_error),
|
||||
|
||||
get chai(){ return _chai() },
|
||||
get expect(){ return this.chai.expect },
|
||||
get assert(){ return this.chai.assert },
|
||||
}
|
||||
|
||||
// launch cds server...
|
||||
before (done => {
|
||||
|
||||
const console = global.console, logs=[]
|
||||
global.console = { __proto__: global.console, logs,
|
||||
if (!process.env.CDS_TEST_VERBOSE) global.console = { __proto__: global.console, logs,
|
||||
time: ()=>{}, timeEnd: (...args)=> logs.push(args),
|
||||
debug: (...args)=> logs.push(args),
|
||||
log: (...args)=> logs.push(args),
|
||||
@@ -56,33 +75,16 @@ exports.launch = (project, args=['--in-memory?']) => {
|
||||
if (p && 'catch' in p) p.catch (done)
|
||||
|
||||
cds.once('listening', ({ server, url }) => {
|
||||
after (done => {
|
||||
if (global.console !== console) global.console = console
|
||||
server.close (done)
|
||||
})
|
||||
baseURL = url
|
||||
Object.assign (test,{server,url})
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
return {
|
||||
|
||||
GET: (path) => axios.get (baseURL+path) .catch(_error),
|
||||
PUT: (path,data) => axios.put (baseURL+path,data) .catch(_error),
|
||||
POST: (path,data) => axios.post (baseURL+path,data) .catch(_error),
|
||||
PATCH: (path,data) => axios.patch (baseURL+path,data) .catch(_error),
|
||||
DELETE: (path) => axios.delete (baseURL+path) .catch(_error),
|
||||
|
||||
get: (path) => axios.get (baseURL+path) .catch(_error),
|
||||
put: (path,data) => axios.put (baseURL+path,data) .catch(_error),
|
||||
post: (path,data) => axios.post (baseURL+path,data) .catch(_error),
|
||||
patch: (path,data) => axios.patch (baseURL+path,data) .catch(_error),
|
||||
delete: (path) => axios.delete (baseURL+path) .catch(_error),
|
||||
|
||||
get chai(){ return _chai() },
|
||||
get expect(){ return this.chai.expect },
|
||||
get assert(){ return this.chai.assert },
|
||||
}
|
||||
// shutdown cds server...
|
||||
after (done => {
|
||||
if (global.console !== console) global.console = console
|
||||
test.server.close (done)
|
||||
})
|
||||
|
||||
function _error (e) {
|
||||
if (!e.response) throw e
|
||||
@@ -91,4 +93,6 @@ exports.launch = (project, args=['--in-memory?']) => {
|
||||
const { code, message } = e.response.data.error
|
||||
throw new Error (`${code} - ${message}`)
|
||||
}
|
||||
|
||||
return test
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user