From 3ae4f9352ee73a21581735883b4049327dce07bf Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Wed, 11 Dec 2019 09:40:22 -0500 Subject: Add Jest to handle unit and integration testing Wrote a couple of initial unit tests for some of the logic-heavy points in the application, being logger.ts and formats.ts. Evidently colorize() causes an error when transforming on the Format object that's returned. Since that is specific to the Console transport anyways, I just moved it to the transport, since the unit test was only testing the passed label in the returned Formats object. Also the bash template has some issues that I didn't test (oops), so those are fixed now. --- src/formats.test.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/formats.ts | 9 +++++--- src/util/logger.test.ts | 24 +++++++++++++++++++++ src/util/logger.ts | 19 ++++++++--------- 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 src/formats.test.ts create mode 100644 src/util/logger.test.ts (limited to 'src') diff --git a/src/formats.test.ts b/src/formats.test.ts new file mode 100644 index 0000000..fa22f07 --- /dev/null +++ b/src/formats.test.ts @@ -0,0 +1,57 @@ +import { defaultColorMatches } from "./formats"; + +describe("Color matcher", () => { + it.each` + n | s1 | s2 | expected + ${100} | ${75} | ${50} | ${"0f0"} + ${75} | ${75} | ${50} | ${"0f0"} + ${50} | ${75} | ${50} | ${"ff0"} + ${0} | ${75} | ${50} | ${"f00"} + ${51} | ${51} | ${50} | ${"0f0"} + ${50} | ${51} | ${50} | ${"ff0"} + ${0} | ${50} | ${1} | ${"f00"} + ${100} | ${100} | ${0} | ${"0f0"} + ${0} | ${100} | ${0} | ${"ff0"} + ${75} | ${75} | ${60} | ${"0f0"} + ${74} | ${75} | ${60} | ${"1f0"} + ${73} | ${75} | ${60} | ${"2f0"} + ${72} | ${75} | ${60} | ${"3f0"} + ${71} | ${75} | ${60} | ${"4f0"} + ${70} | ${75} | ${60} | ${"5f0"} + ${69} | ${75} | ${60} | ${"6f0"} + ${68} | ${75} | ${60} | ${"7f0"} + ${67} | ${75} | ${60} | ${"8f0"} + ${66} | ${75} | ${60} | ${"9f0"} + ${65} | ${75} | ${60} | ${"af0"} + ${64} | ${75} | ${60} | ${"bf0"} + ${63} | ${75} | ${60} | ${"cf0"} + ${62} | ${75} | ${60} | ${"df0"} + ${61} | ${75} | ${60} | ${"ef0"} + ${60} | ${75} | ${60} | ${"ff0"} + ${15} | ${75} | ${15} | ${"ff0"} + ${14} | ${75} | ${15} | ${"fe0"} + ${13} | ${75} | ${15} | ${"fd0"} + ${12} | ${75} | ${15} | ${"fc0"} + ${11} | ${75} | ${15} | ${"fb0"} + ${10} | ${75} | ${15} | ${"fa0"} + ${9} | ${75} | ${15} | ${"f90"} + ${8} | ${75} | ${15} | ${"f80"} + ${7} | ${75} | ${15} | ${"f70"} + ${6} | ${75} | ${15} | ${"f60"} + ${5} | ${75} | ${15} | ${"f50"} + ${4} | ${75} | ${15} | ${"f40"} + ${3} | ${75} | ${15} | ${"f30"} + ${2} | ${75} | ${15} | ${"f20"} + ${1} | ${75} | ${15} | ${"f10"} + ${0} | ${75} | ${15} | ${"f00"} + `("should return $expected at $n%", ({ n, s1, s2, expected }) => { + // Arrange + const gradient = { stage1: s1, stage2: s2 }; + + // Act + const result = defaultColorMatches(n, gradient); + + // Assert + expect(result).toEqual(expected); + }); +}); diff --git a/src/formats.ts b/src/formats.ts index e878399..86d3f6b 100644 --- a/src/formats.ts +++ b/src/formats.ts @@ -24,7 +24,7 @@ export interface GradientStyle { } // color is a gradient from green (>=stage_1) -> yellow (stage_2) -> red. Stage values should come from metadata. -const defaultColorMatches = ( +export const defaultColorMatches = ( coverage: number, style: GradientStyle ): string => { @@ -32,9 +32,12 @@ const defaultColorMatches = ( coverage >= style.stage1 ? 15 : coverage >= style.stage2 - ? (Math.floor(coverage) - style.stage2) * 16 + 15 + ? Math.floor( + ((style.stage1 - coverage) / (style.stage1 - style.stage2)) * 240 + ) + 15 : 240 + Math.floor(coverage / (style.stage2 / 15)); - return gradient.toString(16) + "0"; + const result = gradient.toString(16); + return (result.length === 1 ? "0" : "") + result + "0"; }; const FormatsObj: FormatObj = { diff --git a/src/util/logger.test.ts b/src/util/logger.test.ts new file mode 100644 index 0000000..ca181c4 --- /dev/null +++ b/src/util/logger.test.ts @@ -0,0 +1,24 @@ +import configureLogger from "./logger"; + +describe("Logger configurer", () => { + it("should set passed clazz as label", () => { + // Arrange + const clazz = "important-clazz-name"; + + // Act + const result = configureLogger(clazz); + + // Assert + const adapter = { + level: "info", + message: "test/10" + }; + const actual = result.format.transform(Object.assign({}, adapter)); + 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); + } + }); +}); diff --git a/src/util/logger.ts b/src/util/logger.ts index d108ae0..27eeefe 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -6,6 +6,13 @@ const { Console } = winston.transports; const LOG_LEVEL = process.env.LOG_LEVEL || "info"; +const consoleFormat = combine( + colorize(), + printf(({ level, message, label, timestamp }) => { + return `${timestamp} [${label}] ${level}: ${message}`; + }) +); + /** * Provides standard logging format and output for the server. */ @@ -16,14 +23,6 @@ export default ( format: Format; transports: Transport[]; } => ({ - format: combine( - splat(), - timestamp(), - label({ label: clazz }), - colorize(), - printf(({ level, message, label, timestamp }) => { - return `${timestamp} [${label}] ${level}: ${message}`; - }) - ), - transports: [new Console({ level: level })] + format: combine(splat(), timestamp(), label({ label: clazz })), + transports: [new Console({ level: level, format: consoleFormat })] }); -- cgit