Skip to content

Commit

Permalink
test(mocha): test extra reporters by ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie committed Oct 31, 2024
1 parent 411ecc5 commit 8a20d4a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/allure-mocha/test/samples/customReporter.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Mocha = require("mocha");

class CustomReporter extends Mocha.reporters.Base {
constructor(runner, opts) {
super(runner, opts);
runner.on("start", () => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(opts.reporterOptions));
});
}
}

module.exports = CustomReporter;
28 changes: 28 additions & 0 deletions packages/allure-mocha/test/spec/framework/extraReporters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,34 @@ describe("extra reporters", () => {
});
});

describe("local reporter", () => {
test("local reporter without options", async () => {
const { tests, stdout } = await runMochaInlineTest(
{
extraReporters: "./customReporter.cjs",
inputFiles: ["customReporter.cjs"],
},
"plain-mocha/testInSuite",
);

expect(tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(JSON.parse(stdout.join(""))).toEqual({});
});

test("local reporter with options", async () => {
const { tests, stdout } = await runMochaInlineTest(
{
extraReporters: ["./customReporter.cjs", { foo: "bar" }],
inputFiles: ["customReporter.cjs"],
},
"plain-mocha/testInSuite",
);

expect(tests).toEqual([expect.objectContaining({ name: "a test in a suite" })]);
expect(JSON.parse(stdout.join(""))).toEqual({ foo: "bar" });
});
});

describe("errors", () => {
test("a reporter must be a string", async () => {
const { exitCode, stderr } = await runMochaInlineTest(
Expand Down
6 changes: 5 additions & 1 deletion packages/allure-mocha/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type MochaRunOptions = {
environmentInfo?: { [keys: string]: string };
categories?: readonly Category[];
extraReporters?: AllureMochaReporterConfig["extraReporters"];
inputFiles?: string[];
outputFiles?: Record<string, string>;
};

Expand Down Expand Up @@ -88,7 +89,10 @@ abstract class AllureMochaTestRunner {

const testDir = path.join(this.runResultsDir, randomUUID());

const filesToCopy = [...this.getFilesToCopy(testDir)];
const filesToCopy = [
...this.getFilesToCopy(testDir),
...(this.config.inputFiles?.map((f) => this.getCopyEntry(f, testDir)) ?? []),
];
const filesToTransform = [
...this.getFilesToTransform(testDir),
...samples.map((sample) => this.#getSampleEntry(sample, this.specsPath, testDir)),
Expand Down

0 comments on commit 8a20d4a

Please sign in to comment.