This is consistent with what we're pretty much already doing, and saves us from making the request during code review. These were all autofixed and most of them seem easier to read. Some in the legacy services should be rewritten in more legible forms during refactor (ie using intermediate variables, or using request’s qs option). There are some in helper functions and elsewhere that should get rewritten separately. I don't want to change them in this PR because the changes will get lost in this diff, though we could identify them here and fix them before or just after.
68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
'use strict'
|
|
|
|
const { loadTesters } = require('../../services')
|
|
|
|
/**
|
|
* Load a collection of ServiceTester objects and register them with Mocha.
|
|
*/
|
|
class Runner {
|
|
/**
|
|
* Function to invoke before each test. This is a stub which can be
|
|
* overridden on instances.
|
|
*/
|
|
beforeEach() {}
|
|
|
|
/**
|
|
* Prepare the runner by loading up all the ServiceTester objects.
|
|
*/
|
|
prepare() {
|
|
this.testers = loadTesters()
|
|
this.testers.forEach(tester => {
|
|
tester.beforeEach = () => {
|
|
this.beforeEach()
|
|
}
|
|
})
|
|
}
|
|
|
|
_testersForService(service) {
|
|
return this.testers.filter(t => t.id.toLowerCase().startsWith(service))
|
|
}
|
|
|
|
/**
|
|
* Limit the test run to the specified services.
|
|
*
|
|
* @param services An array of service id prefixes to run
|
|
*/
|
|
only(services) {
|
|
const normalizedServices = new Set(services.map(v => v.toLowerCase()))
|
|
|
|
const missingServices = []
|
|
normalizedServices.forEach(service => {
|
|
const testers = this._testersForService(service)
|
|
|
|
if (testers.length === 0) {
|
|
missingServices.push(service)
|
|
}
|
|
|
|
testers.forEach(tester => {
|
|
tester.only()
|
|
})
|
|
})
|
|
|
|
// Throw at the end, to provide a better error message.
|
|
if (missingServices.length > 0) {
|
|
throw Error(`Unknown services: ${missingServices.join(', ')}`)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register the tests with Mocha.
|
|
*/
|
|
toss() {
|
|
this.testers.forEach(tester => {
|
|
tester.toss()
|
|
})
|
|
}
|
|
}
|
|
module.exports = Runner
|