Awesome
tape-promise
Promise and ES2016 (ES7) async/await
support for Tape.
This module assumes that you're familiar with at least the concepts of Promises
and if you want to use async/await
, then you must be familiar with Babel.
Install
npm i --save-dev tape-promise
Usage
Make sure that you have tape
installed:
npm i --save-dev tape
Unlike blue-tape
, tape-promise
is
just a decorator (not an ES7 decorator). That is, it does NOT depend upon tape
and requires that you pass it in.
ES5
var tape = require('tape')
var _test = require('tape-promise').default // <---- notice 'default'
var test = _test(tape) // decorate tape
ES6 (ES2015)
import tape from 'tape'
import _test from 'tape-promise'
const test = _test(tape) // decorate tape
or, for convenience...
import test from 'tape-promise/tape'
but you must explicitly have tape
as a dependency.
Example (promises)
Just return the promise.
// example function that returns a Promise
function delay (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve()
}, time)
})
}
test('ensure promises works', function (t) {
return delay(100).then(function () {
t.true(true)
})
})
Example (async/await)
Async/await functions just transform to Promises.
Don't forget to put the async
keyword on your function.
// example function that returns a Promise
// it could also be an async function
function delay (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve()
}, time)
})
}
// NOTICE 'async'?
test('ensure async works', async function (t) {
await delay(100)
t.true(true)
t.end() // not really necessary
})
Example (normal tape tests)
Of course you can write normal tape tests as you would.
test('ensure that regular test functions still work', function (t) {
t.true(true)
t.end()
})
New Assertions
t.rejects(promise, expected, msg)
Assert that the promise will reject.
If promise
is a function, then it is called, and the returned promise is used.
expected
is the error that should be rejected with, if present, must be a RegExp
or Function
. The RegExp
matches the string representation of the exception, as generated by err.toString()
. The Function
is the exception thrown (e.g. Error
). If the promise rejects, but its error doesn't match expected
, then the assertion fails.
msg
is an optional description of the assertion.
This function returns a promise that resolves when the assertion is complete (after the promise is settled). The test will not wait for the assertion automatically, so it needs to be await
ed.
t.doesNotReject(promise, expected, msg)
Assert that the promise will resolve.
If promise
is a function, then it is called, and the returned promise is used.
expected
is the error that the promise shouldn't reject with, if present, it must be a RegExp
or Function
. The RegExp
matches the string representation of the exception, as generated by err.toString()
. The Function
is the exception thrown (e.g. Error
). If the promise rejects, but its error doesn't match expected
, then the assertion passes.
msg
is an optional description of the assertion.
This function returns a promise that resolves when the assertion is complete (after the promise is settled). The test will not wait for the assertion automatically, so it needs to be await
ed.
example
test('reject and doesNotReject example', async (t) => {
await t.rejects(asyncFunction)
await t.rejects(asyncFunction())
await t.doesNotReject(Promise.resolve())
})
License
Licensed under MIT
Copyright (c) JP Richardson