From 428f1ce29dbfa436847f97f13fd00746f5c76cc3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 5 Apr 2020 14:41:14 +0200 Subject: [PATCH] Fixed hanging test servers --- test/lib/helpers.js | 56 ++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/test/lib/helpers.js b/test/lib/helpers.js index 04fbeaa2..25664118 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -29,20 +29,39 @@ exports.launch = (project, args=['--in-memory?']) => { const cds = require('@sap/cds') + // Supporting .launch () 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 }