tape-around
Add before and after hooks to tape tests
Usage
Call around(test)
to define a test block. The functions given to before() and after() will be executed before and after the test. The return value of this is a function that works exactly like tape's test
.
var test = var around = // Define a test block using around().testBlock = })
Passing data
You can use this to build data before your tests and clean them up after. Call t.next()
with arguments and they will be passed onto your tests as additional arguments after t
.
var test = testBlock =
Nesting
Since the return value of around(test)
works exactly like tape's test
, you can chain them to make multiple around() blocks wrap around each other.
var one = var two =
API Reference
around
around(test, [prefix])
Creates a wrapper around test
. You can also call .before()
and .after()
to add hooks to the pipeline. See t.end for an example.
Parameters:
test
Function — can either berequire('tape')
, or anotheraround()
function.prefix
String? — if given, test names will be prefixed by this name.
Returns:
- Function — the return value is a function that works exactly like tape.
var aTest =
before
around(...).before(fn)
Adds hooks to the pipeline to run before the tests. These methods are chainable, and you may call this multiple times to add more functions. See t.end for an example. Be sure to call t.end() or t.next() or t.nextAdd in each of these blocks.
When one of your before hooks fail, the test is not executed, and other before hooks will not be executed too. The after hooks, however, will continue to run.
Parameters:
fn
(Function(t, ...params)) — The function block to be invoked before (or after) the test. Theparams
arguments are parameters taken from whatever was passed tot.next()
in the previous function in the pipeline.
after
around(...).after(fn)
Adds hooks to the pipeline to run before the tests. These methods are chainable, and you may call this multiple times to add more functions. See t.end for an example. Be sure to call t.end() or t.next() or t.nextAdd in each of these blocks.
Unlike before(), after hooks will always run, even if tests and before hooks fail.
See before() for explanation on parameters.
t.end
t.end([err])
Ends the current block in the pipeline.
This is changed from tape's default t.end
so that you can invoke t.end()
in any of the blocks (before, after, or the test) to call the next function in the pipeline. In short: it doesn't end the whole test, it end the block in the pipeline. If there are no more functions in the pipeline, the test will be ended.
testBlock = })
t.next
t.next(...params)
Calls the next function in the pipeline and passes params
to the parameters.
Note that calling t.next()
with no arguments will erase the current arguments. If you wish to preserve them, use t.end()
.
Parameters:
...params
(Any) — The objects to be passed to the next function in the pipeline.
var nextTest =
t.nextAdd
t.nextAdd(...params)
Calls the next function in the pipeline and passes params
as additional parameters.
If you notice in the t.next example, the parameter a
is passed through t.next()
. This may be cumbersome once you have a lot of parameters to pass. Use t.nextAdd()
to simply append it to the already-passed parameters.
Here's the same t.next example but written with t.nextAdd
:
var addTest =
Sinon.js example
You can create sinon sandboxes to automatically clear out sinon stubs.
var sandbox =
Thanks
tape-around © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).
ricostacruz.com · GitHub @rstacruz · Twitter @rstacruz