@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
50 lines (49 loc) • 2.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runMochaBenchmark = runMochaBenchmark;
// eslint-disable-next-line import/no-extraneous-dependencies
const mocha_1 = __importDefault(require("mocha"));
const globalState_js_1 = require("./globalState.js");
const mochaCliExports_js_1 = require("../utils/mochaCliExports.js");
const reporter_js_1 = require("./reporter.js");
const types_js_1 = require("../types.js");
async function runMochaBenchmark(opts, prevBench) {
const mocha = new mocha_1.default({
// Pass all options to mocha, from upstream CLI
...opts,
reporter: (0, reporter_js_1.benchmarkReporterWithPrev)(prevBench, opts.threshold),
// rootHooks: {beforeAll},
});
// Register mocha root suite to append results on it() blocks
const results = new Map();
globalState_js_1.resultsByRootSuite.set(mocha.suite, results);
globalState_js_1.optsByRootSuite.set(mocha.suite, (0, types_js_1.onlyBenchmarkOpts)(opts));
// Recreate `singleRun()` function - https://github.com/mochajs/mocha/blob/dcad90ad6e79864c871e2bc55b22c79ac6952991/lib/cli/run-helpers.js#L120
const fileCollectParams = {
ignore: opts.ignore ?? [],
extension: opts.extension ?? [],
file: opts.file ?? [],
recursive: opts.recursive ?? false,
sort: opts.sort ?? false,
spec: opts.spec ?? [],
};
const files = (0, mochaCliExports_js_1.collectFiles)(fileCollectParams);
mocha.files = files.files;
await mocha.loadFilesAsync();
// Run the tests.
await new Promise((resolve, reject) => {
mocha.run(function (failures) {
// process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
if (failures > 0) {
reject(Error("Some tests failed"));
}
else {
resolve();
}
});
});
return Array.from(results.values());
}