diff --git a/generators/app/templates/tests/xqs/xqSuite.js b/generators/app/templates/tests/xqs/xqSuite.js index ef1be151..2b74a67a 100644 --- a/generators/app/templates/tests/xqs/xqSuite.js +++ b/generators/app/templates/tests/xqs/xqSuite.js @@ -1,7 +1,6 @@ 'use strict' const Mocha = require('mocha') -const Chai = require('chai') const http = require('http') const expect = require('chai').expect @@ -73,7 +72,7 @@ function xqsTests (mochaInstance, xqsPkg, xqstCount, xqstCase) { function xqsResult (suiteInstance, xqstCase) { suiteInstance.addTest(new Test('Test: ' + xqstCase.name, () => { - switch (xqstCase.hasOwnProperty()) { + switch (Object.prototype.hasOwnProperty.call(xqstCase, '') { // Red xqs test: filter to dynamically ouput messages only when record contains them case 'failure': expect(xqstCase, 'Function ' + xqstCase.class + ' ' + xqstCase.failure.message).to.not.have.own.property('failure') diff --git a/test/fixtures/mono-case.json b/test/fixtures/mono-case.json index d2c01573..d904f033 100644 --- a/test/fixtures/mono-case.json +++ b/test/fixtures/mono-case.json @@ -1,6 +1,6 @@ { "testsuite": { - "package": "https://dunddrum.eu/apps/mocha/tests", + "package": "https://dunddrum.eu/apps/mocha/mono", "timestamp": "2020-10-13T12:17:01.271Z", "tests": "1", "failures": "0", diff --git a/test/fixtures/multi-case.json b/test/fixtures/multi-case.json index 6913e856..b1c01992 100644 --- a/test/fixtures/multi-case.json +++ b/test/fixtures/multi-case.json @@ -1,6 +1,6 @@ { "testsuite": { - "package": "https://dunddrum.eu/apps/mocha/tests", + "package": "https://dunddrum.eu/apps/mocha/multi", "timestamp": "2020-10-13T12:18:34.64Z", "tests": "3", "failures": "1", diff --git a/test/xQsuite.js b/test/mock_rest.js similarity index 54% rename from test/xQsuite.js rename to test/mock_rest.js index 031ce6fc..89c0fd9b 100644 --- a/test/xQsuite.js +++ b/test/mock_rest.js @@ -48,37 +48,6 @@ describe('mocking xqSuite rest responses', function () { }) }) - describe('running mock XQsuite test', function () { - it('returns 0 errors or failures', function (done) { - client - .get('/exist/rest/db/my-app/modules/test-runner.xq') - .set('Accept', 'application/json') - .expect('content-type', 'application/json; charset=utf-8') - .end(function (err, res) { - try { - console.group() - console.group() - console.group() - console.info(res.body.testsuite.tests + ' xqsuite tests:') - if (err) return done(err) - } finally { - console.group() - res.body.testsuite.testcase.forEach(function (entry) { - if (entry.failure) console.error([entry.name, entry.failure.message]) - else if (entry.error) console.error([entry.name, entry.error.message]) - else (console.log(entry.name)) - }) - console.groupEnd() - } - console.groupEnd() - console.groupEnd() - console.groupEnd() - expect(res.body.testsuite.failures).to.equal('0') - expect(res.body.testsuite.errors).to.equal('0') - done() - }) - }) - }) // see http://www.marcusoft.net/2015/10/eaddrinuse-when-watching-tests-with-mocha-and-supertest.html after('shutdown mock server', function (done) { done() diff --git a/test/mock_xqs.js b/test/mock_xqs.js new file mode 100644 index 00000000..ac12e963 --- /dev/null +++ b/test/mock_xqs.js @@ -0,0 +1,105 @@ +'use strict' + +const Mocha = require('mocha') +const expect = require('chai').expect + +const monoCase = require('./fixtures/mono-case.json') +const multiCase = require('./fixtures/multi-case.json') +const multiSuite = require('./fixtures/multi-suite.json') + +describe('mock xqs runs', function () { + it('should pass the mono test', function (done) { + /* eslint-disable-next-line */ + expect(xqsMock(monoCase)).to.not.throw + done() + }) + + it('should fail the multi case', function (done) { + /* eslint-disable-next-line */ + expect(xqsMock(multiCase)).to.throw + done() + }) + + it('should pass the mutli suite', function (done) { + /* eslint-disable-next-line */ + expect(xqsMock(multiSuite)).to.not.throw + done() + }) +}) + +function xqsMock (mockRun) { + // Dynamically generate a mocha testsuite for xqsuite tests. Requires its own process, hence && in package.json + const Test = Mocha.Test + + const xqsReport = mockRun + const xqsPkg = xqsReport.testsuite.package + const xqstCount = xqsReport.testsuite.tests + const xqstCase = xqsReport.testsuite.testcase + + // TODO: get rid of first "0 passing message" + + const mochaInstance = new Mocha() + + if (Array.isArray(xqsReport.testsuite)) { + const xqsSuites = xqsReport.testsuite + console.warn('support for multiple testsuites per run is experimental') + xqsSuites.forEach((entry) => { + xqsTests(mochaInstance, entry.package, entry.tests, entry.testcase) + }) + } else { + xqsTests(mochaInstance, xqsPkg, xqstCount, xqstCase) + } + // enable repeated runs + // see https://github.com/mochajs/mocha/issues/995 + // see https://mochajs.org/api/mocha#unloadFiles + const suiteRun = mochaInstance.cleanReferencesAfterRun(true).run() + process.on('exit', () => { + process.exit(suiteRun.stats.failures > 0) + }) + + // TODO: mark %pending xqstests as pending in mocha report + function xqsTests (mochaInstance, xqsPkg, xqstCount, xqstCase) { + const suiteInstance = Mocha.Suite.create(mochaInstance.suite, 'Xqsuite tests for ' + xqsPkg) + + if (xqstCase === undefined) { + // if xqs contains 0 tests close open mocha instance + mochaInstance.unloadFiles() + suiteInstance.dispose() + console.log('no test cases defined by suite ' + xqsPkg) + } else if (Array.isArray(xqstCase)) { + for (let i = 0; i < xqstCount; i++) { + xqsResult(suiteInstance, xqstCase[i]) + } + } else { + xqsResult(suiteInstance, xqstCase) + } + } + + function xqsResult (suiteInstance, xqstCase) { + suiteInstance.addTest(new Test('Test: ' + xqstCase.name, () => { + switch (Object.prototype.hasOwnProperty.call(xqstCase, '') + + ) { + // Red xqs test: filter to dynamically ouput messages only when record contains them + case 'failure': + expect(xqstCase, 'Function ' + xqstCase.class + ' ' + xqstCase.failure.message).to.not.have.own.property('failure') + break + case 'error': + expect(xqstCase, 'Function ' + xqstCase.class + ' ' + xqstCase.error.message).to.not.have.own.property('error') + break + // TODO: Blue xqs tests: pending not yet implemented + case 'pending': + Test.isPending(true) + break + // Green xqs tests: pass passing tests + default: + /* eslint-disable-next-line */ + expect(xqstCase.failure).to.not.exist + /* eslint-disable-next-line */ + expect(xqstCase.error).to.not.exist + break + } + } + )) + } +}