---
title: Testare i componenti con il tester
project: Total.js Flow
version: 11.1
section: development
language: it
license: MIT
url: https://totaljs.es/docs/it/flow/11-1/development/testing/
updated: 2026-11-19T10:58:54.144Z
---

# Testare i componenti con il tester

> Scrivi test automatici per un componente con il tester del repository ufficiale dei componenti.

Il repository ufficiale dei componenti ha un piccolo tester. Carica un componente in un FlowStream, invia dati ai suoi ingressi e controlla cosa esce dalle sue uscite, senza Flow e senza un browser.

Il tester esegue il componente con Total.js 4, dal pacchetto `total4`, come lo configura il repository.

## Preparare il tester

1. Scarica il repository:

```bash
git clone https://github.com/totaljs/flowstreamcomponents.git
cd flowstreamcomponents
```

2. Installa il framework, una sola volta:

```bash
npm install total4
```

3. Copia il tuo componente nella cartella `components`, per esempio `components/uppercase.html`. Il tester trova un componente in base al nome del file.

## Scrivere un test

Crea un file nella cartella `tests`, per esempio `tests/uppercase.js`:

```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)` carica `components/<name>.html` e collega ogni uscita a un raccoglitore.
- `test.input(input, data, callback)` invia `data` all'ingresso con quell'id, e chiama `callback` con il primo messaggio che esce.
- `test.ok(condition, text)` registra un test superato quando `condition` è vera, e uno fallito quando è falsa.
- `next()` termina questo test e fa partire il `describe` successivo. `done()` stampa i risultati.

Passa sempre l'id dell'ingresso come primo argomento di `test.input`: quando il primo argomento è una stringa, il tester la legge come id dell'ingresso.

## Eseguirlo

```bash
node tests/uppercase.js
```

Il tester stampa una riga per ogni controllo e un riepilogo:

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

Per eseguire tutti i file della cartella `tests`, esegui `node tester.js`.

## Testare errori e uscite

Questo test controlla entrambe le uscite di **JSON parse** da [Errori e log](../errors-and-logging/), e l'errore che segnala:

```js
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();
});
```

```text
[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` è l'id dell'uscita che ha inviato il messaggio.

## Altro sull'oggetto test

| Nome | Cosa fa |
|---|---|
| `test.configure(config)` | Riporta la configurazione a `exports.config`, applica queste chiavi e chiama `instance.configure`. Con `true` come secondo argomento, non chiama `instance.configure`. |
| `test.trigger(data)` | Chiama `instance.trigger(data)`. |
| `test.output = function(msg)` | Riceve ogni messaggio da ogni uscita. |
| `test.status = function(status)` | Riceve ogni stato del nodo. |
| `test.onerror = function(err)` | Riceve gli errori di `instance.throw`. |
| `test.fail(condition, text)` | Registra un test fallito quando `condition` è vera. |
| `test.instance` | L'istanza del nodo. |

Il tester si ferma dopo 5 secondi. Per test più lunghi, imposta `this.timeout` in millisecondi all'inizio della callback di `require('../tester')`.
