diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-12-11 11:24:28 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-12-11 11:24:28 -0500 |
| commit | 0f7e9dddb3fef9f490d03133f8ea860d4b4da349 (patch) | |
| tree | 69ca08eef6e5e80390cc1766c14497420a098789 /src | |
| parent | 3ae4f9352ee73a21581735883b4049327dce07bf (diff) | |
| download | ao-coverage-0f7e9dddb3fef9f490d03133f8ea860d4b4da349.tar.gz ao-coverage-0f7e9dddb3fef9f490d03133f8ea860d4b4da349.tar.bz2 ao-coverage-0f7e9dddb3fef9f490d03133f8ea860d4b4da349.zip | |
Finish developing unit tests for formats, logger
Diffstat (limited to 'src')
| -rw-r--r-- | src/formats.test.ts | 97 | ||||
| -rw-r--r-- | src/util/logger.test.ts | 45 |
2 files changed, 137 insertions, 5 deletions
diff --git a/src/formats.test.ts b/src/formats.test.ts index fa22f07..a3bc03b 100644 --- a/src/formats.test.ts +++ b/src/formats.test.ts @@ -1,4 +1,7 @@ -import { defaultColorMatches } from "./formats"; +import Formats, { defaultColorMatches } from "./formats"; +import fs from "fs"; +import path from "path"; +import { JSDOM } from "jsdom"; describe("Color matcher", () => { it.each` @@ -55,3 +58,95 @@ describe("Color matcher", () => { expect(result).toEqual(expected); }); }); + +describe("Formats object", () => { + it("should list the available formats", () => { + // Arrange + + // Act + const result = Formats.listFormats(); + + // Assert + expect(result).toEqual(["tarpaulin"]); + }); + + it("should return the requested format", () => { + // Arrange + + // Act + const result = Formats.getFormat("tarpaulin"); + + // Assert + expect(result).toBeDefined(); + expect(result.matchColor).toBeInstanceOf(Function); + expect(result.parseCoverage).toBeInstanceOf(Function); + }); +}); + +describe("Tarpaulin format", () => { + const reportPath = (file: string): string => + path.join(__dirname, "..", "example_reports", file); + + it("should use the default color matcher", () => { + // Arrange + const format = Formats.getFormat("tarpaulin"); + + // Act + const matcher = format.matchColor; + + // Assert + expect(matcher).toEqual(defaultColorMatches); + }); + + it("should parse coverage from a normal tarpaulin file", () => { + // Arrange + const file = fs.readFileSync(reportPath("tarpaulin-report.html"), "utf-8"); + const document = new JSDOM(file).window.document; + + const format = Formats.getFormat("tarpaulin"); + + // Act + const result = format.parseCoverage(document); + + // 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.17"); + } + }); + + it("should parse coverage from an empty tarpaulin file", () => { + // Arrange + const file = fs.readFileSync(reportPath("tarpaulin-empty.html"), "utf-8"); + const document = new JSDOM(file).window.document; + + const format = Formats.getFormat("tarpaulin"); + + // Act + const result = format.parseCoverage(document); + + // 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", () => { + // Arrange + const file = fs.readFileSync(reportPath("tarpaulin-invalid.html"), "utf-8"); + const document = new JSDOM(file).window.document; + + const format = Formats.getFormat("tarpaulin"); + + // Act + const result = format.parseCoverage(document); + + // Assert + expect(typeof result).not.toEqual("number"); + if (typeof result !== "number") { + expect(result.message).toEqual("Invalid report document"); + } + }); +}); diff --git a/src/util/logger.test.ts b/src/util/logger.test.ts index ca181c4..3a2193d 100644 --- a/src/util/logger.test.ts +++ b/src/util/logger.test.ts @@ -1,24 +1,61 @@ import configureLogger from "./logger"; +import { SPLAT } from "triple-beam"; +import Transport from "winston-transport"; describe("Logger configurer", () => { it("should set passed clazz as label", () => { // Arrange const clazz = "important-clazz-name"; + const adapter = { + level: "info", + message: "test/10" + }; // Act const result = configureLogger(clazz); + const actual = result.format.transform(Object.assign({}, adapter)); // Assert + expect(typeof actual).not.toEqual("boolean"); + if (typeof actual !== "boolean") { + expect(actual.level).toEqual(adapter.level); + expect(actual.message).toEqual(adapter.message); + expect(actual.label).toEqual(clazz); + } + }); + + it("should set default formats", () => { + // Arrange + const label = "aaa"; const adapter = { level: "info", - message: "test/10" + message: "%s/10", + [SPLAT]: ["test"] }; + + // Act + const result = configureLogger(label); const actual = result.format.transform(Object.assign({}, adapter)); + + // Assert expect(typeof actual).not.toEqual("boolean"); if (typeof actual !== "boolean") { - expect(actual.level).toEqual(adapter.level); - expect(actual.message).toEqual(adapter.message); - expect(actual.label).toEqual(clazz); + expect(actual.message).toEqual("test/10"); + expect(typeof actual.timestamp).toEqual("string"); + expect(typeof actual.label).toEqual("string"); } }); + + it("should set expected transport methods", () => { + // Arrange + const label = "aaa"; + + // Act + const result = configureLogger(label); + + // Assert + expect(result.transports).toBeInstanceOf(Array); + expect(result.transports.length).toEqual(1); + result.transports.forEach(t => expect(t).toBeInstanceOf(Transport)); + }); }); |
