Skip to content

Instantly share code, notes, and snippets.

@tohagan
Last active November 23, 2017 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tohagan/d4dc8acf54f44cd2a843b03fb54a8f5d to your computer and use it in GitHub Desktop.
Save tohagan/d4dc8acf54f44cd2a843b03fb54a8f5d to your computer and use it in GitHub Desktop.
import { BootScript } from '@mean-expert/boot-script';
import * as Mocha from 'mocha';
import * as fs from 'fs';
import * as path from 'path';
import { FsDirectory } from '../../common/util/fs-directory';
import { stat } from 'fs';
@BootScript()
class TestSuite {
private mocha: Mocha;
private rootDir: string = path.join(__dirname, '..', '..', 'tests');
constructor(app: any) {
if (process.env.NODE_ENV !== 'testing') return;
app.on('started', () => {
this.setup();
this.run();
});
}
setup() {
console.log('Setting up Mocha Environment.');
this.mocha = new Mocha();
let dirs = new FsDirectory(this.filterTests, this.mocha.addFile.bind(this.mocha))
.readSyncRecursive(this.rootDir);
}
filterTests(name: string, stats: fs.Stats): boolean {
return stats.isDirectory() || name.substr(-3) === '.ts';
}
run(): void {
console.log('Running Mocha Tests.');
this.mocha.run((failures: any) => {
console.log('Running Mocha Done');
process.exit(failures);
});
}
}
module.exports = TestSuite;
'use strict';
import * as path from 'path';
import * as fs from 'fs';
import { stat } from 'fs';
/**
* Filesystem directory wrapper to support recursive/filtered traversal.
*/
export class FsDirectory {
private files: string[];
constructor(
private filter?: (name: string, stats?: fs.Stats, index?: number, dir?: string) => boolean,
private cb?: (fileName: string, stats?: fs.Stats) => void
) {
this.cb = cb || (file => this.files.push(file));
this.filter = filter || (name => name[0] !== '.');
}
public readSyncRecursive(rootDir: string) {
this.visit(rootDir);
}
private visit(filename: string) {
var me = this;
var stats = fs.statSync(filename);
if (!fs.existsSync(filename)) return;
if (stats.isFile()) {
me.cb(filename, stats);
} else if (stats.isDirectory()) {
let dir = filename;
fs.readdirSync(dir)
.filter(function (name, index) {
return me.filter(name, stats, index, dir);
})
.forEach(function (name) {
me.readSyncRecursive(path.join(dir, name));
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment