Skip to content

Instantly share code, notes, and snippets.

@joelharkes
Created November 21, 2017 13:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelharkes/e67652454fad825b8fa025ba6f927596 to your computer and use it in GitHub Desktop.
Save joelharkes/e67652454fad825b8fa025ba6f927596 to your computer and use it in GitHub Desktop.
Run ts-mocha programmatically (in a script)
require('ts-mocha');
const Mocha = require('mocha');
const fs = require('fs');
let testSubject = null;
if (process.argv.length === 3) {
testSubject = process.argv[2];
}
const mocha = new Mocha({ timeout: 50000 });
if (testSubject && testSubject.endsWith('.test.ts')) {
mocha.addFile(`tests/${testSubject}`);
console.log(`using single file ${testSubject}`);
} else if (testSubject) {
// expect test subject to be a folder
fs.readdirSync(`tests/${testSubject}`)
.filter(file => file.endsWith('.test.ts'))
.forEach(file => mocha.addFile(`tests/${testSubject}/${file}`));
} else {
fs.readdirSync('tests/units')
.filter(file => file.endsWith('.test.ts'))
.forEach(file => mocha.addFile(`tests/${file}`));
}
mocha.run((failures) => {
process.on('exit', () => {
process.exit(failures); // exit with non-zero status if there were failures
});
// Explicitly kill application, something makes it hang.
process.exit(failures);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment