Skip to content

Commit

Permalink
patch(vest): organize genTestSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 15, 2022
1 parent bc85119 commit f78a477
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
5 changes: 5 additions & 0 deletions packages/vest/src/core/suite/produce/Severity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export enum Severity {
WARNINGS = 'warnings',
ERRORS = 'errors',
}

export enum SeverityCount {
ERROR_COUNT = 'errorCount',
WARN_COUNT = 'warnCount',
}
74 changes: 48 additions & 26 deletions packages/vest/src/core/suite/produce/genTestsSummary.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
);
}

/**
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -89,8 +109,10 @@ function baseStats() {
};
}

type TGroups = Record<string, TTestGroup>;

type TTestSummary = {
groups: Record<string, TTestGroup>;
groups: TGroups;
tests: TTestGroup;
} & TTestSummaryBase;

Expand Down

1 comment on commit f78a477

@vercel
Copy link

@vercel vercel bot commented on f78a477 Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next.vercel.app
vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-website.vercel.app

Please sign in to comment.