Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
refactor(*): add debug statements, fix parts of code
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 29, 2017
1 parent 689c65f commit a89ed8f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
95 changes: 79 additions & 16 deletions commands/RunTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 }`
}

/**
Expand All @@ -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']
}

/**
Expand All @@ -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)
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions providers/VowProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
}

Expand Down Expand Up @@ -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'))
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +62,7 @@ class Cli {
* @chainable
*/
unit (glob) {
debug('setting unit tests glob as %s', glob)
this._unitTests = glob
return this
}
Expand All @@ -76,6 +78,7 @@ class Cli {
* @chainable
*/
functional (glob) {
debug('setting functional tests glob as %s', glob)
this._functionalTests = glob
return this
}
Expand Down Expand Up @@ -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
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -80,6 +82,7 @@ class TestRunner {
props.grep = null
props.timeout = 2000
props.bail = false
this.executedStack = false
this._stack = {
before: [],
after: []
Expand All @@ -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
Expand All @@ -113,6 +117,7 @@ class TestRunner {
* @return {void}
*/
timeout (timeout) {
debug('setting global timeout as %s', timeout)
props.timeout = timeout
}

Expand All @@ -126,6 +131,7 @@ class TestRunner {
* @return {void}
*/
bail (state) {
debug('toggling bail status to %s', state)
props.bail = state
}

Expand All @@ -139,6 +145,7 @@ class TestRunner {
* @chainable
*/
grep (term) {
debug('setting runner grep term as %s', term)
props.grep = term
return this
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a89ed8f

Please sign in to comment.