Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict' | ||
|
||
/* | ||
* japa-cli | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
class JapaCli { | ||
constructor () { | ||
this._initiate() | ||
} | ||
|
||
/** | ||
* Initiate private and public properties | ||
* | ||
* @method _initiate | ||
* | ||
* @return {void} | ||
* | ||
* @private | ||
*/ | ||
_initiate () { | ||
this.filterCallback = null | ||
this.ignorePattern = [] | ||
this.testsGlob = 'test/*.spec.js' | ||
} | ||
|
||
/** | ||
* Filter files to be used for running the tests. You can | ||
* return a string, an array of globs or a function | ||
* which is called for each file. | ||
* | ||
* @method filter | ||
* | ||
* @param {String|Array|Function} patternOrCallback | ||
* | ||
* @chainable | ||
*/ | ||
filter (patternOrCallback) { | ||
if (typeof (patternOrCallback) === 'function') { | ||
this.filterCallback = patternOrCallback | ||
return this | ||
} | ||
|
||
if (typeof (patternOrCallback) === 'string') { | ||
this.ignorePattern = [patternOrCallback] | ||
return this | ||
} | ||
|
||
if (patternOrCallback instanceof Array === true) { | ||
this.ignorePattern = patternOrCallback | ||
return this | ||
} | ||
|
||
throw new Error('cli.filter only excepts a glob string, array or a callback function') | ||
} | ||
|
||
/** | ||
* Set the glob to be used for picking tests | ||
* files and then running the tests. | ||
* | ||
* @method run | ||
* | ||
* @param {String} glob | ||
* | ||
* @return {void} | ||
*/ | ||
run (glob) { | ||
if (!glob) { | ||
return | ||
} | ||
|
||
if (typeof (glob) !== 'string') { | ||
throw new Error(`cli.run excepts glob pattern to be a string. You passed ${typeof (glob)}`) | ||
} | ||
|
||
this.testsGlob = glob | ||
} | ||
} | ||
|
||
module.exports = new JapaCli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict' | ||
|
||
require('./string.spec.js') | ||
require('./fizzbuzz.spec.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const test = require('tape') | ||
const cli = require('../cli') | ||
|
||
test('allow a string to be ignored', (assert) => { | ||
assert.plan(1) | ||
cli.filter('foo.js') | ||
assert.deepEqual(cli.ignorePattern, ['foo.js']) | ||
cli._initiate() | ||
}) | ||
|
||
test('allow an array of globs to be ignored', (assert) => { | ||
assert.plan(1) | ||
cli.filter(['foo.js', 'bar.js']) | ||
assert.deepEqual(cli.ignorePattern, ['foo.js', 'bar.js']) | ||
cli._initiate() | ||
}) | ||
|
||
test('allow a callback to be passed to filter fn', (assert) => { | ||
assert.plan(2) | ||
cli.filter(function () {}) | ||
assert.equal(typeof (cli.filterCallback), 'function') | ||
assert.equal(cli.ignorePattern.length, 0) | ||
cli._initiate() | ||
}) | ||
|
||
test('throw exception when filter param is not an array,string of fn', (assert) => { | ||
assert.plan(1) | ||
try { | ||
cli.filter({}) | ||
} catch (error) { | ||
assert.equal(error.message, 'cli.filter only excepts a glob string, array or a callback function') | ||
cli._initiate() | ||
} | ||
}) | ||
|
||
test('set glob pattern for test files', (assert) => { | ||
assert.plan(2) | ||
assert.equal(cli.testsGlob, 'test/*.spec.js') | ||
cli.run('test/**/*.js') | ||
assert.equal(cli.testsGlob, 'test/**/*.js') | ||
cli._initiate() | ||
}) | ||
|
||
test('throw exception when pattern is not a string', (assert) => { | ||
assert.plan(1) | ||
try { | ||
cli.run(['test/**/*.js']) | ||
} catch (error) { | ||
assert.equal(error.message, 'cli.run excepts glob pattern to be a string. You passed object') | ||
cli._initiate() | ||
} | ||
}) |