diff --git a/lib/reporter/json.js b/lib/reporter/json.js index 53590e3..fe1de74 100644 --- a/lib/reporter/json.js +++ b/lib/reporter/json.js @@ -19,7 +19,9 @@ function toJSON(results) { name: result.name, runsSampled: result.runsSampled, min: result.minFormatted, + minNS: result.min, max: result.maxFormatted, + maxNS: result.max, plugins: result.plugins.map((p) => p.report).filter(Boolean), opsSec: result.opsSec, totalTime: result.totalTime, diff --git a/test/reporter.js b/test/reporter.js index a64efde..4c03c3a 100644 --- a/test/reporter.js +++ b/test/reporter.js @@ -678,19 +678,11 @@ describe("custom reporter should have access to histogram data", async () => { }); }); -describe("jsonReport should produce valid JSON output", async () => { - let output = ""; +describe("toJSON", () => { + let results; before(async () => { - const originalStdoutWrite = process.stdout.write; - process.stdout.write = (data) => { - output += data; - }; - - // Create a new Suite with the JSON reporter - const suite = new Suite({ - reporter: jsonReport, - }); + const suite = new Suite({}); suite .add("single with matcher", () => { @@ -710,25 +702,22 @@ describe("jsonReport should produce valid JSON output", async () => { }); // Run the suite - await suite.run(); + results = await suite.run(); + }); - // Restore stdout - process.stdout.write = originalStdoutWrite; + it("should run", () => { + toJSON(results); }); it("should print valid JSON", () => { - // Verify if the output can be parsed as JSON - let data; - try { - data = JSON.parse(output); - } catch (err) { - assert.fail(`Output is not valid JSON: ${err.message}`); - } + const output = toJSON(results); + const data = JSON.parse(output); assert.ok(Array.isArray(data), "Output should be an array of results"); }); it("should contain the required benchmark fields", () => { + const output = toJSON(results); const data = JSON.parse(output); // We expect the two benchmarks we added: 'single with matcher' and 'Multiple replaces' @@ -746,15 +735,74 @@ describe("jsonReport should produce valid JSON output", async () => { typeof entry.min === "string", "min should be a string (formatted time)", ); + assert.ok( + typeof entry.minNS === "number", + "minNS should be a number (time in NS)", + ); assert.ok( typeof entry.max === "string", "max should be a string (formatted time)", ); + assert.ok( + typeof entry.maxNS === "number", + "maxNS should be a number (time in NS)", + ); assert.ok(Array.isArray(entry.plugins), "plugins should be an array"); } }); }); +describe("jsonReport", () => { + let output = ""; + + before(async () => { + const originalStdoutWrite = process.stdout.write; + process.stdout.write = (data) => { + output += data; + }; + + // Create a new Suite with the JSON reporter + const suite = new Suite({ + reporter: jsonReport, + }); + + suite + .add("single with matcher", () => { + const pattern = /[123]/g; + const replacements = { 1: "a", 2: "b", 3: "c" }; + const subject = "123123123123123123123123123123123123123123123123"; + const r = subject.replace(pattern, (m) => replacements[m]); + assert.ok(r); + }) + .add("Multiple replaces", () => { + const subject = "123123123123123123123123123123123123123123123123"; + const r = subject + .replace(/1/g, "a") + .replace(/2/g, "b") + .replace(/3/g, "c"); + assert.ok(r); + }); + + // Run the suite + await suite.run(); + + // Restore stdout + process.stdout.write = originalStdoutWrite; + }); + + it("should print valid JSON", () => { + // Verify if the output can be parsed as JSON + let data; + try { + data = JSON.parse(output); + } catch (err) { + assert.fail(`Output is not valid JSON: ${err.message}`); + } + + assert.ok(Array.isArray(data), "Output should be an array of results"); + }); +}); + describe("toCSV", () => { it("should generate valid CSV output", async (t) => { const result = toCSV([