Spero di aver compreso correttamente i requisiti. Quello che ho implementato è un forwarder dei risultati del test a mocha che è integrato in mocha.
Per l'integrazione con Mocha questa implementazione descrive uno custom mocha interface per i risultati del test proxy da test eseguiti in un altro ambiente.
Per utilizzare questa interfaccia l'argomento -u
deve essere passato a moka durante l'esecuzione di moka
> mocha -u ./path/to/proxy-interface ...
Nota che ./path/to/proxy-interface
è il percorso che utilizza moka in una chiamata require
per richiedere il modulo di interfaccia.
L'interfaccia proxy è responsabile dell'esporre una funzione proxyTest
al contesto globale come l'interfaccia BDD di mocha fa con it
, richiamare la funzione passata per ottenere i risultati del test e inoltrare i risultati del test mantenendo il numero di test eseguiti visualizzati da il corridore di prova.
var Mocha = require('mocha');
var Suite = Mocha.Suite;
var Test = Mocha.Test;
var escapeRe = require('escape-string-regexp');
module.exports = function(suite) {
var suites = [suite];
suite.on('pre-require', function(context, file, mocha) {
// A bit hacky since we require mocha internal common interface module
var common = require('mocha/lib/interfaces/common')(suites, context);
context.run = mocha.options.delay && common.runWithSuite(suite);
context.proxyTest = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new ProxyTest(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
});
};
var Runnable = Mocha.Runnable;
var inherits = require('util').inherits;
function ProxyTest(title, fn) {
Runnable.call(this, title, null);
this.pending = !fn;
this.type = 'test';
this.body = (fn || '').toString();
this.fn = fn;
}
inherits(ProxyTest, Runnable);
ProxyTest.prototype.run = function(done) {
var proxiedTestResult = this.fn();
this.duration = proxiedTestResult.duration;
this.timedOut = this.timeout() > proxiedTestResult.timeout;
done(proxiedTestResult.result);
};
ProxyTest.prototype.clone = function() {
var test = new ProxyTest(this.title, this.fn);
test.timeout(this.timeout());
test.slow(this.slow());
test.enableTimeouts(this.enableTimeouts());
test.retries(this.retries());
test.currentRetry(this.currentRetry());
test.globals(this.globals());
test.parent = this.parent;
test.file = this.file;
test.ctx = this.ctx;
return test;
};
Il codice sovrascrive sopra implementazione Runnable
corsa di Mocha e gestisce la funzione passata per ottenere i risultati dei test e imposta i campi richiesti nell'interfaccia ProxyTest
per essere compatibile con i test moka.
Uso
Nei vostri test utilizzare il globale proxyTest
per registrare un nuovo test con la moka
var proxy = {
getErrorTestResult() {
return {slow: 200, timeout: 2000, duration: 50, result: 'error'};
},
getTimeoutTestResult() {
return {slow: 200, timeout: 2000, duration: 3000 };
},
getSlowTestResult() {
return {slow: 200, timeout: 2000, duration: 235 };
},
getSuccessTestResult() {
return {slow: 200, timeout: 2000, duration: 50 };
}
}
proxyTest('error', proxy.getErrorTestResult);
proxyTest('timeout', proxy.getTimeoutTestResult);
proxyTest('slow', proxy.getSlowTestResult);
proxyTest('success', proxy.getSuccessTestResult);
uscita

Implicati ons
Lo svantaggio di questo approccio è che un'interfaccia personalizzata deve essere passato al moka E che non è possibile utilizzare moka BDD vocabolario come describe
. Il secondo inconveniente può essere eliminato se si "estende" (in questo caso: copiare un codice) l'interfaccia BDD di moka:
var Mocha = require('mocha');
/**
* Module dependencies.
*/
var Suite = Mocha.Suite;
var Test = Mocha.Test;
var escapeRe = require('escape-string-regexp');
/**
* BDD-style interface - extended with proxy functionality:
*
* describe('Array', function() {
* describe('#indexOf()', function() {
* it('should return -1 when not present', function() {
* // ...
* });
*
* it('should return the index when present', function() {
* // ...
* });
* });
* });
*
* @param {Suite} suite Root suite.
*/
module.exports = function(suite) {
var suites = [suite];
suite.on('pre-require', function(context, file, mocha) {
// A bit hacky since we require mocha internal common interface module
var common = require('mocha/lib/interfaces/common')(suites, context);
context.before = common.before;
context.after = common.after;
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
context.run = mocha.options.delay && common.runWithSuite(suite);
/**
* Describe a "suite" with the given `title`
* and callback `fn` containing nested suites
* and/or tests.
*/
context.describe = context.context = function(title, fn) {
var suite = Suite.create(suites[0], title);
suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
return suite;
};
/**
* Pending describe.
*/
context.xdescribe = context.xcontext = context.describe.skip = function(title, fn) {
var suite = Suite.create(suites[0], title);
suite.pending = true;
suites.unshift(suite);
fn.call(suite);
suites.shift();
};
/**
* Exclusive suite.
*/
context.describe.only = function(title, fn) {
var suite = context.describe(title, fn);
mocha.grep(suite.fullTitle());
return suite;
};
/**
* Describe a specification or test-case
* with the given `title` and callback `fn`
* acting as a thunk.
*/
var it = context.it = context.specify = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new Test(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
/**
* Exclusive test-case.
*/
context.it.only = function(title, fn) {
var test = it(title, fn);
var reString = '^' + escapeRe(test.fullTitle()) + '$';
mocha.grep(new RegExp(reString));
return test;
};
/**
* Pending test case.
*/
context.xit = context.xspecify = context.it.skip = function(title) {
context.it(title);
};
/**
* Number of attempts to retry.
*/
context.it.retries = function(n) {
context.retries(n);
};
context.proxyTest = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new ProxyTest(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
});
};
var Runnable = Mocha.Runnable;
var inherits = require('util').inherits;
function ProxyTest(title, fn) {
Runnable.call(this, title, null);
this.pending = !fn;
this.type = 'test';
this.body = (fn || '').toString();
this.fn = fn;
}
inherits(ProxyTest, Runnable);
ProxyTest.prototype.run = function(done) {
var proxiedTestResult = this.fn();
this.duration = proxiedTestResult.duration;
this.timedOut = this.timeout() > proxiedTestResult.timeout;
done(proxiedTestResult.result);
};
ProxyTest.prototype.clone = function() {
var test = new ProxyTest(this.title, this.fn);
test.timeout(this.timeout());
test.slow(this.slow());
test.enableTimeouts(this.enableTimeouts());
test.retries(this.retries());
test.currentRetry(this.currentRetry());
test.globals(this.globals());
test.parent = this.parent;
test.file = this.file;
test.ctx = this.ctx;
return test;
};
Se ho capito la tua domanda in modo corretto, è possibile impostare params tramite variabili d'ambiente. http: // stackoverflow.it/questions/16144455/mocha-test-con-opzioni-extra-o-parametri –
no non è circa, fornendo opzioni extra a tutti – antpaw
Sembra che ottengo. Si desidera inviare il comando per avviare i test dalla macchina/sito principale all'altra macchina/sito e stampare i risultati? –