This is a bit of sugar that reduces the boilerplate for creating testers in what I expect will become the standard case: a service in `foo/foo-thing.service.js` with its tests in `foo/foo-thing.tester.js`. This makes a small stylistic change, which is to name the service by its CamelCase class name rather than an invented snake-case ID. Whereas before the name was specified in `class FooThing extends Base[Json]Service` and a second time in the tester, it now only needs to be specified once.
30 lines
920 B
JavaScript
30 lines
920 B
JavaScript
'use strict'
|
|
|
|
const caller = require('caller')
|
|
const ServiceTester = require('./service-tester')
|
|
const BaseService = require('./base')
|
|
|
|
// Automatically create a ServiceTester. When run from e.g.
|
|
// `gem-rank.tester.js`, this will create a tester that attaches to the
|
|
// service found in `gem-rank.service.js`.
|
|
//
|
|
// This can't be used for `.service.js` files which export more than one
|
|
// service.
|
|
function createServiceTester() {
|
|
const servicePath = caller().replace('.tester.js', '.service.js')
|
|
let ServiceClass
|
|
try {
|
|
ServiceClass = require(servicePath)
|
|
} catch (e) {
|
|
throw Error(`Couldn't load service from ${servicePath}`)
|
|
}
|
|
if (!(ServiceClass.prototype instanceof BaseService)) {
|
|
throw Error(
|
|
`${servicePath} does not export a single service. Invoke new ServiceTester() directly.`
|
|
)
|
|
}
|
|
return ServiceTester.forServiceClass(ServiceClass)
|
|
}
|
|
|
|
module.exports = createServiceTester
|