Skip to content

Commit

Permalink
test(mocha): cover extra reporters with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie committed Oct 30, 2024
1 parent 4244bd0 commit 976d1da
Showing 1 changed file with 278 additions and 0 deletions.
278 changes: 278 additions & 0 deletions packages/allure-mocha/test/spec/framework/extraReporters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
import { describe, expect, test } from "vitest";
import { runMochaInlineTest } from "../../utils.js";

describe("extra reporters", () => {
describe("json", () => {
test("configuration by name", async () => {
const results = await runMochaInlineTest({ extraReporters: "json" }, ["plain-mocha", "testInSuite"]);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(JSON.parse(results.stdout.join(""))).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});
});

test("configuration by name in array", async () => {
const results = await runMochaInlineTest({ extraReporters: ["json"] }, ["plain-mocha", "testInSuite"]);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(JSON.parse(results.stdout.join(""))).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});
});

test("configuration by name and option", async () => {
const results = await runMochaInlineTest(
{
extraReporters: ["json", { output: "output.json" }],
outputFiles: { "output.json": "application/json" },
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);

expect(JSON.parse(results.outputFiles.get("output.json")?.toString("utf-8") ?? "null")).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});

expect(results.stdout).toEqual([]);
});
});

describe("two entries", () => {
test("both are strings", async () => {
const results = await runMochaInlineTest(
{
extraReporters: ["json-stream", "xunit"],
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(results.stdout).toEqual(
expect.arrayContaining([
expect.stringMatching(/^\["start",/),
expect.stringMatching(/^\["pass",/),
expect.stringMatching(/^\["end",/),

expect.stringMatching(/^<testsuite/),
expect.stringMatching(/^<testcase/),
expect.stringMatching(/testsuite>\s*$/),
]),
);
});

test("both are option-less arrays", async () => {
const results = await runMochaInlineTest(
{
extraReporters: [["json-stream"], ["xunit"]],
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(results.stdout).toEqual(
expect.arrayContaining([
expect.stringMatching(/^\["start",/),
expect.stringMatching(/^\["pass",/),
expect.stringMatching(/^\["end",/),

expect.stringMatching(/^<testsuite/),
expect.stringMatching(/^<testcase/),
expect.stringMatching(/testsuite>\s*$/),
]),
);
});

test("first entry has options", async () => {
const results = await runMochaInlineTest(
{
extraReporters: [["json", { output: "output.json" }], "xunit"],
outputFiles: { "output.json": "application/json" },
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(results.stdout).toEqual(
expect.arrayContaining([
expect.stringMatching(/^<testsuite/),
expect.stringMatching(/^<testcase/),
expect.stringMatching(/testsuite>\s*$/),
]),
);
expect(results.stdout).not.toEqual(
expect.arrayContaining([
expect.stringMatching(/^\["start",/),
expect.stringMatching(/^\["pass",/),
expect.stringMatching(/^\["end",/),
]),
);
expect(JSON.parse(results.outputFiles.get("output.json")?.toString("utf-8") ?? "null")).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});
});

test("second entry has options", async () => {
const results = await runMochaInlineTest(
{
extraReporters: ["json", ["xunit", { output: "output.xml" }]],
outputFiles: { "output.xml": "application/xml" },
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(JSON.parse(results.stdout.join(""))).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});
expect(results.stdout).not.toEqual(
expect.arrayContaining([
expect.stringMatching(/^<testsuite/),
expect.stringMatching(/^<testcase/),
expect.stringMatching(/testsuite>\s*$/),
]),
);
expect(results.outputFiles.get("output.xml")?.toString("utf-8")).toMatch(
/<testsuite[^<]+<testcase[^<]+<\/testsuite>/,
);
});

test("both entries have options", async () => {
const results = await runMochaInlineTest(
{
extraReporters: [
["json", { output: "output.json" }],
["xunit", { output: "output.xml" }],
],
outputFiles: {
"output.json": "application/json",
"output.xml": "application/xml",
},
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(results.stdout).toEqual([]);
expect(JSON.parse(results.outputFiles.get("output.json")?.toString("utf-8") ?? "null")).toMatchObject({
stats: expect.objectContaining({
suites: 1,
tests: 1,
passes: 1,
}),
});
expect(results.outputFiles.get("output.xml")?.toString("utf-8")).toMatch(
/<testsuite[^<]+<testcase[^<]+<\/testsuite>/,
);
});

test("both reporters have done callback", async () => {
const results = await runMochaInlineTest(
{
extraReporters: [
["xunit", { output: "output1.xml" }],
["xunit", { output: "output2.xml" }],
],
outputFiles: {
"output1.xml": "application/xml",
"output2.xml": "application/xml",
},
},
["plain-mocha", "testInSuite"],
);

expect(results.tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(results.stdout).toEqual([]);
expect(results.outputFiles.get("output1.xml")?.toString("utf-8")).toMatch(
/<testsuite[^<]+<testcase[^<]+<\/testsuite>/,
);
expect(results.outputFiles.get("output2.xml")?.toString("utf-8")).toMatch(
/<testsuite[^<]+<testcase[^<]+<\/testsuite>/,
);
});
});

describe("errors", () => {
test("a reporter must be a string", async () => {
const { exitCode, stderr } = await runMochaInlineTest(
// @ts-ignore
{ extraReporters: 1 },
["plain-mocha", "testInSuite"],
);

expect(exitCode).not.toEqual(0);
expect(stderr).toEqual(
expect.arrayContaining([
expect.stringContaining("A reporter entry must be a module name or a constructor. Got number"),
]),
);
});

test("a reporter module must exist", async () => {
const { exitCode, stderr } = await runMochaInlineTest(
// @ts-ignore
{ extraReporters: "foo" },
["plain-mocha", "testInSuite"],
);

expect(exitCode).not.toEqual(0);
expect(stderr).toEqual(expect.arrayContaining([expect.stringContaining("Can't load the 'foo' reporter")]));
});

test("a reporter entry can't be an empty array", async () => {
const { exitCode, stderr } = await runMochaInlineTest(
// @ts-ignore
{ extraReporters: [] },
["plain-mocha", "testInSuite"],
);

expect(exitCode).not.toEqual(0);
expect(stderr).toEqual(
expect.arrayContaining([
expect.stringContaining(
"If an extra reporter entry is an array, it must contain one or two elements. 0 found",
),
]),
);
});

test("a reporter entry in an array can't be an empty array", async () => {
const { exitCode, stderr } = await runMochaInlineTest(
// @ts-ignore
{ extraReporters: [[]] },
["plain-mocha", "testInSuite"],
);

expect(exitCode).not.toEqual(0);
expect(stderr).toEqual(
expect.arrayContaining([
expect.stringContaining(
"If an extra reporter entry is an array, it must contain one or two elements. 0 found",
),
]),
);
});
});
});

0 comments on commit 976d1da

Please sign in to comment.