---
title: Testing
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/testing/
updated: 2026-11-18T16:57:44.579Z
---

# Testing

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

The testing tools of Total.js 4. The guide [Testing](../../guides/testing/) explains how to use them with examples.

## API

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

Type: `method`

Runs groups of tests in order and stops at the first failure.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function(group, start)` | yes |  | Declares the groups and calls `start()`. |
| `options` | `Object` | no |  | `{ 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.

Example: tests/index.js
```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);
		});
	});
});
```

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

Type: `method` · Since: `0.0.96`

Sends a virtual request to a route.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the path, e.g. `'GET /api/products/'`. |
| `data` | `Object` | no |  | The body of the request. |
| `callback` | `Function(err, response)` | no |  | 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.

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

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

Type: `method` · Since: `0.0.96`

Runs a list of virtual requests and reports the result.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(add, response)` | yes |  | Declares the requests. |
| `done` | `Function(errors, output)|Controller` | no |  | 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 }`.

Example: A test route
```js
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);
}
```

### action.data(body)

Type: `method` · Since: `0.0.96`

Sets the body of a test request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `Object` | yes |  | 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.

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

### action.pass(fn(res))

Type: `method` · Since: `0.0.96`

Marks the request as failed unless `fn` returns `true`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(res)` | yes |  | 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.

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

### action.make(fn(action, response))

Type: `method` · Since: `0.0.96`

Prepares a test request just before it runs.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(action, response)` | yes |  | 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.

Example: Use an earlier response
```js
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 + '/');
});
```
