From f78a4772c43bf4ccd4ee6c4496a59445fb1e0f06 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Tue, 15 Mar 2022 14:03:43 -0700 Subject: [PATCH] patch(vest): organize genTestSummary --- .../vest/src/core/suite/produce/Severity.ts | 5 ++ .../src/core/suite/produce/genTestsSummary.ts | 74 ++++++++++++------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/packages/vest/src/core/suite/produce/Severity.ts b/packages/vest/src/core/suite/produce/Severity.ts index a40f80b6c..db1fb8e68 100644 --- a/packages/vest/src/core/suite/produce/Severity.ts +++ b/packages/vest/src/core/suite/produce/Severity.ts @@ -2,3 +2,8 @@ export enum Severity { WARNINGS = 'warnings', ERRORS = 'errors', } + +export enum SeverityCount { + ERROR_COUNT = 'errorCount', + WARN_COUNT = 'warnCount', +} diff --git a/packages/vest/src/core/suite/produce/genTestsSummary.ts b/packages/vest/src/core/suite/produce/genTestsSummary.ts index a9de2f513..814f2f3be 100644 --- a/packages/vest/src/core/suite/produce/genTestsSummary.ts +++ b/packages/vest/src/core/suite/produce/genTestsSummary.ts @@ -1,6 +1,6 @@ import assign from 'assign'; -import { Severity } from 'Severity'; +import { Severity, SeverityCount } from 'Severity'; import VestTest from 'VestTest'; import { useTestsFlat } from 'stateHooks'; @@ -15,25 +15,37 @@ export default function genTestsSummary(): TTestSummary { tests: {}, }); - appendSummary(testObjects); + testObjects.reduce( + (summary: TTestSummary, testObject: VestTest): TTestSummary => { + appendToTest(summary.tests, testObject); + appendToGroup(summary.groups, testObject); + return summary; + }, + summary + ); return countFailures(summary); +} - function appendSummary(testObjects: VestTest[]) { - testObjects.forEach(testObject => { - const { fieldName, groupName } = testObject; +function appendToTest(tests: TTestGroup, testObject: VestTest) { + tests[testObject.fieldName] = appendTestObject(tests, testObject); +} - summary.tests[fieldName] = genTestObject(summary.tests, testObject); +/** + * Appends to a group object if within a group + */ +function appendToGroup(groups: TGroups, testObject: VestTest) { + const { groupName } = testObject; - if (groupName) { - summary.groups[groupName] = summary.groups[groupName] || {}; - summary.groups[groupName][fieldName] = genTestObject( - summary.groups[groupName], - testObject - ); - } - }); + if (!groupName) { + return; } + + groups[groupName] = groups[groupName] || {}; + groups[groupName][testObject.fieldName] = appendTestObject( + groups[groupName], + testObject + ); } /** @@ -48,8 +60,11 @@ function countFailures(summary: TTestSummary): TTestSummary { return summary; } +/** + * Appends the test to a results object + */ // eslint-disable-next-line max-statements -function genTestObject( +function appendTestObject( summaryKey: TTestGroup, testObject: VestTest ): TSingleTestSummary { @@ -63,22 +78,27 @@ function genTestObject( summaryKey[fieldName].testCount++; - // Adds to severity group - function addTo(severity: Severity) { - const countKey = severity === Severity.ERRORS ? 'errorCount' : 'warnCount'; + if (testObject.isFailing()) { + incrementFailures(Severity.ERRORS); + } else if (testObject.isWarning()) { + incrementFailures(Severity.WARNINGS); + } + + return testKey; + + function incrementFailures(severity: Severity) { + const countKey = getCountKey(severity); testKey[countKey]++; if (message) { testKey[severity] = (testKey[severity] || []).concat(message); } } +} - if (testObject.isFailing()) { - addTo(Severity.ERRORS); - } else if (testObject.isWarning()) { - addTo(Severity.WARNINGS); - } - - return testKey; +function getCountKey(severity: Severity): SeverityCount { + return severity === Severity.ERRORS + ? SeverityCount.ERROR_COUNT + : SeverityCount.WARN_COUNT; } function baseStats() { @@ -89,8 +109,10 @@ function baseStats() { }; } +type TGroups = Record; + type TTestSummary = { - groups: Record; + groups: TGroups; tests: TTestGroup; } & TTestSummaryBase;