Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

japa

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

japa - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

api.js

12

CHANGELOG.md

@@ -0,1 +1,13 @@

<a name="1.0.4"></a>
## [1.0.4](https://github.com/thetutlage/japa/compare/v1.0.3...v1.0.4) (2017-08-17)
### Features
* out of the box support for embed api ([6a9ed2e](https://github.com/thetutlage/japa/commit/6a9ed2e))
* **assertion:** expose api to extend chai ([66e09a7](https://github.com/thetutlage/japa/commit/66e09a7))
* **test:** add support for custom arg to test callback ([f92c8cb](https://github.com/thetutlage/japa/commit/f92c8cb))
<a name="1.0.3"></a>

@@ -2,0 +14,0 @@ ## [1.0.3](https://github.com/thetutlage/japa/compare/v1.0.2...v1.0.3) (2017-04-01)

20

index.js

@@ -12,3 +12,3 @@ 'use strict'

const runner = new (require('./src/Runner'))()
const slimRunner = new (require('./src/SlimRunner'))(require('./lib/props'))

@@ -18,3 +18,3 @@ const nextTick = typeof (setImmediate) !== 'undefined' ? setImmediate : process.nextTick

nextTick(function () {
runner
slimRunner
.run()

@@ -29,9 +29,9 @@ .then(() => {

exports = module.exports = runner.test.bind(runner)
exports.skip = runner.skip.bind(runner)
exports.failing = runner.failing.bind(runner)
exports.group = runner.group.bind(runner)
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 = module.exports = slimRunner.test.bind(slimRunner)
exports.skip = slimRunner.skip.bind(slimRunner)
exports.failing = slimRunner.failing.bind(slimRunner)
exports.group = slimRunner.group.bind(slimRunner)
exports.timeout = slimRunner.timeout.bind(slimRunner)
exports.use = slimRunner.use.bind(slimRunner)
exports.bail = slimRunner.bail.bind(slimRunner)
exports.grep = slimRunner.grep.bind(slimRunner)

@@ -12,23 +12,4 @@ 'use strict'

let DEFAULT_TIMEOUT = 2000
let BAIL_TESTS = false
const util = exports = module.exports = {}
const util = exports = module.exports = {
get timeout () {
return DEFAULT_TIMEOUT
},
set timeout (timeout) {
DEFAULT_TIMEOUT = timeout
},
get bail () {
return BAIL_TESTS
},
set bail (state) {
BAIL_TESTS = !!state
}
}
/**

@@ -50,28 +31,19 @@ * The `s` verb to be used by checking the

/**
* List of events emitted by Japa
*
* @method getEventsList
*
* @return {Object}
*/
util.getEventsList = function () {
return {
GROUP_START: 'group:start',
GROUP_END: 'group:end',
TEST_START: 'test:start',
TEST_END: 'test:end',
GROUP_HOOK_START: {
before: 'hook:before:start',
beforeEach: 'hook:beforeEach:start',
after: 'hook:after:start',
afterEach: 'hook:afterEach:start'
},
GROUP_HOOK_END: {
before: 'hook:before:end',
beforeEach: 'hook:beforeEach:end',
after: 'hook:after:end',
afterEach: 'hook:afterEach:end'
}
util.eventsList = Object.freeze({
GROUP_START: 'group:start',
GROUP_END: 'group:end',
TEST_START: 'test:start',
TEST_END: 'test:end',
GROUP_HOOK_START: {
before: 'hook:before:start',
beforeEach: 'hook:beforeEach:start',
after: 'hook:after:start',
afterEach: 'hook:afterEach:start'
},
GROUP_HOOK_END: {
before: 'hook:before:end',
beforeEach: 'hook:beforeEach:end',
after: 'hook:after:end',
afterEach: 'hook:afterEach:end'
}
}
})
{
"name": "japa",
"version": "1.0.3",
"version": "1.0.4",
"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)",

@@ -22,2 +22,3 @@ "main": "index.js",

"istanbul": "^0.4.5",
"lodash": "^4.17.4",
"require-all": "^2.1.0",

@@ -24,0 +25,0 @@ "semver": "^5.3.0",

@@ -14,5 +14,6 @@ 'use strict'

const assert = chai.assert
const $ = require('../lib/util')
chai.config.showDiff = true
const util = require('../lib/util')
class Assertion {

@@ -54,2 +55,4 @@ constructor () {

*
* @method plan
*
* @param {Function} num

@@ -69,2 +72,4 @@ */

*
* @method evaluate
*
* @throws {Error} If planned assertions mis-match the ran assertions

@@ -76,6 +81,25 @@ */

}
assert.fail(this._ran, this._planned, `planned for ${this._planned} assertion${$.verb(this._planned)} but ran ${this._ran}`)
assert.fail(
this._ran,
this._planned,
`planned for ${this._planned} assertion${util.verb(this._planned)} but ran ${this._ran}`
)
}
/**
* Apply plugin on the chai instance
*
* @method use
*
* @param {Function} chaiPlugin
*
* @chainable
*/
static use (chaiPlugin) {
chai.use(chaiPlugin)
return this
}
}
module.exports = Assertion

@@ -32,4 +32,8 @@ 'use strict'

*
* @method _validateFn
*
* @param {Function} fn
* @throws {Error} if fn is not a function
*
* @private
*/

@@ -45,4 +49,8 @@ _validateFn (fn) {

*
* @method _validateTimeout
*
* @param {Number} timeout
* @throws {Error} if timeout is not a number
*
* @private
*/

@@ -58,2 +66,6 @@ _validateTimeout (timeout) {

* any running timer.
*
* @method _clearTimer
*
* @private
*/

@@ -69,4 +81,8 @@ _clearTimer () {

*
* @method _callFn
*
* @param {Function} [done]
* @return {Function}
*
* @private
*/

@@ -80,4 +96,8 @@ _callFn (done) {

*
* @method _internalResolve
*
* @param {Function} resolve
* @return {Function}
*
* @private
*/

@@ -95,4 +115,8 @@ _internalResolve (resolve) {

*
* @method _internalReject
*
* @param {Function} reject
* @return {Function}
*
* @private
*/

@@ -110,2 +134,4 @@ _internalReject (reject) {

*
* @method args
*
* @param {Array} args

@@ -123,2 +149,4 @@ * @chainable

*
* @method run
*
* @return {Promise}

@@ -125,0 +153,0 @@ */

@@ -14,11 +14,19 @@ 'use strict'

const Middleware = require('./Middleware')
const $ = require('../lib/util')
const emitter = require('../lib/emitter')
const util = require('../lib/util')
const eventsList = $.getEventsList()
class Group {
constructor (title, isRoot) {
constructor (title, globals, isRoot) {
this._title = title
this._globals = globals
/**
* A root group where tests are not nested intentionally
* by the user but group was created by the runner for
* the sake of simplicity.
*/
this._isRoot = !!isRoot
/**
* Hooks to be executed
*/
this._hooks = {

@@ -30,4 +38,5 @@ beforeEach: [],

}
this._tests = []
this.middleware = new Middleware(this, this._wrapFn)
this.middleware = new Middleware(this, this._wrapFn, this._globals.bail)
}

@@ -37,2 +46,6 @@

* Emits the start event for the group
*
* @method _start
*
* @private
*/

@@ -44,3 +57,3 @@ _start () {

emitter.emit(eventsList['GROUP_START'], {
this._globals.emitter.emit(util.eventsList['GROUP_START'], {
title: this._title,

@@ -52,5 +65,9 @@ status: 'pending'

/**
* Emits the end event for the group
* Emits the end event for the group.
*
* @method _end
*
* @param {Object|Null} error
*
* @private
*/

@@ -62,3 +79,3 @@ _end (error) {

emitter.emit(eventsList['GROUP_END'], {
this._globals.emitter.emit(util.eventsList['GROUP_END'], {
title: this._title,

@@ -88,2 +105,4 @@ status: error ? 'failed' : 'passed',

*
* @method _composeStack
*
* @return {Array}

@@ -130,8 +149,12 @@ *

/**
* Add a new closure to the before hook
* Add a new closure to the before hooks stack.
*
* @method before
*
* @param {Function} callback
*
* @return {Hook} Instance of hook
*/
before (callback) {
const hook = new Hook(this._title, 'before', callback)
const hook = new Hook(this._title, 'before', callback, this._globals)
this._hooks.before.push(hook)

@@ -142,8 +165,12 @@ return hook

/**
* Add a new closure to the beforeEach hook
* Add a new closure to the beforeEach hooks stack.
*
* @method beforeEach
*
* @param {Function} callback
*
* @return {Hook} Instance of hook
*/
beforeEach (callback) {
const hook = new Hook(this._title, 'beforeEach', callback)
const hook = new Hook(this._title, 'beforeEach', callback, this._globals)
this._hooks.beforeEach.push(hook)

@@ -154,8 +181,12 @@ return hook

/**
* Add a new closure to the after hook
* Add a new closure to the after hooks stack.
*
* @method after
*
* @param {Function} callback
*
* @return {Hook} Instance of hook
*/
after (callback) {
const hook = new Hook(this._title, 'after', callback)
const hook = new Hook(this._title, 'after', callback, this._globals)
this._hooks.after.push(hook)

@@ -166,8 +197,12 @@ return hook

/**
* Add a new closure to the afterEach hook
* Add a new closure to the afterEach hooks stack.
*
* @method afterEach
*
* @param {Function} callback
*
* @return {Hook} Instance of hook
*/
afterEach (callback) {
const hook = new Hook(this._title, 'afterEach', callback)
const hook = new Hook(this._title, 'afterEach', callback, this._globals)
this._hooks.afterEach.push(hook)

@@ -181,2 +216,4 @@ return hook

*
* @method addTest
*
* @param {Object} test callerInstance

@@ -189,4 +226,7 @@ */

/**
* Run tests with their hooks
* Run tests with their hooks. All errors will be stacked
* and returned as nested arrays.
*
* @method run
*
* @return {Promise}

@@ -193,0 +233,0 @@ */

@@ -13,12 +13,11 @@ 'use strict'

const Callable = require('./Callable')
const $ = require('../lib/util')
const emitter = require('../lib/emitter')
const eventsList = $.getEventsList()
const util = require('../lib/util')
class Hook {
constructor (groupTitle, hookFor, callback) {
constructor (groupTitle, hookFor, callback, globals) {
this._title = groupTitle
this._hookFor = hookFor
this._callback = callback
this._timeout = $.timeout
this._globals = globals
this._timeout = globals.timeout
}

@@ -30,4 +29,8 @@

*
* @method _parseError
*
* @param {String|Object|Null} error
* @return {Object}
*
* @private
*/

@@ -60,7 +63,11 @@ _parseError (error) {

*
* @method _end
*
* @param {String|Object|Null} error
* @param {Number} start
*
* @private
*/
_end (error, start) {
emitter.emit(eventsList['GROUP_HOOK_END'][this._hookFor], {
this._globals.emitter.emit(util.eventsList['GROUP_HOOK_END'][this._hookFor], {
title: this._title,

@@ -75,5 +82,9 @@ status: error ? 'failed' : 'passed',

* Emits the {start} event for a given hook.
*
* @method _start
*
* @private
*/
_start () {
emitter.emit(eventsList['GROUP_HOOK_START'][this._hookFor], {
this._globals.emitter.emit(util.eventsList['GROUP_HOOK_START'][this._hookFor], {
title: this._title,

@@ -87,2 +98,4 @@ status: 'pending'

*
* @method run
*
* @return {Promise}

@@ -112,2 +125,4 @@ */

*
* @method timeout
*
* @param {Number} time

@@ -114,0 +129,0 @@ */

@@ -12,10 +12,8 @@ 'use strict'

const $ = require('../lib/util')
class Middleware {
constructor (context, fnWrapper) {
constructor (context, fnWrapper, bail) {
this._context = context
this._fnWrapper = fnWrapper
this._stack = []
this._bail = bail
this.errorsStack = []

@@ -26,4 +24,6 @@ }

* Rejects the error by making sure bail is true.
* Otherwise stacks error for later rejection
* Otherwise stacks error for later rejection.
*
* @method _internalRejection
*
* @param {Number}

@@ -33,5 +33,7 @@ * @param {Object}

* @param {Function}
*
* @private
*/
_internalRejection (index, resolve, reject, error) {
if ($.bail) {
if (this._bail) {
reject(error)

@@ -48,4 +50,8 @@ return

*
* @method _dispatch
*
* @param {Number}
* @return {Promise}
*
* @private
*/

@@ -67,19 +73,7 @@ _dispatch (index) {

/**
* Returns the first promise from the stack
* or a blank promise if stack is empty.
*
* @param {Function}
* @return {Promise}
*/
_middleware (next) {
if (this._stack.length === 0) {
return Promise.resolve()
}
return this._dispatch(0)
}
/**
* Composes a middleware layer to be executed
* in sequence.
*
* @method stack
*
* @param {Array} stack

@@ -90,3 +84,8 @@ * @return {Fuction}

this._stack = stack
return this._middleware.bind(this)
return function () {
if (this._stack.length === 0) {
return Promise.resolve()
}
return this._dispatch(0)
}.bind(this)
}

@@ -93,0 +92,0 @@ }

@@ -13,14 +13,8 @@ 'use strict'

const Middleware = require('./Middleware')
const Test = require('./Test')
const Group = require('./Group')
const emitter = require('../lib/emitter')
const $ = require('../lib/util')
class Runner {
constructor () {
this._testReporter = require('../src/Reporters/list')
this._testGroups = []
this._pushDefaultGroup()
this._grepOn = null
this.emitter = emitter // refrence to emitter for listening events
constructor (stack, reporter, globals) {
this._reporter = reporter
this._stack = stack
this._globals = globals
}

@@ -31,2 +25,4 @@

*
* @method _end
*
* @param {Object|Null}

@@ -37,3 +33,3 @@ *

_end (error) {
emitter.emit('end', {
this._globals.emitter.emit('end', {
status: error ? 'failed' : 'passed',

@@ -48,80 +44,11 @@ error: error || null

*
* @method _start
*
* @private
*/
_start () {
emitter.emit('start')
this._globals.emitter.emit('start')
}
/**
* Pushes a default group to the tests groups.
* This is required to make sure all tests
* outside of explicit groups are executed
* in order.
*
* @private
*/
_pushDefaultGroup () {
this._testGroups.push(new Group('default', true))
}
/**
* Returns the latest group from the groups
* stack.
*
* @return {Object}
*
* @private
*/
_getLatestGroup () {
return this._testGroups[this._testGroups.length - 1]
}
/**
* Returns whether string contains the substring
* or not.
*
* @method _passesGrep
*
* @param {String} title
*
* @return {Boolean}
*
* @private
*/
_passesGrep (title) {
if (!this._grepOn) {
return true
}
return title.includes(this._grepOn)
}
/**
* Adds a new test to the latest group
*
* @param {String} title
* @param {Function} callback
* @param {Boolean} skip
*
* @private
*/
_addTest (title, callback, skip, failing) {
const test = new Test(title, callback, skip, failing)
/**
* Grep on the test title and make sure it passes the grep
* pattern if defined. If not we do not push it to the
* group but still instantiate the test since the
* end user will be chaining methods on it and
* it should not throw exception.
*
* In short: We create the test but do not execute it
*/
if (this._passesGrep(title)) {
const lastGroup = this._getLatestGroup()
lastGroup.addTest(test)
}
return test
}
/**
* Flattens the array stack by forming

@@ -160,77 +87,21 @@ * a new array.

/**
* Returns the list of groups
*
* @return {Array}
*
* @private
*/
getGroups () {
return this._testGroups
}
/**
* Adds a new test to the relevant test group
* @param {String} title
* @param {Function} callback
* @return {Object}
*/
test (title, callback) {
return this._addTest(title, callback, false, false)
}
/**
* Add a skipable test.
*
* @param {String} title
* @param {Function} callback
* @return {Object}
*/
skip (title, callback) {
return this._addTest(title, callback, true, false)
}
/**
* Create a test that would fail but will be marked
* as passed.
*
* @param {String}
* @param {Function}
* @return {Object}
*/
failing (title, callback) {
return this._addTest(title, callback, false, true)
}
/**
* 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._testGroups.push(group)
callback(group)
// Push default group after each test
this._pushDefaultGroup()
}
/**
* The lord who runs the entire tests stack in sequence
* by respecting the bail feature and also makes sure
* to pass the emitter instance to the reporter. So
* to pass the props.emitter instance to the reporter. So
* that reporter can generate nice output.
*
* @method run
*
* @return {Promise}
*/
run () {
if (typeof (this._testReporter) === 'function') {
this._testReporter(emitter)
if (typeof (this._reporter) === 'function') {
this._reporter(this._globals.emitter)
}
return new Promise((resolve, reject) => {
const middleware = new Middleware(this, this._wrapFn)
const middleware = new Middleware(this, this._wrapFn, this._globals.bail)
this._start()
middleware.compose(this._testGroups)()
middleware.compose(this._stack)()
.then(() => {

@@ -249,44 +120,4 @@ if (middleware.errorsStack.length) {

}
/**
* 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) {
$.timeout = time
}
/**
* Use a third part reporter.
*
* @param {Function} reporter
*/
use (reporter) {
this._testReporter = reporter
}
/**
* Grep on test title to run only specific tests
*
* @method grep
*
* @param {String} pattern
*/
grep (pattern) {
this._grepOn = pattern
}
}
module.exports = Runner

@@ -13,20 +13,19 @@ 'use strict'

const retry = require('retry')
const Callable = require('./Callable')
const Assertion = require('./Assertion')
const $ = require('../lib/util')
const emitter = require('../lib/emitter')
const util = require('../lib/util')
const eventsList = $.getEventsList()
class Test {
constructor (title, callback, skip, expectedToFail) {
constructor (title, callback, globals, skip, regression) {
this._title = title
this._skip = !!skip
this._todo = typeof (callback) !== 'function'
this._timeout = $.timeout
this._callback = callback
this._regression = !!expectedToFail
this._regression = !!regression
this._regressionMessage = null
this._globals = globals
this._timeout = globals.timeout
this._retry = 0
this._resolveArgFn = null
}

@@ -49,8 +48,31 @@

/**
* Returns the 1st argument to be passed to the
* each test.
*
* @method _getArg
*
* @param {String} assert
*
* @return {Object}
*
* @private
*/
_getArg (assert) {
if (typeof (this._resolveArgFn) === 'function') {
return this._resolveArgFn(assert)
}
return assert
}
/**
* Returns the test status
*
* @method _getPostRunStatus
*
* @param {String|Object} error
* @return {String}
*
* @private
*/
_getTestStatus (error) {
_getPostRunStatus (error) {
if (error) {

@@ -75,4 +97,8 @@ return 'failed'

*
* @method _parseError
*
* @param {String|Object|Null} error
* @return {Object}
*
* @private
*/

@@ -105,4 +131,8 @@ _parseError (error) {

*
* @method _end
*
* @param {String|Object|Null} error
* @param {Number} start
*
* @private
*/

@@ -117,3 +147,3 @@ _end (error, start) {

eventBody.duration = new Date() - start
eventBody.status = this._getTestStatus(error)
eventBody.status = this._getPostRunStatus(error)

@@ -127,3 +157,3 @@ /**

emitter.emit(eventsList['TEST_END'], eventBody)
this._globals.emitter.emit(util.eventsList['TEST_END'], eventBody)
}

@@ -133,5 +163,9 @@

* Emits the {start} event for a given test.
*
* @method _start
*
* @private
*/
_start () {
emitter.emit(eventsList['TEST_START'], this.eventBody)
this._globals.emitter.emit(util.eventsList['TEST_START'], this.eventBody)
}

@@ -142,3 +176,7 @@

*
* @method _runTest
*
* @return {Promise}
*
* @private
*/

@@ -149,3 +187,3 @@ _runTest () {

new Callable(this._callback, this._timeout, 1)
.args([assert])
.args([this._getArg(assert)])
.run()

@@ -175,2 +213,4 @@ .then(() => assert.evaluate())

*
* @method run
*
* @return {Promise}

@@ -228,4 +268,24 @@ */

/**
* Attach a callback to customize the 1st argument
* to be passed to the test callback function.
*
* For multiple values, pass an object, which can be
* destrctured within the callback.
*
* @method resolveArg
*
* @param {Function} callback
*
* @chainable
*/
resolveArg (callback) {
this._resolveArgFn = callback
return this
}
/**
* Modify test timeout
*
* @method timeout
*
* @param {Number} time

@@ -243,2 +303,4 @@ */

*
* @method retry
*
* @param {Number} ops

@@ -245,0 +307,0 @@ */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc