npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tape-around

v2.2.0

Published

Add before and after hooks to tape tests

Downloads

42

Readme

tape-around

Add before and after hooks to tape tests

Status

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 = require('tape')
var around = require('tape-around')

// Define a test block using around().
testBlock = around(test)
  .before(function (t) {
    t.pass('before hook called')
    t.next()
  })
  .after(function (t) {
    t.pass('after hook called')
    t.end()
  })
})

testBlock('my test', function (t) {
  t.equal(25 * 4, 100)
  t.end()
})

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 = require('tape')

testBlock = around()
  .before(function (t) {
    var user = User.create({ name: 'John' })
    t.next(user)
  })
  .after(function (t, user) {
    user.destroy()
    t.end()
  })

testBlock('user test', function (t, user) {
  t.equal(user.name, 'John', 'value is passed from the block')
  t.end()
})

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 = around(test)
  .before((t) => { t.next(100) })

var two = around(one)
  .before((t, a) => { t.next(a, 200) })

two('chaining', function (t, a, b) {
  t.equal(a, 100)
  t.equal(b, 200)
  t.end()
})

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 be require('tape'), or another around() 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 = around(test)
  .before(...)
  .after(...)

aTest('using around()', function (t) {
  t.pass('this works')
  t.end()
})

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. The params arguments are parameters taken from whatever was passed to t.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 = around(test)
  .before(function (t) {
    t.pass('before hooks')
    t.end()
  })
  .after(function (t) {
    t.pass('after hooks')
    t.end()
  })
})

testBlock('my test', function (t) {
  t.equal(25 * 4, 100)
  t.end()
})

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 = around(test)
  .before((t)       => { t.next(100) })
  .before((t, a)    => { t.next(a, 200) })
  .before((t, a, b) => { t.next(a, b, 300) })

addTest('using t.next', function (t, a, b, c) {
  t.equal(a, 100)
  t.equal(b, 200)
  t.equal(c, 300)
  t.end()
})

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 = around(test)
  .before((t) => { t.nextAdd(100) })
  .before((t) => { t.nextAdd(200) })
  .before((t) => { t.nextAdd(300) })

addTest('using nextAdd', function (t, a, b, c) {
  t.equal(a, 100)
  t.equal(b, 200)
  t.equal(c, 300)
  t.end()
})

Sinon.js example

You can create sinon sandboxes to automatically clear out sinon stubs.

var sandbox = around(test)
  .before((t) => {
    var sandbox = require('sinon').sandbox.create()
    t.next(sandbox)
  })
  .after((t, sandbox) => {
    sandbox.restore()
    t.end()
  })

sandbox('my test', (t, sinon) => {
  sinon.spyOn($, 'ajax')
  // ...the spies will be cleaned up on exit automatically
  t.end()
})

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