From 6bbd3f03104e6dcd9da89a8ec5dcb5d992ee3ed5 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Thu, 16 Sep 2021 15:45:34 -0400 Subject: #7 Add Cobertura format --- src/formats.test.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 7 deletions(-) (limited to 'src/formats.test.ts') diff --git a/src/formats.test.ts b/src/formats.test.ts index 1536b5d..02f9a3e 100644 --- a/src/formats.test.ts +++ b/src/formats.test.ts @@ -74,7 +74,7 @@ describe("Formats object", () => { const result = Formats.listFormats(); // Assert - expect(result).toEqual(["tarpaulin"]); + expect(result).toEqual(["tarpaulin", "cobertura"]); }); it("should return the requested format", () => { @@ -105,14 +105,14 @@ describe("Tarpaulin format", () => { expect(matcher).toEqual(defaultColorMatches); }); - it("should parse coverage from a normal tarpaulin file", () => { + it("should parse coverage from a normal tarpaulin file", async () => { // Arrange const file = fs.readFileSync(reportPath("tarpaulin-report.html"), "utf-8"); const format = Formats.getFormat("tarpaulin"); // Act - const result = format.parseCoverage(file); + const result = await format.parseCoverage(file); // Assert expect(typeof result).toEqual("number"); @@ -122,14 +122,14 @@ describe("Tarpaulin format", () => { } }); - it("should parse coverage from an empty tarpaulin file", () => { + it("should parse coverage from an empty tarpaulin file", async () => { // Arrange const file = fs.readFileSync(reportPath("tarpaulin-empty.html"), "utf-8"); const format = Formats.getFormat("tarpaulin"); // Act - const result = format.parseCoverage(file); + const result = await format.parseCoverage(file); // Assert expect(typeof result).toEqual("number"); @@ -138,14 +138,79 @@ describe("Tarpaulin format", () => { } }); - it("should return error when parsing coverage from invalid file", () => { + it("should return error when parsing coverage from invalid file", async () => { // Arrange const file = fs.readFileSync(reportPath("tarpaulin-invalid.html"), "utf-8"); const format = Formats.getFormat("tarpaulin"); // Act - const result = format.parseCoverage(file); + const result = await format.parseCoverage(file); + + // Assert + expect(typeof result).not.toEqual("number"); + if (typeof result !== "number") { + expect(result.message).toEqual("Invalid report document"); + } + }); +}); + +describe("Cobertura format", () => { + const reportPath = (file: string): string => + path.join(__dirname, "..", "example_reports", file); + + it("should use the default color matcher", () => { + // Arrange + const format = Formats.getFormat("cobertura"); + + // Act + const matcher = format.matchColor; + + // Assert + expect(matcher).toEqual(defaultColorMatches); + }); + + it("should parse coverage from a normal cobertura file", async () => { + // Arrange + const file = fs.readFileSync(reportPath("cobertura-report.xml"), "utf-8"); + + const format = Formats.getFormat("cobertura"); + + // Act + const result = await format.parseCoverage(file); + + // Assert + expect(typeof result).toEqual("number"); + if (typeof result === "number") { + // 96.17% is the result given in the document itself + expect(result.toFixed(2)).toEqual("96.04"); + } + }); + + it("should parse coverage from an empty cobertura file", async () => { + // Arrange + const file = fs.readFileSync(reportPath("cobertura-empty.xml"), "utf-8"); + + const format = Formats.getFormat("cobertura"); + + // Act + const result = await format.parseCoverage(file); + + // Assert + expect(typeof result).toEqual("number"); + if (typeof result === "number") { + expect(result.toFixed(2)).toEqual("0.00"); + } + }); + + it("should return error when parsing coverage from invalid file", async () => { + // Arrange + const file = fs.readFileSync(reportPath("cobertura-invalid.xml"), "utf-8"); + + const format = Formats.getFormat("cobertura"); + + // Act + const result = await format.parseCoverage(file); // Assert expect(typeof result).not.toEqual("number"); -- cgit