From 976d1daae99539b3438cb411e72f5a11303125d5 Mon Sep 17 00:00:00 2001 From: Maksim Stepanov <17935127+delatrie@users.noreply.github.com> Date: Thu, 31 Oct 2024 05:22:18 +0700 Subject: [PATCH] test(mocha): cover extra reporters with tests --- .../spec/framework/extraReporters.test.ts | 278 ++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 packages/allure-mocha/test/spec/framework/extraReporters.test.ts diff --git a/packages/allure-mocha/test/spec/framework/extraReporters.test.ts b/packages/allure-mocha/test/spec/framework/extraReporters.test.ts new file mode 100644 index 000000000..c44155e9d --- /dev/null +++ b/packages/allure-mocha/test/spec/framework/extraReporters.test.ts @@ -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(/^\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(/^\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(/^\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(/^\s*$/), + ]), + ); + expect(results.outputFiles.get("output.xml")?.toString("utf-8")).toMatch( + //, + ); + }); + + 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( + //, + ); + }); + + 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( + //, + ); + expect(results.outputFiles.get("output2.xml")?.toString("utf-8")).toMatch( + //, + ); + }); + }); + + 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", + ), + ]), + ); + }); + }); +});