Skip to content

Instantly share code, notes, and snippets.

@ddimitrioglo
Last active January 19, 2018 21:55
Show Gist options
  • Save ddimitrioglo/8862a7184fb8b7d2d1a57cc36c62860f to your computer and use it in GitHub Desktop.
Save ddimitrioglo/8862a7184fb8b7d2d1a57cc36c62860f to your computer and use it in GitHub Desktop.
Mocha test runner
'use strict';
const fs = require('fs');
const path = require('path');
const Mocha = require('mocha');
const uuidv1 = require('uuid/v1');
class MochaRunner {
/**
* @param {Object} options
*/
constructor(options = {}) {
this._tmps = [];
this._mocha = new Mocha(options);
}
/**
* Run tests
* @param {Array} tests
* @returns {Promise}
*/
run(tests) {
return new Promise((resolve, reject) => {
tests.forEach(test => {
const testDir = path.dirname(test);
const tmpTest = path.join(testDir, `${uuidv1()}.spec.js`);
fs.writeFileSync(tmpTest, fs.readFileSync(test));
this._tmps.push(tmpTest);
this._mocha.addFile(tmpTest);
});
this._mocha.run(err => {
return err ? reject(err) : resolve();
});
}).catch(() => {
return this.cleanup();
});
}
/**
* Get mocha instance
* @returns {Mocha}
*/
getMocha() {
return this._mocha;
}
/**
* Remove tmp test files
* @returns {Promise}
*/
cleanup() {
this._tmps.forEach(tmpTest => {
fs.unlinkSync(tmpTest);
});
return Promise.resolve();
}
}
module.exports = MochaRunner;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment