Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/reporter/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
90 changes: 69 additions & 21 deletions test/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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'
Expand All @@ -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([
Expand Down
Loading