Testare i componenti con il tester
Scrivi test automatici per un componente con il tester del repository ufficiale dei componenti.
In questa pagina
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
- Scarica il repository:
git clone https://github.com/totaljs/flowstreamcomponents.git
cd flowstreamcomponents- Installa il framework, una sola volta:
npm install total4- Copia il tuo componente nella cartella
components, per esempiocomponents/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:
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)caricacomponents/<name>.htmle collega ogni uscita a un raccoglitore.test.input(input, data, callback)inviadataall'ingresso con quell'id, e chiamacallbackcon il primo messaggio che esce.test.ok(condition, text)registra un test superato quandoconditionè vera, e uno fallito quando è falsa.next()termina questo test e fa partire ildescribesuccessivo.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
node tests/uppercase.jsIl tester stampa una riga per ogni controllo e un riepilogo:
[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, e l'errore che segnala:
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 è 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').