Spanish Total.js
English
Documentation For AI agents
Contents

Testing

TESTER(), TEST(), ACTION() and the methods of a test request.

.md GitHub
On this page

The testing tools of Total.js 4. The guide Testing explains how to use them with examples.

API

method

TESTER(callback(group, start), [options])

Runs groups of tests in order and stops at the first failure.
ParameterTypeDescription
callback Function(group, start) Declares the groups and calls start().
options? Object { silent: true } hides the console output.

group(name, fn(test, cleanup)) declares a group. Inside it, test([name], fn(next)) declares a test; call next() on success or next(err) on failure. cleanup(fn) sets a function that runs after the group (with the error object { group, test, err } when a test failed). start([callback]) runs all groups. A failure calls the group cleanup and the callback of start() with the error, then throws an Error with the group and test names. Passed tests print [OK], the end prints [DONE] in N ms; options.silent hides these lines.

tests/index.js
require('total4');

LOAD('config,definitions,schemas,controllers', function() {
	TESTER(function(group, start) {

		group('Users', function(test, cleanup) {

			test('Create', function(next) {
				ACTION('POST /api/users/', { name: 'Peter' }, function(err) {
					next(err);
				});
			});

			cleanup(function(err) {
				// remove test data
			});
		});

		start(function() {
			process.exit(0);
		});
	});
});
method

ACTION(url, [data], [callback])

since 0.0.96 Sends a virtual request to a route.
ParameterTypeDescription
url String The method and the path, e.g. 'GET /api/products/'.
data? Object The body of the request.
callback? Function(err, response) Called with the response.

Returns Controller The controller of the request, or undefined when no route matches.

url is 'METHOD /path/', with an optional query string. The prefix + runs the request as authorized, - as unauthorized. 'API /url/ schema' calls a Route API action. The request has the header x-test: 1 and the IP of the server. data becomes the body. The callback receives the result of the route (err, response), or '404' when no route matches. Returns the controller, so you can set user, session or query before it runs.

Authorized request
var ctrl = ACTION('+POST /api/orders/', { product: 'A1' }, function(err, response) {
	console.log(err, response);
});
ctrl.user = { id: 'test' };
method

TEST(fn(add, response), [done])

since 0.0.96 Runs a list of virtual requests and reports the result.
ParameterTypeDescription
fn Function(add, response) Declares the requests.
done? Function(errors, output)|Controller Receives the result.

Returns Object The test instance.

add(url, name) adds a request (see ACTION()) and returns a test action. The requests run one after another. response collects responses of actions with id(). done is a function (errors, output), or a controller: it then answers with status 200 or 409 and the output as JSON (XHR) or as an HTML page. output is { items: [{ failed, name, error, ts }], failed }.

A test route
exports.install = function() {
	ROUTE('GET /tests/', tests);
};

function tests() {
	TEST(function(add, response) {
		add('POST /api/products/', 'Create a product').data({ name: 'Test' }).id('product');
		add('GET /api/products/', 'List products').pass(function(res) {
			return res.value instanceof Array;
		});
	}, this);
}
method

action.data(body)

since 0.0.96 Sets the body of a test request.
ParameterTypeDescription
body Object The body.

Returns Object The test action.

body() is an alias. The other setters are query(obj), user(obj), session(obj) and url(url). Each returns the action, so you can chain them.

Chain
add('+POST /api/orders/', 'Order').data({ product: 'A1' }).user({ id: 'admin', sa: true });
method

action.pass(fn(res))

since 0.0.96 Marks the request as failed unless fn returns true.
ParameterTypeDescription
fn Function(res) Returns true when the response is correct.

Returns Object The test action.

res has err (also error), value, response (all collected responses) and name. res.invalid(err) (also throw()) sets the error shown in the output. fail(fn) is the opposite: the request fails when fn returns true. Without pass or fail, the request fails when the route returns an error.

Check the value
add('GET /api/products/', 'Products').pass(function(res) {
	if (!res.value.length)
		res.invalid('Empty list');
	return res.value.length > 0;
});
method

action.make(fn(action, response))

since 0.0.96 Prepares a test request just before it runs.
ParameterTypeDescription
fn Function(action, response) Changes the action before it runs.

Returns Object The test action.

Use it to build the request from earlier responses, for example an id saved with id(name). Return false to skip the request. done(fn(value)) runs on success, callback(fn(err, value)) always, and log(fn(err, value)) before the check.

Use an earlier response
add('POST /api/products/', 'Create').data({ name: 'A' }).id('created');
add('', 'Read').make(function(action, response) {
	// response.created is the response of the request with id('created')
	action.url('GET /api/products/' + response.created.id + '/');
});
Made in Europe