Skip to content

Instantly share code, notes, and snippets.

@jhoguet
Last active March 5, 2016 19:04
Show Gist options
  • Save jhoguet/626d1b6d66a345f5bc3e to your computer and use it in GitHub Desktop.
Save jhoguet/626d1b6d66a345f5bc3e to your computer and use it in GitHub Desktop.
this ended up being too slow (given the overhead of spinning up mocha with babel). I could probably spin up mocha in memory (with babel) and re-run that way and be fast again... not really worth it til I have a lot more files so just using --watch for now
var gaze = require('gaze');
var Rx = require('rxjs');
var spawn = require('child_process').spawn;
// Watch all .js files/dirs in process.cwd()
gaze([
'./src/**/*.js',
'./src/*.js'
],
function(err, watcher) {
const getAllWatchedFiles = this.watched.bind(this);
this.on('all', function(event, filepath) {
const testFilePath = getTestFilePathFor(filepath, getAllWatchedFiles());
if (testFilePath){
console.log('deteched change in', filepath, 'testing with', testFilePath);
var child = spawn('node_modules/.bin/mocha', ['--compilers', 'js:babel-register', testFilePath], { stdio : 'inherit'});
}
});
console.log('watching for file changes');
}
);
function getTestFilePathFor(filePath, allWatchedFilePaths){
const flatFilePaths = flatten(allWatchedFilePaths);
// if it is a test file return it
if (filePath.endsWith('.tests.js')){
return filePath;
}
// if it has a test file, return the test file
const testFilePath = filePath.replace(/\.js$/, '.tests.js');
return flatFilePaths.indexOf(testFilePath) !== -1 ? testFilePath : null;
}
function flatten(allWatchedFilePaths){
return Object.keys(allWatchedFilePaths).reduce(function (accumulator, key) {
return accumulator.concat(allWatchedFilePaths[key]);
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment