Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: encapsulate mocha into test runner #3490

Merged
merged 1 commit into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions test/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const fs = require('fs');
const path = require('path');
const program = require('commander');
const { Runner } = require('./runner');
const Mocha = require('mocha');
const { fixturesUI } = require('./fixturesUI');
const { TestRunner, createTestSuite } = require('./testRunner');

class NullReporter {}

Expand All @@ -36,26 +35,21 @@ program
.action(async (command) => {
// Collect files
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;
// Build the test model, suite per file.
for (const file of files) {
const mocha = new Mocha({
const testRunner = new TestRunner(file, {
forbidOnly: command.forbidOnly || undefined,
grep: command.grep,
reporter: NullReporter,
retries: command.retries,
timeout: command.timeout,
ui: fixturesUI.bind(null, true),
trialRun: true,
});
if (command.grep)
mocha.grep(command.grep);
mocha.addFile(file);
mocha.loadFiles();
total += grepTotal(mocha.suite, mocha.options.grep);

rootSuite.addSuite(mocha.suite);
mocha.suite.title = path.basename(file);
total += testRunner.grepTotal();
rootSuite.addSuite(testRunner.suite);
testRunner.suite.title = path.basename(file);
}

if (!total) {
Expand Down Expand Up @@ -115,12 +109,3 @@ function collectFiles(dir, filters) {
}
return files;
}

function grepTotal(suite, grep) {
let total = 0;
suite.eachTest(test => {
if (grep.test(test.fullTitle()))
total++;
});
return total;
}
22 changes: 19 additions & 3 deletions test/runner/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ class TestRunner extends EventEmitter {
constructor(file, options) {
super();
this.mocha = new Mocha({
ui: fixturesUI.bind(null, options.trialRun),
forbidOnly: options.forbidOnly,
reporter: NullReporter,
timeout: options.timeout,
reporter: NullReporter
ui: fixturesUI.bind(null, options.trialRun),
});
if (options.grep)
this.mocha.grep(options.grep);
this.mocha.addFile(file);
this.mocha.suite.filterOnly();
this.mocha.loadFiles();
this.suite = this.mocha.suite;
this._lastOrdinal = -1;
this._failedWithError = false;
}
Expand Down Expand Up @@ -79,6 +82,19 @@ class TestRunner extends EventEmitter {
});
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) {
Expand Down Expand Up @@ -122,4 +138,4 @@ function serializeError(error) {
return trimCycles(error);
}

module.exports = { TestRunner };
module.exports = { TestRunner, createTestSuite };