test: encapsulate mocha into test runner (#3490)
This commit is contained in:
parent
5410c30908
commit
73d2dc11d7
|
|
@ -18,8 +18,7 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const program = require('commander');
|
const program = require('commander');
|
||||||
const { Runner } = require('./runner');
|
const { Runner } = require('./runner');
|
||||||
const Mocha = require('mocha');
|
const { TestRunner, createTestSuite } = require('./testRunner');
|
||||||
const { fixturesUI } = require('./fixturesUI');
|
|
||||||
|
|
||||||
class NullReporter {}
|
class NullReporter {}
|
||||||
|
|
||||||
|
|
@ -36,26 +35,21 @@ program
|
||||||
.action(async (command) => {
|
.action(async (command) => {
|
||||||
// Collect files
|
// Collect files
|
||||||
const files = collectFiles(path.join(process.cwd(), command.args[0]), command.args.slice(1));
|
const files = collectFiles(path.join(process.cwd(), command.args[0]), command.args.slice(1));
|
||||||
const rootSuite = new Mocha.Suite('', new Mocha.Context(), true);
|
const rootSuite = new createTestSuite();
|
||||||
|
|
||||||
let total = 0;
|
let total = 0;
|
||||||
// Build the test model, suite per file.
|
// Build the test model, suite per file.
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const mocha = new Mocha({
|
const testRunner = new TestRunner(file, {
|
||||||
forbidOnly: command.forbidOnly || undefined,
|
forbidOnly: command.forbidOnly || undefined,
|
||||||
|
grep: command.grep,
|
||||||
reporter: NullReporter,
|
reporter: NullReporter,
|
||||||
retries: command.retries,
|
|
||||||
timeout: command.timeout,
|
timeout: command.timeout,
|
||||||
ui: fixturesUI.bind(null, true),
|
trialRun: true,
|
||||||
});
|
});
|
||||||
if (command.grep)
|
total += testRunner.grepTotal();
|
||||||
mocha.grep(command.grep);
|
rootSuite.addSuite(testRunner.suite);
|
||||||
mocha.addFile(file);
|
testRunner.suite.title = path.basename(file);
|
||||||
mocha.loadFiles();
|
|
||||||
total += grepTotal(mocha.suite, mocha.options.grep);
|
|
||||||
|
|
||||||
rootSuite.addSuite(mocha.suite);
|
|
||||||
mocha.suite.title = path.basename(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!total) {
|
if (!total) {
|
||||||
|
|
@ -115,12 +109,3 @@ function collectFiles(dir, filters) {
|
||||||
}
|
}
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
function grepTotal(suite, grep) {
|
|
||||||
let total = 0;
|
|
||||||
suite.eachTest(test => {
|
|
||||||
if (grep.test(test.fullTitle()))
|
|
||||||
total++;
|
|
||||||
});
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -36,14 +36,17 @@ class TestRunner extends EventEmitter {
|
||||||
constructor(file, options) {
|
constructor(file, options) {
|
||||||
super();
|
super();
|
||||||
this.mocha = new Mocha({
|
this.mocha = new Mocha({
|
||||||
ui: fixturesUI.bind(null, options.trialRun),
|
forbidOnly: options.forbidOnly,
|
||||||
|
reporter: NullReporter,
|
||||||
timeout: options.timeout,
|
timeout: options.timeout,
|
||||||
reporter: NullReporter
|
ui: fixturesUI.bind(null, options.trialRun),
|
||||||
});
|
});
|
||||||
if (options.grep)
|
if (options.grep)
|
||||||
this.mocha.grep(options.grep);
|
this.mocha.grep(options.grep);
|
||||||
this.mocha.addFile(file);
|
this.mocha.addFile(file);
|
||||||
this.mocha.suite.filterOnly();
|
this.mocha.suite.filterOnly();
|
||||||
|
this.mocha.loadFiles();
|
||||||
|
this.suite = this.mocha.suite;
|
||||||
this._lastOrdinal = -1;
|
this._lastOrdinal = -1;
|
||||||
this._failedWithError = false;
|
this._failedWithError = false;
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +82,19 @@ class TestRunner extends EventEmitter {
|
||||||
});
|
});
|
||||||
await result;
|
await result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
grepTotal() {
|
||||||
|
let total = 0;
|
||||||
|
this.suite.eachTest(test => {
|
||||||
|
if (this.mocha.options.grep.test(test.fullTitle()))
|
||||||
|
total++;
|
||||||
|
});
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTestSuite() {
|
||||||
|
return new Mocha.Suite('', new Mocha.Context(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function serializeTest(test, origin) {
|
function serializeTest(test, origin) {
|
||||||
|
|
@ -122,4 +138,4 @@ function serializeError(error) {
|
||||||
return trimCycles(error);
|
return trimCycles(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { TestRunner };
|
module.exports = { TestRunner, createTestSuite };
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue