-
-
Notifications
You must be signed in to change notification settings - Fork 0
test()
Eugene Lazutkin edited this page Jan 18, 2026
·
10 revisions
test is the entry point to the test suite:
import test from 'tape-six';
// import {test} from 'tape-six';
// CommonJS:
// const {test} = require('tape-six');
// const {default: test} = require('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:
async testFn(tester)- The test function can be async or sync. No need to return a promise.
-
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 whole
resolve/rejectpair is modeled after thePromiseAPI. - The
resolvefunction is used to notify that the test suite is finished successfully.- The value passed to
resolve()will be ignored.
- The value passed to
- The
rejectfunction is used to notify that the test suite is finished with an error.- The error passed to
reject()will be reported as a failure.
- The error passed to
- The whole
- 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
nameargument.
- 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,
tape6will stop waiting for the test to finish, but it will continue running in the background. - The default: no timeout.
-
testFn— the optional test function to be executed (see below).- Can be overridden by the
testFnargument.
- Can be overridden by the
-
testPromiseFn— the optional callback-based test function to be executed.- Can be overridden by the
testPromiseFnargument.
- Can be overridden by the
-
-
testFn— a test function to be executed. It will be called with thetesterobject. The result will be ignored.- This function can be synchronous or asynchronous.
-
testPromiseFn— a callback-based test function to be executed (see below). It will be called with thetesterobject and two callbacks:resolveandrejectmodeled on the Promise API.- Value supplied to
resolve()will be ignored. - Value supplied to
reject()will be used as the error message.
- Value supplied to
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);
}
});
test.skip('foo1', t => {
t.pass();
t.fail();
});
test.todo('foo2', t => {
t.pass();
t.fail();
});
test.asPromise('foo3', (t, resolve, reject) => {
const stream = fs.readStream('foo.txt');
let buffer = '';
stream.on('data', chunk => {
buffer += chunk;
});
stream.on('end', () => {
t.equal(buffer, 'foo');
resolve();
});
stream.on('error', error => reject(error));
});