Spanish Total.js
English
Documentation For AI agents
Contents

Test components with the tester

Write automatic tests for a component with the tester of the official components repository.

.md GitHub
On this page

The official components repository has a small tester. It loads one component into a FlowStream, sends data to its inputs and checks what comes out of its outputs, without Flow and without a browser.

The tester runs the component with Total.js 4, from the total4 package, as the repository sets it up.

Set up the tester

  1. Download the repository:
git clone https://github.com/totaljs/flowstreamcomponents.git
cd flowstreamcomponents
  1. Install the framework, once:
npm install total4
  1. Copy your component into the components folder, for example components/uppercase.html. The tester finds a component by its file name.

Write a test

Create a file in the tests folder, for example tests/uppercase.js:

require('../tester')(async function(describe, done) {

    await describe('uppercase', function(test, next) {
        test.input('input', 'hello flow', function(msg) {
            test.ok(msg.data === 'HELLO FLOW', 'Converts text to uppercase');
            next();
        });
    });

    done();
});
  • describe(name, fn) loads components/<name>.html and connects every output to a catcher.
  • test.input(input, data, callback) sends data to the input with that id, and calls callback with the first message that comes out.
  • test.ok(condition, text) records a passed test when condition is true, and a failed one when it is false.
  • next() ends this test and lets the next describe start. done() prints the results.

Always pass the id of the input as the first argument of test.input: when the first argument is a string, the tester reads it as the id of the input.

Run it

node tests/uppercase.js

The tester prints one line for each check and a summary:

[OK] Uppercase - Converts text to uppercase (2 ms)
|--------------------------------------|
| Total              |               1 |
| Success            |               1 |
| Failed             |               0 |
| Duration           |          417 ms |
|--------------------------------------|

To run every file in the tests folder, run node tester.js.

Test errors and outputs

This test checks both outputs of JSON parse from Errors and logging, and the error it reports:

require('../tester')(async function(describe, done) {

    await describe('jsonparse', function(test, next) {
        test.input('input', '{"name":"Jane"}', function(msg) {
            test.ok(msg.output === 'output' && msg.data.name === 'Jane', 'Parses valid JSON');
            next();
        });
    });

    await describe('jsonparse', function(test, next) {
        test.onerror = function(err) {
            test.ok(err.indexOf('Invalid JSON') === 0, 'Reports invalid JSON');
        };
        test.input('input', 'not json', function(msg) {
            test.ok(msg.output === 'error' && msg.data.text === 'not json', 'Sends invalid JSON to the error output');
            next();
        });
    });

    done();
});
[OK] JSON parse - Parses valid JSON (1 ms)
[OK] JSON parse - Reports invalid JSON (1 ms)
[OK] JSON parse - Sends invalid JSON to the error output (1 ms)

msg.output is the id of the output that sent the message.

More of the test object

NameWhat it does
test.configure(config)Resets the configuration to exports.config, applies these keys and calls instance.configure. With true as the second argument, it does not call instance.configure.
test.trigger(data)Calls instance.trigger(data).
test.output = function(msg)Receives every message from every output.
test.status = function(status)Receives every status of the node.
test.onerror = function(err)Receives the errors of instance.throw.
test.fail(condition, text)Records a failed test when condition is true.
test.instanceThe instance of the node.

The tester stops after 5 seconds. For longer tests, set this.timeout in milliseconds at the start of the callback of require('../tester').

Made in Europe