Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Tape is a minimalist JavaScript testing framework for Node.js. It provides a simple and straightforward way to write tests for your code, focusing on simplicity and ease of use.
Basic Test
This feature allows you to write basic tests. The example demonstrates a simple test that checks if the sum of 1 and 1 equals 2.
const test = require('tape');
test('Basic test', function (t) {
t.plan(1);
t.equal(1 + 1, 2, '1 + 1 should equal 2');
});
Asynchronous Test
This feature allows you to write asynchronous tests. The example demonstrates a test that performs an assertion after a 1-second delay.
const test = require('tape');
test('Asynchronous test', function (t) {
t.plan(1);
setTimeout(function () {
t.equal(1 + 1, 2, '1 + 1 should equal 2');
}, 1000);
});
Nested Tests
This feature allows you to write nested tests. The example demonstrates a parent test containing a child test.
const test = require('tape');
test('Parent test', function (t) {
t.test('Child test', function (st) {
st.plan(1);
st.equal(1 + 1, 2, '1 + 1 should equal 2');
});
t.end();
});
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. It provides a variety of interfaces (e.g., BDD, TDD) and supports a wide range of reporters. Compared to Tape, Mocha is more feature-rich and flexible but also more complex.
Jest is a delightful JavaScript testing framework with a focus on simplicity. It works out of the box for most JavaScript projects and includes features like snapshot testing and a built-in test runner. Compared to Tape, Jest is more opinionated and comes with more built-in features, making it easier to set up but less minimalistic.
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation. It runs tests concurrently, which can lead to faster test execution. Compared to Tape, AVA is more modern and provides better support for ES6/ES7 features, but it may be less familiar to those used to traditional test runners.
tap-producing test harness for node and browsers
var test = require('tape');
test('timing test', function (t) {
t.plan(2);
t.equal(typeof Date.now, 'function');
var start = Date.now();
setTimeout(function () {
t.equal(Date.now() - start, 100);
}, 100);
});
test('test using promises', async function (t) {
const result = await someAsyncThing();
t.ok(result);
});
$ node example/timing.js
TAP version 13
# timing test
ok 1 should be strictly equal
not ok 2 should be strictly equal
---
operator: equal
expected: 100
actual: 107
...
1..2
# tests 2
# pass 1
# fail 1
You always need to require('tape')
in test files. You can run the tests by
usual node means (require('test-file.js')
or node test-file.js
). You can
also run tests using the tape
binary to utilize globbing, on Windows for
example:
$ tape tests/**/*.js
tape
's arguments are passed to the
glob
module. If you want glob
to
perform the expansion on a system where the shell performs such expansion, quote
the arguments as necessary:
$ tape 'tests/**/*.js'
$ tape "tests/**/*.js"
Additionally, it is possible to make tape
load one or more modules before running any tests, by using the -r
or --require
flag. Here's an example that loads babel-register before running any tests, to allow for JIT compilation:
$ tape -r babel-register tests/**/*.js
Depending on the module you're loading, you may be able to parameterize it using environment variables or auxiliary files. Babel, for instance, will load options from .babelrc
at runtime.
The -r
flag behaves exactly like node's require
, and uses the same module resolution algorithm. This means that if you need to load local modules, you have to prepend their path with ./
or ../
accordingly.
For example:
$ tape -r ./my/local/module tests/**/*.js
Please note that all modules loaded using the -r
flag will run before any tests, regardless of when they are specified. For example, tape -r a b -r c
will actually load a
and c
before loading b
, since they are flagged as required modules.
tape
maintains a fairly minimal core. Additional features are usually added by using another module alongside tape
.
The default TAP output is good for machines and humans that are robots.
If you want a more colorful / pretty output there are lots of modules on npm that will output something pretty if you pipe TAP into them:
To use them, try node test/index.js | tap-spec
or pipe it into one
of the modules of your choice!
By default, uncaught exceptions in your tests will not be intercepted, and will cause tape
to crash. If you find this behavior undesirable, use tape-catch
to report any exceptions as TAP errors.
The assertion methods in tape
are heavily influenced or copied from the methods
in node-tap.
var test = require('tape')
Create a new test with an optional name
string and optional opts
object.
cb(t)
fires with the new test object t
once all preceding tests have
finished. Tests execute serially.
Available opts
options are:
NODE_TAPE_OBJECT_PRINT_DEPTH
can set the desired default depth for all tests; locally-set values will take precedence.If you forget to t.plan()
out how many assertions you are going to run and you
don't call t.end()
explicitly, your test will hang.
Generate a new test that will be skipped over.
The onFinish hook will get invoked when ALL tape
tests have finished
right before tape
is about to print the test summary.
The onFailure hook will get invoked whenever any tape
tests has failed.
Declare that n
assertions should be run. t.end()
will be called
automatically after the n
th assertion. If there are any more assertions after
the n
th, or after t.end()
is called, they will generate errors.
Declare the end of a test explicitly. If err
is passed in t.end
will assert
that it is falsey.
Generate a failing assertion with a message msg
.
Generate a passing assertion with a message msg
.
Automatically timeout the test after X ms.
Generate an assertion that will be skipped over.
Assert that value
is truthy with an optional description of the assertion msg
.
Aliases: t.true()
, t.assert()
Assert that value
is falsy with an optional description of the assertion msg
.
Aliases: t.false()
, t.notok()
Assert that err
is falsy. If err
is non-falsy, use its err.message
as the
description message.
Aliases: t.ifError()
, t.ifErr()
, t.iferror()
Assert that Object.is(actual, expected)
with an optional description of the assertion msg
.
Aliases: t.equals()
, t.isEqual()
, t.strictEqual()
, t.strictEquals()
, t.is()
Assert that !Object.is(actual, expected)
with an optional description of the assertion msg
.
Aliases: t.notEquals()
, t.isNotEqual()
, t.doesNotEqual()
, t.isInequal()
, t.notStrictEqual()
, t.notStrictEquals()
, t.isNot()
, t.not()
Assert that actual == expected
with an optional description of the assertion msg
.
Aliases: t.looseEquals()
Assert that actual != expected
with an optional description of the assertion msg
.
Aliases: t.notLooseEquals()
Assert that actual
and expected
have the same structure and nested values using
node's deepEqual() algorithm
with strict comparisons (===
) on leaf nodes and an optional description of the assertion msg
.
Aliases: t.deepEquals()
, t.isEquivalent()
, t.same()
Assert that actual
and expected
do not have the same structure and nested values using
node's deepEqual() algorithm
with strict comparisons (===
) on leaf nodes and an optional description of the assertion msg
.
Aliases: t.notDeepEquals
, t.notEquivalent()
, t.notDeeply()
, t.notSame()
,
t.isNotDeepEqual()
, t.isNotDeeply()
, t.isNotEquivalent()
,
t.isInequivalent()
Assert that actual
and expected
have the same structure and nested values using
node's deepEqual() algorithm
with loose comparisons (==
) on leaf nodes and an optional description of the assertion msg
.
Assert that actual
and expected
do not have the same structure and nested values using
node's deepEqual() algorithm
with loose comparisons (==
) on leaf nodes and an optional description of the assertion msg
.
Aliases: t.notLooseEqual()
, t.notLooseEquals()
Assert that the function call fn()
throws an exception. expected
, 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
). msg
is an optional description of the assertion.
Assert that the function call fn()
does not throw an exception. expected
, if present, limits what should not be thrown. For example, set expected
to /user/
to fail the test only if the string representation of the exception contains the word user
. Any other exception would pass the test. If expected
is omitted, any exception will fail the test. msg
is an optional description of the assertion.
Create a subtest with a new test handle st
from cb(st)
inside the current
test t
. cb(st)
will only fire when t
finishes. Additional tests queued up
after t
will not be run until all subtests finish.
You may pass the same options that test()
accepts.
Print a message without breaking the tap output. (Useful when using e.g. tap-colorize
where output is buffered & console.log
will print in incorrect order vis-a-vis tap output.)
Assert that string
matches the RegExp regexp
. Will throw (not just fail) when the first two arguments are the wrong type.
Assert that string
does not match the RegExp regexp
. Will throw (not just fail) when the first two arguments are the wrong type.
Create a new test harness instance, which is a function like test()
, but with
a new pending stack and test state.
By default the TAP output goes to console.log()
. You can pipe the output to
someplace else if you htest.createStream().pipe()
to a destination stream on
the first tick.
Like test([name], [opts], cb)
except if you use .only
this is the only test case
that will run for the entire process, all other test cases using tape
will
be ignored.
Create a stream of output, bypassing the default output stream that writes
messages to console.log()
. By default stream
will be a text stream of TAP
output, but you can get an object stream instead by setting opts.objectMode
to
true
.
You can create your own custom test reporter using this createStream()
api:
var test = require('tape');
var path = require('path');
test.createStream().pipe(process.stdout);
process.argv.slice(2).forEach(function (file) {
require(path.resolve(file));
});
You could substitute process.stdout
for whatever other output stream you want,
like a network connection or a file.
Pass in test files to run as arguments:
$ node tap.js test/x.js test/y.js
TAP version 13
# (anonymous)
not ok 1 should be strictly equal
---
operator: equal
expected: "boop"
actual: "beep"
...
# (anonymous)
ok 2 should be strictly equal
ok 3 (unnamed assert)
# wheee
ok 4 (unnamed assert)
1..4
# tests 4
# pass 3
# fail 1
Here's how you can render an object stream instead of TAP:
var test = require('tape');
var path = require('path');
test.createStream({ objectMode: true }).on('data', function (row) {
console.log(JSON.stringify(row))
});
process.argv.slice(2).forEach(function (file) {
require(path.resolve(file));
});
The output for this runner is:
$ node object.js test/x.js test/y.js
{"type":"test","name":"(anonymous)","id":0}
{"id":0,"ok":false,"name":"should be strictly equal","operator":"equal","actual":"beep","expected":"boop","error":{},"test":0,"type":"assert"}
{"type":"end","test":0}
{"type":"test","name":"(anonymous)","id":1}
{"id":0,"ok":true,"name":"should be strictly equal","operator":"equal","actual":2,"expected":2,"test":1,"type":"assert"}
{"id":1,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":1,"type":"assert"}
{"type":"end","test":1}
{"type":"test","name":"wheee","id":2}
{"id":0,"ok":true,"name":"(unnamed assert)","operator":"ok","actual":true,"expected":true,"test":2,"type":"assert"}
{"type":"end","test":2}
With npm do:
npm install tape --save-dev
MIT
FAQs
tap-producing test harness for node and browsers
We found that tape demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.