Skip to content

Commit

Permalink
test(mocha): allign meta test
Browse files Browse the repository at this point in the history
allign generator test with generated test

see eXist-db#513
  • Loading branch information
duncdrum committed Nov 2, 2020
1 parent 57f7f2e commit 9c38c02
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 35 deletions.
3 changes: 1 addition & 2 deletions generators/app/templates/tests/xqs/xqSuite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const Mocha = require('mocha')
const Chai = require('chai')
const http = require('http')
const expect = require('chai').expect

Expand Down Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/mono-case.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/multi-case.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
31 changes: 0 additions & 31 deletions test/xQsuite.js → test/mock_rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
105 changes: 105 additions & 0 deletions test/mock_xqs.js
Original file line number Diff line number Diff line change
@@ -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
}
}
))
}
}

0 comments on commit 9c38c02

Please sign in to comment.