From db002480e42f4e60b89702aaa3443c598f2bd521 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Sat, 8 Jun 2024 18:42:19 +0300 Subject: [PATCH] patch(vest): simplify useProduceSuiteSummary --- .../vest/src/suiteResult/SuiteResultTypes.ts | 12 +- .../selectors/useProduceSuiteSummary.ts | 113 +++++++----------- 2 files changed, 51 insertions(+), 74 deletions(-) diff --git a/packages/vest/src/suiteResult/SuiteResultTypes.ts b/packages/vest/src/suiteResult/SuiteResultTypes.ts index 075c35f04..e3b9fc05b 100644 --- a/packages/vest/src/suiteResult/SuiteResultTypes.ts +++ b/packages/vest/src/suiteResult/SuiteResultTypes.ts @@ -1,4 +1,4 @@ -import { Maybe } from 'vest-utils'; +import { Maybe, Nullable } from 'vest-utils'; import { Severity } from 'Severity'; import { SummaryFailure } from 'SummaryFailure'; @@ -14,13 +14,13 @@ export class SummaryBase { export class SuiteSummary< F extends TFieldName, - G extends TGroupName + G extends TGroupName, > extends SummaryBase { public [Severity.ERRORS]: SummaryFailure[] = []; public [Severity.WARNINGS]: SummaryFailure[] = []; public groups: Groups = {} as Groups; public tests: Tests = {} as Tests; - public valid = false; + public valid: Nullable = null; } export type TestsContainer = @@ -38,7 +38,7 @@ export type Tests = Record; export type SingleTestSummary = SummaryBase & { errors: string[]; warnings: string[]; - valid: boolean; + valid: Nullable; pendingCount: number; }; @@ -48,12 +48,12 @@ export type FailureMessages = Record; export type SuiteResult< F extends TFieldName, - G extends TGroupName + G extends TGroupName, > = SuiteSummary & SuiteSelectors & { suiteName: SuiteName }; export type SuiteRunResult< F extends TFieldName, - G extends TGroupName + G extends TGroupName, > = SuiteResult & { done: Done; }; diff --git a/packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts b/packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts index c5ae202d2..bf5e5922b 100644 --- a/packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts +++ b/packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts @@ -23,66 +23,64 @@ export function useProduceSuiteSummary< F extends TFieldName, G extends TGroupName, >(): SuiteSummary { - const summary: SuiteSummary = new SuiteSummary(); - - TestWalker.walkTests(testObject => { - summary.tests = useAppendToTest(summary.tests, testObject); + // @vx-allow use-use (TODO: fix this. the error is in the lint rule) + const summary = TestWalker.reduceTests< + SuiteSummary, + TIsolateTest + >((summary, testObject) => { + const fieldName = VestTest.getData(testObject).fieldName; + summary.tests[fieldName] = useAppendToTest(summary.tests, testObject); summary.groups = useAppendToGroup(summary.groups, testObject); - summary.errors = appendFailures( - Severity.ERRORS, - summary.errors, - testObject, - ); - summary.warnings = appendFailures( - Severity.WARNINGS, - summary.warnings, - testObject, - ); - }); - summary.valid = useShouldAddValidProperty(); + if (VestTest.isOmitted(testObject)) { + return summary; + } + if (summary.tests[fieldName].valid === false) { + summary.valid = false; + } + return addSummaryStats(testObject, summary); + }, new SuiteSummary()); - return countOverallStates(summary); + summary.valid = summary.valid === false ? false : useShouldAddValidProperty(); + + return summary; } -function appendFailures( - key: Severity, - failures: SummaryFailure[], +function addSummaryStats( testObject: TIsolateTest, -): SummaryFailure[] { - if (VestTest.isOmitted(testObject)) { - return failures; + summary: SuiteSummary, +): SuiteSummary { + if (VestTest.isWarning(testObject)) { + summary.warnCount++; + summary.warnings.push(SummaryFailure.fromTestObject(testObject)); + } else if (VestTest.isFailing(testObject)) { + summary.errorCount++; + summary.errors.push(SummaryFailure.fromTestObject(testObject)); } - const shouldAppend = - key === Severity.WARNINGS - ? VestTest.isWarning(testObject) - : VestTest.isFailing(testObject); + if (VestTest.isPending(testObject)) { + summary.pendingCount++; + } - if (shouldAppend) { - return failures.concat(SummaryFailure.fromTestObject(testObject)); + if (!VestTest.isNonActionable(testObject)) { + summary.testCount++; } - return failures; + + return summary; } function useAppendToTest( tests: Tests, testObject: TIsolateTest, -): Tests { +): SingleTestSummary { const fieldName = VestTest.getData(testObject).fieldName; - const newTests = { - ...tests, - }; - - newTests[fieldName] = appendTestObject(newTests[fieldName], testObject); + const test = appendTestObject(tests[fieldName], testObject); // If `valid` is false to begin with, keep it that way. Otherwise, assess. - newTests[fieldName].valid = - newTests[fieldName].valid === false - ? false - : useShouldAddValidProperty(fieldName); + test.valid = + test.valid === false ? false : useShouldAddValidProperty(fieldName); - return newTests; + return test; } /** @@ -98,37 +96,16 @@ function useAppendToGroup( return groups; } - const newGroups = { - ...groups, - }; - - newGroups[groupName] = newGroups[groupName] || {}; - newGroups[groupName][fieldName] = appendTestObject( - newGroups[groupName][fieldName], - testObject, - ); + groups[groupName] = groups[groupName] || {}; + const group = groups[groupName]; + group[fieldName] = appendTestObject(group[fieldName], testObject); - newGroups[groupName][fieldName].valid = - newGroups[groupName][fieldName].valid === false + group[fieldName].valid = + group[fieldName].valid === false ? false : useShouldAddValidPropertyInGroup(groupName, fieldName); - return newGroups; -} - -/** - * Counts the failed tests and adds global counters - */ -function countOverallStates( - summary: SuiteSummary, -): SuiteSummary { - for (const test in summary.tests) { - summary.errorCount += summary.tests[test].errorCount; - summary.warnCount += summary.tests[test].warnCount; - summary.testCount += summary.tests[test].testCount; - summary.pendingCount += summary.tests[test].pendingCount; - } - return summary; + return groups; } /**