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