Skip to content

Commit

Permalink
refactor(bail): manage test bail status from a module
Browse files Browse the repository at this point in the history
Passing bail status is kind of weird, since it applied on all
the tests, instead setting the bail status inside a module and
using the get and set methods to interact with it.
  • Loading branch information
thetutlage committed Apr 1, 2017
1 parent de5d1c4 commit 8054a29
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
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
9 changes: 9 additions & 0 deletions lib/util.js
Expand Up @@ -12,6 +12,7 @@
const util = exports = module.exports = {}

let DEFAULT_TIMEOUT = 2000
let BAIL_TESTS = false

util.verb = function (count) {
return count === 1 ? '' : 's'
Expand All @@ -25,6 +26,14 @@ util.getTimeout = function () {
return DEFAULT_TIMEOUT
}

util.setBail = function (state) {
BAIL_TESTS = !!state
}

util.getBail = function () {
return BAIL_TESTS
}

util.getEventsList = function () {
return {
GROUP_START: 'group:start',
Expand Down
4 changes: 2 additions & 2 deletions src/Group.js
Expand Up @@ -17,7 +17,7 @@ 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()
Expand All @@ -28,7 +28,7 @@ class Group {
after: []
}
this._tests = []
this.middleware = new Middleware(this, !!bail, this._wrapFn)
this.middleware = new Middleware(this, $.getBail(), this._wrapFn)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Middleware.js
Expand Up @@ -83,7 +83,6 @@ class Middleware {
this._stack = stack
return this._middleware.bind(this)
}

}

module.exports = Middleware
31 changes: 15 additions & 16 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 @@ -197,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 @@ -237,7 +225,7 @@ class Runner {
}

return new Promise((resolve, reject) => {
const middleware = new Middleware(this, this._bail, this._wrapFn)
const middleware = new Middleware(this, $.getBail(), this._wrapFn)
this._start()
middleware.compose(this._testGroups)()
.then(() => {
Expand All @@ -254,6 +242,17 @@ 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) {
$.setBail(state)
}

/**
* Sets global timeout to be used for all tests.
*
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 8054a29

Please sign in to comment.