Skip to content

Commit

Permalink
Merge branch 'release-1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 1, 2017
2 parents be74183 + c2915c3 commit f537cf5
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 47 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,19 @@
<a name="1.0.3"></a>
## [1.0.3](https://github.com/thetutlage/japa/compare/v1.0.2...v1.0.3) (2017-04-01)


### Bug Fixes

* **middleware:** do not pass next to wrapFn ([de5d1c4](https://github.com/thetutlage/japa/commit/de5d1c4))
* **middleware:** read bail value from the util module ([f24d2d7](https://github.com/thetutlage/japa/commit/f24d2d7))


### Features

* **reporter:** show custom message when zero tests ran ([6ce1d59](https://github.com/thetutlage/japa/commit/6ce1d59))



<a name="1.0.2"></a>
## [1.0.2](https://github.com/thetutlage/japa/compare/v1.0.1...v1.0.2) (2017-04-01)

Expand Down
2 changes: 0 additions & 2 deletions index.js
Expand Up @@ -10,7 +10,6 @@
*/

const runner = new (require('./src/Runner'))()
const cli = require('./cli')

const nextTick = typeof (setImmediate) !== 'undefined' ? setImmediate : process.nextTick

Expand All @@ -33,4 +32,3 @@ exports.timeout = runner.timeout.bind(runner)
exports.use = runner.use.bind(runner)
exports.bail = runner.bail.bind(runner)
exports.grep = runner.grep.bind(runner)
exports.cli = cli
46 changes: 37 additions & 9 deletions lib/util.js
Expand Up @@ -9,22 +9,50 @@
* file that was distributed with this source code.
*/

const util = exports = module.exports = {}

let DEFAULT_TIMEOUT = 2000
let BAIL_TESTS = false

util.verb = function (count) {
return count === 1 ? '' : 's'
}
const util = exports = module.exports = {
get timeout () {
return DEFAULT_TIMEOUT
},

util.setTimeout = function (timeout) {
DEFAULT_TIMEOUT = timeout
set timeout (timeout) {
DEFAULT_TIMEOUT = timeout
},

get bail () {
return BAIL_TESTS
},

set bail (state) {
BAIL_TESTS = !!state
}
}

util.getTimeout = function () {
return DEFAULT_TIMEOUT
/**
* The `s` verb to be used by checking the
* count. The simplest way to append `s`
* to statement to make it plural only
* when count is not = 1.
*
* @method verb
*
* @param {Number} count
*
* @return {String}
*/
util.verb = function (count) {
return count === 1 ? '' : 's'
}

/**
* List of events emitted by Japa
*
* @method getEventsList
*
* @return {Object}
*/
util.getEventsList = function () {
return {
GROUP_START: 'group:start',
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "japa",
"version": "1.0.2",
"version": "1.0.3",
"description": "Japa is a batteries included minimal testing framework for Node.Js. Japa does not have any cli to run your tests, infact running the test file as a node script will execute the tests for you (quite similar to tape)",
"main": "index.js",
"directories": {
Expand Down
10 changes: 4 additions & 6 deletions src/Group.js
Expand Up @@ -17,18 +17,17 @@ const emitter = require('../lib/emitter')
const eventsList = $.getEventsList()

class Group {
constructor (title, bail, isRoot) {
constructor (title, isRoot) {
this._title = title
this._isRoot = !!isRoot
this._timeout = $.getTimeout()
this._hooks = {
beforeEach: [],
afterEach: [],
before: [],
after: []
}
this._tests = []
this.middleware = new Middleware(this, !!bail, this._wrapFn)
this.middleware = new Middleware(this, this._wrapFn)
}

/**
Expand Down Expand Up @@ -66,13 +65,12 @@ class Group {
* Wraps the test/hook as a middleware fn
*
* @param {Object} fn
* @param {Promise} next
* @return {Promise}
*
* @private
*/
_wrapFn (fn, next) {
return new Promise((resolve, reject) => fn.run().then(next).then(resolve).catch((error) => {
_wrapFn (fn) {
return new Promise((resolve, reject) => fn.run().then(resolve).catch((error) => {
reject({title: fn._title, error: error})
}))
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hook.js
Expand Up @@ -19,7 +19,7 @@ class Hook {
this._title = groupTitle
this._hookFor = hookFor
this._callback = callback
this._timeout = $.getTimeout()
this._timeout = $.timeout
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Middleware.js
Expand Up @@ -9,11 +9,12 @@
* file that was distributed with this source code.
*/

const $ = require('../lib/util')

class Middleware {

constructor (context, bail, fnWrapper) {
constructor (context, fnWrapper) {
this._context = context
this._bail = !!bail
this._fnWrapper = fnWrapper
this._stack = []
this.errorsStack = []
Expand All @@ -29,7 +30,7 @@ class Middleware {
* @param {Function}
*/
_internalRejection (index, resolve, reject, error) {
if (this._bail) {
if ($.bail) {
reject(error)
return
}
Expand All @@ -51,7 +52,8 @@ class Middleware {

const fn = this._stack[index]
return new Promise((resolve, reject) => {
this._fnWrapper(fn, next => this._dispatch(index + 1))
this._fnWrapper(fn)
.then(() => this._dispatch(index + 1))
.then(resolve)
.catch((error) => this._internalRejection.bind(this)(index, resolve, reject, error))
})
Expand Down Expand Up @@ -82,7 +84,6 @@ class Middleware {
this._stack = stack
return this._middleware.bind(this)
}

}

module.exports = Middleware
15 changes: 15 additions & 0 deletions src/Reporters/list.js
Expand Up @@ -235,6 +235,21 @@ class Min {
const end = new Date() - this.start
this.blankLine()

/**
* Show a small message with Zero tests ran when
* total count of tests is zero
*
* @method if
*
* @param {[type]} this.finalStats.total [description]
*
* @return {[type]} [description]
*/
if (this.finalStats.total === 0) {
this.log(chalk.bgMagenta.white(' 0 TESTS RAN '))
return
}

this.printStack(error)

if (status === 'passed') {
Expand Down
38 changes: 18 additions & 20 deletions src/Runner.js
Expand Up @@ -16,12 +16,11 @@ const emitter = require('../lib/emitter')
const $ = require('../lib/util')

class Runner {
constructor (bail) {
constructor () {
this._testReporter = require('../src/Reporters/list')
this._testGroups = []
this._pushDefaultGroup()
this._grepOn = null
this._bail = !!bail
this.emitter = emitter // refrence to emitter for listening events
}

Expand Down Expand Up @@ -58,7 +57,7 @@ class Runner {
* @private
*/
_pushDefaultGroup () {
this._testGroups.push(new Group('default', this._bail, true))
this._testGroups.push(new Group('default', true))
}

/**
Expand Down Expand Up @@ -145,13 +144,12 @@ class Runner {
* Wraps the group.run as a middleware function.
*
* @param {Function}
* @param {Function}
* @return {Promise}
*
* @private
*/
_wrapFn (fn, next) {
return new Promise((resolve, reject) => fn.run().then(next).then(resolve).catch(reject))
_wrapFn (fn) {
return new Promise((resolve, reject) => fn.run().then(resolve).catch(reject))
}

/**
Expand Down Expand Up @@ -198,25 +196,14 @@ class Runner {
return this._addTest(title, callback, false, true)
}

/**
* Toggle the bail status of the runner. Also
* this needs to be done before calling
* the run method.
*
* @param {Boolean} state
*/
bail (state) {
this._bail = !!state
}

/**
* Add a new group to have nested tests.
* @param {String} title
* @param {Function} callback
* @return {Object}
*/
group (title, callback) {
const group = new Group(title, this._bail)
const group = new Group(title)
this._testGroups.push(group)
callback(group)

Expand All @@ -238,7 +225,7 @@ class Runner {
}

return new Promise((resolve, reject) => {
const middleware = new Middleware(this, this._bail, this._wrapFn)
const middleware = new Middleware(this, this._wrapFn)
this._start()
middleware.compose(this._testGroups)()
.then(() => {
Expand All @@ -255,13 +242,24 @@ class Runner {
})
}

/**
* Toggle the bail status of the runner. Also
* this needs to be done before calling
* the run method.
*
* @param {Boolean} state
*/
bail (state) {
$.bail = state
}

/**
* Sets global timeout to be used for all tests.
*
* @param {Number} time
*/
timeout (time) {
$.setTimeout(time)
$.timeout = time
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Test.js
Expand Up @@ -23,7 +23,7 @@ class Test {
this._title = title
this._skip = !!skip
this._todo = typeof (callback) !== 'function'
this._timeout = $.getTimeout()
this._timeout = $.timeout
this._callback = callback
this._regression = !!expectedToFail
this._regressionMessage = null
Expand Down
5 changes: 3 additions & 2 deletions test/group.spec.js
Expand Up @@ -274,7 +274,8 @@ test('run all tests even when a hook fails', function (assert) {

test('stop after first error when bail is true', function (assert) {
assert.plan(3)
const group = new Group('A', true)
const group = new Group('A')
group.middleware._bail = true
const testsStack = []

group.before(function (done) {})
Expand Down Expand Up @@ -313,7 +314,7 @@ test('stop after first error when bail is true', function (assert) {

test('do not emit events for a root level group', function (assert) {
assert.plan(1)
const group = new Group('Default group', false, true)
const group = new Group('Default group', true)
const groupEvents = []

emitter.on('group:start', function () {
Expand Down

0 comments on commit f537cf5

Please sign in to comment.