Spanish Total.js
English
Documentation For AI agents
Contents

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.

.md GitHub
On this page

The Test runner from total5/test. The guide Unit tests explains how to use it with examples.

API

class

Test

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).

Load
require('total5/test');

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

Test.run();
method

Test.push(name, fn)

Adds a group of tests.
ParameterTypeDescription
name String The name printed as the header of the group.
fn Function 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.

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

Test.run([callback])

Runs all groups one by one.
ParameterTypeDescription
callback? Function 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.

Run and stop the server
Test.run(function() {
	F.exit();
});
method

Test.print(message, [err])

Prints the result of one check.
ParameterTypeDescription
message String The name of the check.
err? Error|String 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.

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

Test.start(message)

Prints a header between two divider lines.
ParameterTypeDescription
message String 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.

Header
Test.start('Second part');
method

Test.fake(name, action)

Replaces an action with a fake.
ParameterTypeDescription
name String The name of the action.
action Object 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.

Fake and restore
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();
property

Test.count

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

Returns Number

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

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

Test.restore()

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.

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

Test.items

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.

Count groups
console.log(Test.items.length, 'groups');
Made in Europe