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

# Unit tests

> Write unit tests with the Test runner of total5/test: test groups, results, fake actions and how to run them with total5 test.

`total5` contains a small test runner. `require('total5/test')` loads the framework (when it is not loaded yet) and creates the global object `Test`. You add test groups with `Test.push()`, check results with `Test.print()` and start them with `Test.run()`:

```js
// tests/run.js
require('total5/test');

Test.push('Strings', function(next) {
	Test.print('slug()', 'Hello World'.slug() === 'hello-world' ? null : 'Unexpected slug');
	Test.print('capitalize()', 'total'.capitalize() === 'Total' ? null : 'Unexpected value');
	next();
});

Test.run();
```

The groups run one by one; each one gets `next` and must call it. `Test.print(message)` prints `[OK]`. With a second argument it prints `[FAIL]`, throws the error and ends the process with exit code 1, so the first failure stops the run and a CI job fails. When all groups are done, `Test.run()` prints the number of checks and the time and ends the process with exit code 0, or calls your callback instead.

To test routes, start the server with `F.http()` and wait for `ON('ready')`. Routes answer a moment later, so wait about 500 ms more before the first request. Then send requests with `RESTBuilder` (see [HTTP requests](../../guides/http-requests/)) from the test groups.

## Running tests with total5 test

The command `total5 test` runs the test scripts of a project without a script of your own that loads them. Put the scripts into the `tests` directory of the project:

```
tests/
	config
	init.js
	orders.js
	strings.js
```

```bash
cd ~/projects/shop
total5 test                  # all scripts
total5 test strings          # tests/strings.js
total5 test strings orders   # two scripts, in this order
```

The command:

1. takes the `tests` directory of the current directory, or the current directory when it has no `tests` directory,
2. loads the `config` file of that directory into `CONF`, in the format of the [configuration](../../guides/configuration/) file,
3. creates `Test` and loads `init.js` when it exists,
4. loads the scripts named in the arguments in their order or, without names, every `.js` file of the directory except `init.js`, in alphabetical order,
5. calls `Test.run()`.

The names can also be separated with commas (`total5 test strings,orders`) and the `.js` extension is optional. A name without a file prints `ERROR: the test "name" not found (path)` and ends with exit code 1. The scripts do not need `require('total5/test')`, `Test` is already global:

```js
// tests/strings.js
Test.push('Strings', function(next) {
	Test.print('slug()', 'Hello World'.slug() === 'hello-world' ? null : 'Unexpected slug');
	Test.print('config', CONF.name === 'Shop tests' ? null : 'Unexpected name');
	next();
});
```

The process ends with exit code 0 when all groups are done, and with exit code 1 when a check fails or when the run stops before the end (for example a group that never calls `next`).

`total5 test` uses the `total5` package installed in the project (in `node_modules` of the current directory or of a parent directory), so it works from the `tests` directory too; without one it uses the package the command belongs to. The root of the application (`PATH.root()`) is the current directory, so run the command from the root of the project when the tests load parts of the application.

The command does not start the application. Load what the tests need in `init.js`: it is loaded before the scripts, so a group it adds runs first. `LOAD()` is asynchronous, so call it inside a group and continue with `next` when it is done:

```js
// tests/init.js
Test.push('Init', function(next) {
	LOAD('actions', next);
});
```

## Fake actions

`Test.fake(name, action)` replaces an action for the test. `action` is the same object as in `NEWACTION()`; the original action is kept aside. `Test.restore()` puts all original actions back and removes the fakes of names that had no action. Use it to keep a test away from e-mails, payments or other services. With these actions:

```js
// actions/orders.js
NEWACTION('Orders/create', {
	input: '*name:String',
	action: function($, model) {
		ACTION('Mail/send', { to: 'shop@example.com', subject: 'Order ' + model.name }).callback(function(err, response) {
			if (err)
				$.invalid(err);
			else
				$.callback({ name: model.name, mail: response });
		});
	}
});

NEWACTION('Mail/send', {
	input: '*to:Email,*subject:String',
	action: function($, model) {
		// sends the e-mail
		$.callback('sent');
	}
});
```

the test of `Orders/create` replaces `Mail/send`:

```js
// tests/orders.js
Test.push('Orders', async function(next) {

	var sent = [];

	// no e-mail goes out during the test
	Test.fake('Mail/send', {
		input: '*to:Email,*subject:String',
		action: function($, model) {
			sent.push(model.subject);
			$.callback('fake');
		}
	});

	var response = await ACTION('Orders/create', { name: 'A100' }).promise();
	Test.print('Orders/create', response.mail === 'fake' ? null : 'The fake was not used');
	Test.print('Mail/send', sent[0] === 'Order A100' ? null : 'No e-mail');

	Test.restore();
	next();
});
```

When an action is faked more times before `Test.restore()`, the last fake is used and the restore puts back the original from before the first fake.
