-
-
Notifications
You must be signed in to change notification settings - Fork 0
test()
Eugene Lazutkin edited this page Oct 2, 2024
·
10 revisions
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
nameargument.
- Can be overridden by the
-
testFn— the optional test function to be executed.- Can be overridden by the
testFnargument.
- 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 test function to be executed. It will be called with thetesterobject. 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 thetesterobject and two callbacks:resolveandrejectare modeled on the Promise API. The result will be ignored.
-
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));
})