diff --git a/test/cds.js b/test/cds.js deleted file mode 100644 index bca7bf00..00000000 --- a/test/cds.js +++ /dev/null @@ -1,158 +0,0 @@ -const cds = module.exports = require('@sap/cds/lib') -if (!cds.test) { // monkey patching cds - - const { resolve, dirname } = require('path') - const cwd = process.cwd() - - // harmonizing jest and mocha - const is_mocha = !global.test - const is_jest = !!global.test - if (is_jest) { // it's jest - global.before = (msg,fn) => global.beforeAll(fn||msg) - global.after = (msg,fn) => global.afterAll(fn||msg) - } else { // it's mocha - global.beforeAll = global.before - global.afterAll = global.after - global.test = global.it - } - - // eslint-disable-next-line no-global-assign - require = (mod) => { - try { return module.require(mod) } - catch(e) { if (e.code === 'MODULE_NOT_FOUND') throw new Error (` - Failed to load required package '${mod}'. Please add it thru: - npm add -D ${mod === 'chai' ? 'chai chai-as-promised chai-subset' : mod} - `) } - } - - - - class Test { - - /** - * Launches a cds server with arbitrary port and returns a subclass which - * also acts as an axios lookalike, providing methods to send requests. - * @returns {Test} - */ - static run (cmd, ...args) { - - // Setting up test server - const console = global.console, logs=[] - const axios = require('axios').default - const test = {__proto__:Test.run, - - GET: (path,...etc) => axios.get (test.url+path,...etc) .catch(_error), - PUT: (path,...etc) => axios.put (test.url+path,...etc) .catch(_error), - POST: (path,...etc) => axios.post (test.url+path,...etc) .catch(_error), - PATCH: (path,...etc) => axios.patch (test.url+path,...etc) .catch(_error), - DEL: (path,...etc) => axios.delete (test.url+path,...etc) .catch(_error), - - get: (path,...etc) => axios.get (test.url+path,...etc) .catch(_error), - put: (path,...etc) => axios.put (test.url+path,...etc) .catch(_error), - post: (path,...etc) => axios.post (test.url+path,...etc) .catch(_error), - patch: (path,...etc) => axios.patch (test.url+path,...etc) .catch(_error), - delete: (path,...etc) => axios.delete (test.url+path,...etc) .catch(_error), - - } - - // launch cds server... - before (`launching ${cmd} ${args.join(' ')}...`, done => { - - // const cds = require('../index'), - const { isdir } = cds.utils - if (!args.length) { - let project = cmd; cmd = 'run' - if (isdir(project)) ; //> all fine - // Supporting .launch () - else if (isdir(resolve(project))) project = resolve(project) - else try { project = dirname (require.resolve(project+'/package.json')) } - catch(e) { throw cds.error (`Cannot resolve project folder for '${project}' in '${process.cwd()}'`) } - args.push (project, '--in-memory?') - } - - 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), - warn: (...args)=> logs.push(args), - error: (...args)=> logs.push(args), - dump(){ for (let each of logs) console.log (...each) }, - } - - // return done (new Error(11)) - process.env.PORT = '0' - const p = cds.exec (cmd, ...args) // TODO w/ @sap/cds@3.33.3: , '--port', '0') - if (p && 'catch' in p) p.catch (e => { - if (is_mocha) console.error(e) - done(e) - }) - - cds.once('listening', ({ server, url }) => { - Object.assign (test,{server,url}) - done() - }) - }) - - // shutdown cds server... - after (done => { - if (global.console !== console) global.console = console - if (cwd !== process.cwd()) process.chdir(cwd) - test.server ? test.server.close (done) : done() - process.emit('shutdown') - }) - - function _error (e) { - if (!e.response) throw e - if (!e.response.data) throw e - if (!e.response.data.error) throw e - const { code, message } = e.response.data.error - throw new Error (code && code !== 'null' ? `${code} - ${message}` : message) - } - - return test - } - - /** - * Serving projects from subfolders under the root specified by a sequence - * of path components which are concatenated with path.resolve(). - */ - in (...paths) { - process.chdir (resolve (...paths)) - return this - } - - /** - * Switch on/off console log output. - */ - verbose(v) { - process.env.CDS_TEST_VERBOSE = v - return this - } - - /** Lazily loads and returns an instance of chai */ - get chai() { - const chai = require('chai') - chai.use (require('chai-subset')) - chai.use (require('chai-as-promised')) - Object.defineProperty (this, 'chai', {value:chai}) - return chai - } - get expect(){ return this.chai.expect } - get assert(){ return this.chai.assert } - } - - - /** - * Test kit for jest or mocha testing, which can be used statically - * via the getters for chai, expect and assert or through a server - * started with cds.test(...). - * @type Test.run & Test - */ - Object.defineProperties (Test.run, Object.getOwnPropertyDescriptors (Test.prototype)) - Object.defineProperty (cds, 'test', {value:Test.run}) - const cds_load = cds.load; cds.load = (models,o)=>{ - if (typeof models === 'string') models = models.split(',') - return cds_load.call(cds,models,o) - } - -} diff --git a/test/cds.ql.test.js b/test/cds.ql.test.js index 010b6403..3af3327a 100644 --- a/test/cds.ql.test.js +++ b/test/cds.ql.test.js @@ -1,4 +1,5 @@ -const cds = require('./cds'),{ expect } = cds.test +const cds = require('@sap/cds/lib') +const { expect } = cds.test const CQL = ([cql]) => cds.parse.cql(cql) const Foo = { name: 'Foo' } const Books = { name: 'capire.bookshop.Books' } diff --git a/test/consuming-services.test.js b/test/consuming-services.test.js index 96d49f60..ab550ef4 100644 --- a/test/consuming-services.test.js +++ b/test/consuming-services.test.js @@ -1,4 +1,4 @@ -const cds = require('./cds') +const cds = require('@sap/cds/lib') const { expect } = cds.test ( 'serve', 'AdminService', '--from', '@capire/bookshop,@capire/common', '--in-memory' ).in(__dirname) diff --git a/test/custom-handlers.test.js b/test/custom-handlers.test.js index b2562644..8e798d97 100644 --- a/test/custom-handlers.test.js +++ b/test/custom-handlers.test.js @@ -1,9 +1,5 @@ -const { GET, POST, expect } = require('./cds').test('bookshop').in(__dirname,'..') -const is_jest = !!global.test -if (is_jest) { // it's jest - global.before = (msg,fn) => global.beforeAll(fn||msg) - global.after = (msg,fn) => global.afterAll(fn||msg) -} +const cds = require('@sap/cds/lib') +const { GET, POST, expect } = cds.test('bookshop').in(__dirname,'..') describe('Custom Handlers', () => { diff --git a/test/hello-world.test.js b/test/hello-world.test.js index d6d32974..d8f58a80 100644 --- a/test/hello-world.test.js +++ b/test/hello-world.test.js @@ -1,4 +1,4 @@ -const cds = require ('./cds') +const cds = require('@sap/cds/lib') const { GET, expect } = cds.test('serve','hello/world.cds').in(__dirname,'..') describe('Hello world!', () => { diff --git a/test/hierarchical-data.test.js b/test/hierarchical-data.test.js index b4ea3a32..d92dfcf9 100644 --- a/test/hierarchical-data.test.js +++ b/test/hierarchical-data.test.js @@ -1,5 +1,6 @@ const cwd = process.cwd(); process.chdir (__dirname) //> only for internal CI/CD@SAP -const cds = require ('./cds'), {expect} = cds.test +const cds = require('@sap/cds/lib') +const {expect} = cds.test // monkey patching older releases: if (!cds.compile.cdl) cds.compile.cdl = cds.parse diff --git a/test/localized-data.test.js b/test/localized-data.test.js index 4b345695..ad8256b9 100644 --- a/test/localized-data.test.js +++ b/test/localized-data.test.js @@ -1,4 +1,4 @@ -const cds = require ('./cds') +const cds = require('@sap/cds/lib') const { GET, expect } = cds.test ('serve', __dirname+'/localized-data.cds', '--in-memory') describe('Localized Data', () => { diff --git a/test/messaging.test.js b/test/messaging.test.js index 632b093f..a3e0df3e 100644 --- a/test/messaging.test.js +++ b/test/messaging.test.js @@ -1,5 +1,6 @@ +const cds = require('@sap/cds/lib') const cwd = process.cwd(); process.chdir (__dirname) //> only for internal CI/CD@SAP -const cds = require ('./cds'), {expect} = cds.test +const {expect} = cds.test const _model = '@capire/reviews' diff --git a/test/odata.test.js b/test/odata.test.js index ca89a20d..4a561a95 100644 --- a/test/odata.test.js +++ b/test/odata.test.js @@ -1,9 +1,10 @@ -const { GET, expect } = require('./cds').test('bookshop').in(__dirname,'..') +const cds = require('@sap/cds/lib') +const { GET, expect } = cds.test('bookshop').in(__dirname,'..') describe('OData Protocol', () => { it('serves $metadata documents in v4', async () => { - const { headers, status, data } = await GET`/browse/$metadata` + const { headers, status, data } = await GET `/browse/$metadata` expect(status).to.equal(200) expect(headers).to.contain({ 'content-type': 'application/xml',