From 263fdb6d8bd2af8aa618c43bc5e326d90174a130 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 2 Jul 2017 11:32:16 -0400 Subject: [PATCH] support specifying file or folder when running tests --- lib/cmd.js | 7 +++---- lib/core/run_tests.js | 41 +++++++++++++++++++++++++++++------------ lib/index.js | 4 ++-- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index a1836d44..062bc6e6 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -136,14 +136,13 @@ class Cmd { test() { program - .command('test') + .command('test [file]') .description('run tests') - .action(function () { + .action(function (file) { embark.initConfig('development', { embarkConfig: 'embark.json', interceptLogs: false }); - embark.runTests(); - //shelljs.exec('mocha test'); + embark.runTests(file); }); } diff --git a/lib/core/run_tests.js b/lib/core/run_tests.js index 7d7395e5..ad90fff6 100644 --- a/lib/core/run_tests.js +++ b/lib/core/run_tests.js @@ -1,24 +1,41 @@ module.exports = { - run: function() { + run: function(filepath) { var Mocha = require('mocha'), fs = require('fs'), path = require('path'); var mocha = new Mocha(); - var testDir = 'test/'; + if (filepath) { + if (filepath.substr(-1) === '/') { + // Add each .js file to the mocha instance + fs.readdirSync(filepath).filter(function(file){ + // Only keep the .js files + // TODO: make this a configuration in embark.json + return file.substr(-3) === '.js'; + }).forEach(function(file){ + mocha.addFile( + path.join(filepath, file) + ); + }); + } else { + mocha.addFile(filepath); + } + } else { + var testDir = 'test/'; - // Add each .js file to the mocha instance - fs.readdirSync(testDir).filter(function(file){ - // Only keep the .js files - // TODO: make this a configuration in embark.json - return file.substr(-3) === '.js'; - }).forEach(function(file){ - mocha.addFile( - path.join(testDir, file) - ); - }); + // Add each .js file to the mocha instance + fs.readdirSync(testDir).filter(function(file){ + // Only keep the .js files + // TODO: make this a configuration in embark.json + return file.substr(-3) === '.js'; + }).forEach(function(file){ + mocha.addFile( + path.join(testDir, file) + ); + }); + } let Test = require('./test.js'); diff --git a/lib/index.js b/lib/index.js index 08c33cf0..6764e4c3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -195,9 +195,9 @@ class Embark { } } - runTests() { + runTests(file) { let RunTests = require('./core/run_tests.js'); - RunTests.run(); + RunTests.run(file); } }