Files
shields/service-tests/runner/runner.js
Paul Melnikow 5c147b8d91 Service tests (#937)
- Eliminate manual testing which is error-prone and time consuming, and must be repeated many times through the PR review process
- Make contributing more fun. For many, fixing bugs and making new badges is faster and more satisfying with automated tests than with manual testing.
- Push out the work of testing new badges to a much broader net. The PR originator could write tests, but so could any other contributor who wants to push review along.
- Detect badge failures resulting from changes in vendor contracts without waiting for user reports.
- Detect and prevent regressions in the code.
- Be runnable, readable, writable, and editable by as many developers as possible, including those who may not be familiar with JavaScript test tools.

-- @paulmelnikow, @niccokunzmann, @Daniel15
2017-04-27 23:13:14 -04:00

62 lines
1.5 KiB
JavaScript

'use strict';
const glob = require('glob');
/**
* 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 = glob.sync(`${__dirname}/../*.js`).map(name => require(name));
this.testers.forEach(tester => {
tester.beforeEach = () => { this.beforeEach(); };
});
}
_testersForService (service) {
return this.testers.filter(t => t.id.toLowerCase() === service);
}
/**
* Limit the test run to the specified services.
*
* @param services An array of service ids 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;