Skip to content

Commit

Permalink
lib: support reporter: false|null
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Sep 27, 2024
1 parent 35de97f commit ded2846
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ class Suite {
constructor(options = {}) {
this.#benchmarks = [];
validateObject(options, 'options');
if (options?.reporter) {
validateFunction(options.reporter, 'reporter');
if (options?.reporter !== undefined) {
if (options?.reporter !== false && options?.reporter !== null) {
validateFunction(options.reporter, 'reporter');
}
this.#reporter = options.reporter;
} else {
this.#reporter = reportConsoleBench;
}

if (options?.plugins) {
validateArray(options.plugins, 'plugin');
validatePlugins(options.plugins);
}
this.#plugins = options?.plugins || [new V8NeverOptimizePlugin()];
this.#reporter = options?.reporter || reportConsoleBench;
}

add(name, options, fn) {
Expand Down Expand Up @@ -102,7 +107,9 @@ class Suite {
debugBench(`Starting ${ benchmark.name } with minTime=${ benchmark.minTime }, maxTime=${ benchmark.maxTime }`);
const result = await runBenchmark(benchmark, initialIteration);
results[i] = result;
this.#reporter(benchmark, result);
if (this.#reporter) {
this.#reporter(benchmark, result);
}
}
return results;
}
Expand Down
7 changes: 7 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe('API Interface', () => {
new Suite({ reporter: () => {} });
});

it('reporter can be false or null', () => {
[false, null].forEach((r) => {
// doesNotThrow
new Suite({ reporter: r });
});
});

describe('suite.add', () => {
const bench = new Suite({ reporter: noop });
it('name should be an string', () => {
Expand Down

0 comments on commit ded2846

Please sign in to comment.