---
title: Unit tests
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/unit-tests/
updated: 2026-11-18T16:57:34.578Z
---

# Unit tests

> The Test runner of total5/test: Test.push(), Test.run(), Test.print(), Test.start(), Test.fake(), Test.restore(), Test.count and Test.items.

The `Test` runner from `total5/test`. The guide [Unit tests](../../guides/unit-tests/) explains how to use it with examples.

## API

### Test

Type: `class`

The global test runner created by `require('total5/test')`.

Returns: `Object`

Loads `total5` when it is not loaded and creates the global `Test` object with `push()`, `run()`, `print()`, `start()`, `fake()`, `restore()`, `count` and `items`. Run the test file with `node`, or the test scripts of a project with `total5 test` (see [Command line](../../reference/command-line/)).

Example: Load
```js
require('total5/test');

Test.push('Numbers', function(next) {
	Test.print('sum', 1 + 1 === 2 ? null : 'Wrong sum');
	next();
});

Test.run();
```

### Test.push(name, fn)

Type: `method`

Adds a group of tests.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name printed as the header of the group. |
| `fn` | `Function` | yes |  | `function(next)` |

Returns: `undefined`

The groups run in the order they were added. `fn` gets `next` and must call it when the group is done, also after asynchronous checks.

Example: Async group
```js
Test.push('Download', function(next) {
	RESTBuilder.GET('https://www.example.com').callback(function(err) {
		Test.print('GET', err);
		next();
	});
});
```

### Test.run([callback])

Type: `method`

Runs all groups one by one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | `function()` called instead of ending the process. |

Returns: `undefined`

Prints the header of each group, then the number of checks and the time. Without `callback` the process ends with exit code 0 when all groups are done.

Example: Run and stop the server
```js
Test.run(function() {
	F.exit();
});
```

### Test.print(message, [err])

Type: `method`

Prints the result of one check.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | yes |  | The name of the check. |
| `err` | `Error|String` | no |  | The reason of a failure. |

Returns: `undefined`

Without `err` (or with a falsy value) it prints `[OK] message` and counts the check. With `err` it prints `[FAIL] message`, throws the error (a string becomes an `Error`) and ends the process with exit code 1.

Example: Check a value
```js
var value = 'Total.js'.slug();
Test.print('slug()', value === 'total-js' ? null : 'Unexpected: ' + value);
```

### Test.start(message)

Type: `method`

Prints a header between two divider lines.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | yes |  | The text of the header. |

Returns: `undefined`

`Test.run()` calls it with the name of each group. Call it yourself to separate parts of a long group.

Example: Header
```js
Test.start('Second part');
```

### Test.fake(name, action)

Type: `method`

Replaces an action with a fake.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the action. |
| `action` | `Object` | yes |  | The definition of the fake, the same object as in `NEWACTION()`. |

Returns: `Object` The registered fake action.

Keeps the action `name` aside (only the first time until `Test.restore()`) and registers `action` under the same name with `NEWACTION()`. Calls of `ACTION(name)` then run the fake. It also works for a name that has no action.

Example: Fake and restore
```js
NEWACTION('Users/read', {
	action: $ => $.callback('real')
});

Test.push('Fake', async function(next) {
	Test.fake('Users/read', { action: $ => $.callback('fake') });
	var value = await ACTION('Users/read').promise();
	Test.print('fake', value === 'fake' ? null : 'Unexpected: ' + value);
	Test.restore();
	value = await ACTION('Users/read').promise();
	Test.print('restore', value === 'real' ? null : 'Unexpected: ' + value);
	next();
});

Test.run();
```

### Test.count

Type: `property`

The number of checks printed by `Test.print()`.

Returns: `Number`

`Test.run()` prints it at the end as `Tests: <count>`.

Example: Read
```js
Test.run(function() {
	console.log('Checks:', Test.count);
	process.exit(0);
});
```

### Test.restore()

Type: `method`

Puts the original actions back.

Returns: `undefined`

Removes every fake registered with `Test.fake()` and puts the original actions back. A fake of a name that had no action is only removed.

Example: After a group
```js
Test.push('Payments', function(next) {
	Test.fake('Payments/charge', { action: $ => $.callback({ paid: true }) });
	// ... the checks
	Test.restore();
	next();
});
```

### Test.items

Type: `property`

The groups added with `Test.push()`.

Returns: `Array`

An array of `{ name, fn }` objects. `Test.run()` runs them one by one; the array keeps them.

Example: Count groups
```js
console.log(Test.items.length, 'groups');
```
