tape-six
v1.0.2
Published
TAP the test harness for the modern JavaScript (ES6).
Downloads
316
Maintainers
Readme
tape-six
tape-six
is a TAP-based library for unit tests.
It is written in the modern JavaScript for the modern JavaScript and works in Node,
Deno, Bun and browsers.
Why tape-six
? It was supposed to be named tape6
but npm
does not allow names "similar"
to existing packages. Instead of eliminating name-squatting they force to use unintuitive and
unmemorable names. That's why all internal names, environment variables, and public names still use tape6
.
Rationale
Why another library? Working on projects written in modern JS (with modules) I found several problems with existing unit test libraries:
- In my opinion unit test files should be directly executable with
node
,deno
,bun
, browsers (with a trivial HTML file to load a test file) without a need for a special test runner utility, which wraps and changes my beautiful code.- Debugging my tests should be trivial. It should not be different from debugging any regular file.
- The test harness should not obfuscate code nor include hundreds of other packages.
- I want to debug my code, not dependencies I've never heard about.
- I want to see where a problem happens, not some guts of a test harness.
- Tests should work with ES modules natively.
- What if I want to debug some CommonJS code with Node? Fret not! Modules can import CommonJS files directly. But not the other way around (yet). And it helps to test how module users can use your beautiful CommonJS package.
- The DX in browsers are usually abysmal.
- Both console-based debugging and a UI to navigate results should be properly supported.
Docs
The documentation can be found in the wiki. See how it can be used in tests/.
The whole API is based on two objects: test
and Tester
.
test
test
is the entry point to the test suite:
import test from 'tape-six';
This function registers a test suite. Available options:
async test(name, options, testFn)
— registers a test suite to be executed asynchronously. The returned promise is resolved when the test suite is finished.- In most cases no need to wait for the returned promise.
- The test function has the following signature:
testFn(tester)
test.skip(name, options, testFn)
— registers a test suite to be skipped.- It is used to mark a test suite to be skipped. It will not be executed.
test.todo(name, options, testFn)
— registers a test suite that is marked as work in progress.- Tests in this suite will be executed, errors will be reported but not counted as failures.
- It is used to mark tests for incomplete features under development.
test.asPromise(name, options, testPromiseFn)
— registers a test suite to be executed asynchronously using the callback-style API to notify that the test suite is finished.- The test function has a different signature:
testPromiseFn(tester, resolve, reject)
.
- The test function has a different signature:
The arguments mentioned above are:
name
— the optional name of the test suite. If not provided, it will be set to the name of the test function or'(anonymous)'
.options
— the optional options object. Available options:skip
— iftrue
, the test suite will be skipped.todo
— iftrue
, the test suite will be marked as work in progress.name
— the optional name of the test suite. If not provided, it will be set to the name of the test function or'(anonymous)'
.- Can be overridden by the
name
argument.
- Can be overridden by the
testFn
— the optional test function to be executed.- Can be overridden by the
testFn
argument.
- Can be overridden by the
timeout
— the optional timeout in milliseconds. It is used for asynchronous tests.- If the timeout is exceeded, the test suite will be marked as failed.
- Important: JavaScript does not provide a generic way to cancel asynchronous operations.
When the timeout is exceeded,
tape6
will stop waiting for the test to finish, but it will continue running in the background. - The default: no timeout.
testFn
— the test function to be executed. It will be called with thetester
object. The result will be ignored.- This function can be an asynchronous one or return a promise.
testPromiseFn
— the test function to be executed. It will be called with thetester
object and two callbacks:resolve
andreject
modeled on the Promise API.
Given all that test
and its helpers can be called like this:
test(name, options, testFn);
test(name, testFn);
test(testFn);
test(name, options);
test(options, testFn);
test(options);
// examples:
test('foo', t => {
t.pass();
});
test('bar', async t => {
t.fail();
});
test(function baz(t) {
t.ok(1 < 2);
});
test({
name: 'qux',
todo: true,
testFn: t => {
t.ok(1 < 2);
}
});
Tester
Tester
helps to do asserts and provides an interface between a test suite and the test harness.
The following methods are available (all msg
arguments are optional):
- Asserts:
pass(msg)
— asserts that the test passed.fail(msg)
— asserts that the test failed.ok(val, msg)
— asserts thatval
is truthy.true()
— an alias ofok()
.assert()
— an alias ofok()
.
notOk(val, msg)
— asserts thatval
is falsy.false()
— an alias ofnotOk()
.notok()
— an alias ofnotOk()
.
error(err, msg)
— asserts thaterr
is falsy.ifError()
— an alias oferror()
.ifErr()
— an alias oferror()
.iferror()
— an alias oferror()
.
strictEqual(a, b, msg)
— asserts thata
andb
are strictly equal.- Strict equality is defined as
a === b
. is()
— an alias ofstrictEqual()
.equal()
— an alias ofstrictEqual()
.isEqual()
— an alias ofstrictEqual()
.equals()
— an alias ofstrictEqual()
.strictEquals()
— an alias ofstrictEqual()
.
- Strict equality is defined as
notStrictEqual(a, b, msg)
— asserts thata
andb
are not strictly equal.not()
— an alias ofnotStrictEqual()
.notEqual()
— an alias ofnotStrictEqual()
.notEquals()
— an alias ofnotStrictEqual()
.notStrictEquals()
— an alias ofnotStrictEqual()
.doesNotEqual()
— an alias ofnotStrictEqual()
.isUnequal()
— an alias ofnotStrictEqual()
.
looseEqual(a, b, msg)
— asserts thata
andb
are loosely equal.- Loose equality is defined as
a == b
. looseEquals()
— an alias oflooseEqual()
.
- Loose equality is defined as
notLooseEqual(a, b, msg)
— asserts thata
andb
are not loosely equal.notLooseEquals()
— an alias ofnotLooseEqual()
.
deepEqual(a, b, msg)
— asserts thata
andb
are deeply equal.- Individual components of
a
andb
are compared recursively using the strict equality. - See deep6's equal() for details.
same()
— an alias ofdeepEqual()
.deepEquals()
— an alias ofdeepEqual()
.isEquivalent()
— an alias ofdeepEqual()
.
- Individual components of
notDeepEqual(a, b, msg)
— asserts thata
andb
are not deeply equal.notSame()
— an alias ofnotDeepEqual()
.notDeepEquals()
— an alias ofnotDeepEqual()
.notEquivalent()
— an alias ofnotDeepEqual()
.notDeeply()
— an alias ofnotDeepEqual()
.isNotDeepEqual()
— an alias ofnotDeepEqual()
.isNotEquivalent()
— an alias ofnotDeepEqual()
.
deepLooseEqual(a, b, msg)
— asserts thata
andb
are deeply loosely equal.- Individual components of
a
andb
are compared recursively using the loose equality.
- Individual components of
notDeepLooseEqual(a, b, msg)
— asserts thata
andb
are not deeply loosely equal.throws(fn, msg)
— asserts thatfn
throws.fn
is called with no arguments in the global context.
doesNotThrow(fn, msg)
— asserts thatfn
does not throw.matchString(string, regexp, msg)
— asserts thatstring
matchesregexp
.doesNotMatchString(string, regexp, msg)
— asserts thatstring
does not matchregexp
.match(a, b, msg)
— asserts thata
matchesb
.- See deep6's match() for details.
doesNotMatch(a, b, msg)
— asserts thata
does not matchb
.rejects(promise, msg)
— asserts thatpromise
rejects.- This is an asynchronous method. It is likely to be waited for.
doesNotResolve()
— an alias ofrejects()
.
resolves(promise, msg)
— asserts thatpromise
resolves.- This is an asynchronous method. It is likely to be waited for.
doesNotReject()
— an alias ofresolves()
.
- Embedded test suites (all of them are asynchronous and should be waited for):
test(name, options, testFn)
— runs a test suite asynchronously. Seetest()
above.skip(name, options, testFn)
— skips a test suite asynchronously. Seetest.skip()
above.todo(name, options, testFn)
— runs a provisional test suite asynchronously. Seetest.todo()
above.asPromise(name, options, testPromiseFn)
— runs a test suite asynchronously. Seetest.asPromise()
above.
- Miscellaneous:
any
— returns theany
object. It can be used in deep equivalency asserts to match any value. See deep6's any for details.plan(n)
— sets the number of tests in the test suite. Rarely used.comment(msg)
— sends a comment to the test harness. Rarely used.skipTest(...args, msg)
— skips the current test yet sends a message to the test harness.bailOut(msg)
— stops the test suite and sends a message to the test harness.
In all cases, the msg
message is optional. If it is not provided, some suitable generic message will be used.
Example:
test('Sample test', async t => {
const result = await getFromDb({first: 'Bob', last: 'Smith'});
t.equal(result.position, 'chief bozo', 'the position is correct');
t.ok(result.manager, 'the manager exists');
const manager = await getFromDb(result.manager);
t.ok(manager, 'the manager is retrieved');
t.equal(manager.first, 'Jane', 'the manager is Jane');
t.deepEqual(manager.employees, ['Bob Smith'], 'Jane manages only Bob Smith');
});
Running tests
It is super easy to run tests:
- Install the
tape-six
package:npm i -D tape-six
- Write a test. For example, you named it
test.js
. - Run the test:
node test.js
- Or:
bun run test.js
- Or:
deno run -A test.js
- Or you can run them in a browser!
- Or:
- Profit!
If you have a lot of tests, you can organize them using multiple files and directories.
tape-six
provides multiple test runners that can run them in different environments.
Configuring test runners
See set-up tests for details.
Command-line utilities
- tape6 — the main utility of the package to run tests in different environments.
- tape6-server — a custom web server with a web application that helps running tests in browsers.
Release notes
The most recent releases:
- 1.0.2 Bugfix for Deno using the JSONL reporter.
- 1.0.1 Technical release: added more links.
- 1.0.0 The first official release.
- 0.12.3 Technical release: exposed internal classes for external utilities.
- 0.12.2 Fixed a minor serialization issue.
- 0.12.1 Minor Deno-related refactoring, fixed the way tests are triggered.
- 0.12.0 Removed data to avoid serializing non-serializable objects.
- 0.11.0 Minor improvements to the server: temporary redirects, a hyperlink to the web app.
- 0.10.0 Refactored test runners, refactored stopping tests on failure, added JSONL reporter, fixed bugs.
- 0.9.6 Updated deps.
- 0.9.5 Updated the lock file.
- 0.9.4 Updated deps. Added test runners for Bun and Deno.
- 0.9.3 Made TTY reporter work with non-TTY streams.
- 0.9.2 Fixed Windows runner.
- 0.9.1 More updates related to renaming
tape6
⇒tape-six
. - 0.9.0 Initial release.
For more info consult full release notes.