From a89ed8fac9df79784882aebafaae9314f00336e6 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sat, 29 Jul 2017 13:03:40 +0530 Subject: [PATCH] refactor(*): add debug statements, fix parts of code --- commands/RunTests.js | 95 +++++++++++++++++++++++++++++++++------- package.json | 1 + providers/VowProvider.js | 8 +--- src/Cli/index.js | 7 ++- src/Runner/index.js | 9 ++++ 5 files changed, 97 insertions(+), 23 deletions(-) diff --git a/commands/RunTests.js b/commands/RunTests.js index d7580a7..382126e 100644 --- a/commands/RunTests.js +++ b/commands/RunTests.js @@ -9,9 +9,16 @@ * file that was distributed with this source code. */ +const _ = require('lodash') const { Command } = require('@adonisjs/ace') class RunTests extends Command { + constructor (runner, cli) { + super() + this.runner = runner + this.cli = cli + } + /** * The command signature * @@ -24,7 +31,9 @@ class RunTests extends Command { { type?=all: Define test types, needs to be unit or functional } { --bail: Stop running tests on first failure } { --timeout: Define a global timeout for all the tests } - { --files: Pick only specific files. File names are seperated by comma }` + { --files=@value: Pick only specific files. File names are seperated by comma } + { --grep=@value: Grep on tests title to run only selected tests } + { --glob=@value: Define a custom glob to pick test files }` } /** @@ -38,10 +47,15 @@ class RunTests extends Command { return 'Run application tests' } - constructor (runner, cli) { - super() - this.runner = runner - this.cli = cli + /** + * Injecting dependencies via IoC container + * + * @method inject + * + * @return {Array} + */ + static get inject () { + return ['Test/Runner', 'Test/Cli'] } /** @@ -50,32 +64,81 @@ class RunTests extends Command { * * @method handle * - * @param {String} options.type + * @param {String} options.type * @param {Boolean} options.bail - * @param {Number} options.timeout - * @param {String} options.files + * @param {Number} options.timeout + * @param {String} options.files + * @param {Boolean} options.grep + * @param {String} options.glob * * @return {void} */ - handle ({ type }, { bail, timeout, files }) { - if (bail) { - this.runner.bail(true) + async handle ({ type }, { bail, timeout, files, grep, glob }) { + this.runner.bail(bail || false) + + /** + * If grep statement is defined, use it + */ + if (grep) { + this.runner.grep(grep) } - if (type === 'unit') { - this.cli.functional(null) + /** + * If user has asked to run only unit tests, + * test functional tests glob to null + */ + if (type === 'unit' || (typeof (glob) === 'string' && glob)) { + this.cli.functional(glob) } - if (type === 'functional') { + /** + * If user has asked for functional tests, then + * set unit tests glob to null + */ + if (type === 'functional' || (typeof (glob) === 'string' && glob)) { this.cli.unit(null) } + /** + * If there is a global timeout set it on + * runner + */ if (timeout && Number(timeout)) { this.runner.timeout(Number(timeout)) } - const testFiles = this.cli.getTestFiles() - testFiles.forEach((file) => require(file)) + /** + * Only run tests for following files ( only if defined ) + * + * @type {Array} + */ + const filesToPick = typeof (files) === 'string' ? files.split(',') : [] + + /** + * Getting all test files from the cli + */ + let testFiles = await this.cli.getTestFiles() + + /** + * If there are specific files defined, then grep on + * them to pick only those files + */ + if (_.size(filesToPick)) { + testFiles = _.filter(testFiles, (file) => { + return !!_.find(filesToPick, (selectedFile) => { + return file.endsWith(selectedFile.trim()) + }) + }) + } + + try { + _.each(testFiles, (file) => require(file)) + await this.runner.run() + } catch (error) { + if (!this.runner.executedStack) { + console.error(error) + } + } } } diff --git a/package.json b/package.json index e220541..c32db4c 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "standard": "^10.0.2" }, "dependencies": { + "debug": "^2.6.8", "globby": "^6.1.0", "japa": "git+https://github.com/thetutlage/japa.git#develop", "lodash": "^4.17.4", diff --git a/providers/VowProvider.js b/providers/VowProvider.js index 23f4500..0d4e8ef 100644 --- a/providers/VowProvider.js +++ b/providers/VowProvider.js @@ -40,9 +40,8 @@ class VowProvider extends ServiceProvider { */ _registerCli () { this.app.singleton('Test/Cli', (app) => { - const Env = app.use('Adonis/Src/Env') const Cli = require('../src/Cli') - return new Cli(Env) + return new Cli(app.use('Adonis/Src/Env'), app.use('Adonis/Src/Helpers')) }) } @@ -101,10 +100,7 @@ class VowProvider extends ServiceProvider { * @private */ _registerTestCommand () { - this.app.bind('Adonis/Commands/Test', (app) => { - const RunTests = require('../commands/RunTests') - return new RunTests(app.use('Test/Runner'), app.use('Test/Cli')) - }) + this.app.bind('Adonis/Commands/Test', (app) => require('../commands/RunTests')) } /** diff --git a/src/Cli/index.js b/src/Cli/index.js index 41de246..d8387bd 100644 --- a/src/Cli/index.js +++ b/src/Cli/index.js @@ -11,6 +11,7 @@ const path = require('path') const globby = require('globby') +const debug = require('debug')('adonis:vow') /** * The Cli class is used to load the test files @@ -61,6 +62,7 @@ class Cli { * @chainable */ unit (glob) { + debug('setting unit tests glob as %s', glob) this._unitTests = glob return this } @@ -76,6 +78,7 @@ class Cli { * @chainable */ functional (glob) { + debug('setting functional tests glob as %s', glob) this._functionalTests = glob return this } @@ -126,7 +129,9 @@ class Cli { return files } - return files.filter(this._filterCallback) + const testFiles = files.filter(this._filterCallback) + debug('test files %j', testFiles) + return testFiles } } diff --git a/src/Runner/index.js b/src/Runner/index.js index fc6499c..d4406d4 100644 --- a/src/Runner/index.js +++ b/src/Runner/index.js @@ -12,6 +12,7 @@ const { Runner, reporters } = require('japa/api') const pSeries = require('p-series') const { resolver } = require('@adonisjs/fold') +const debug = require('debug')('adonis:vow') const Suite = require('../Suite') const props = require('../../lib/props') @@ -57,6 +58,7 @@ class TestRunner { * @private */ _runTraits (suite) { + debug('running %d trait(s) for %s suite', suite.traits.length, suite.group.title) suite.traits.forEach((trait) => { if (typeof (trait.action) === 'function') { return trait.action(suite, trait.options) @@ -80,6 +82,7 @@ class TestRunner { props.grep = null props.timeout = 2000 props.bail = false + this.executedStack = false this._stack = { before: [], after: [] @@ -98,6 +101,7 @@ class TestRunner { * @return {Suite} */ suite (title) { + debug('added new test suite %s', title) const suite = new Suite(title) this._suites.push(suite) return suite @@ -113,6 +117,7 @@ class TestRunner { * @return {void} */ timeout (timeout) { + debug('setting global timeout as %s', timeout) props.timeout = timeout } @@ -126,6 +131,7 @@ class TestRunner { * @return {void} */ bail (state) { + debug('toggling bail status to %s', state) props.bail = state } @@ -139,6 +145,7 @@ class TestRunner { * @chainable */ grep (term) { + debug('setting runner grep term as %s', term) props.grep = term return this } @@ -212,6 +219,8 @@ class TestRunner { this._runTraits(suite) groups.push(suite.group) }) + this.executedStack = true + debug('executing tests') await new Runner(groups, this._reporter, props).run() } catch (error) { testsError = error