Skip to content

Commit

Permalink
patch(vest): simplify hasFailures
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Apr 1, 2022
1 parent 9d6a435 commit 4dd8e5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
@@ -1,21 +1,22 @@
import { isPositive } from 'isPositive';

import { countKeyBySeverity, Severity } from 'Severity';
import { SeverityCount } from 'Severity';
import { useSummary } from 'genTestsSummary';

export function hasErrors(fieldName?: string): boolean {
return hasFailures(Severity.ERRORS, fieldName);
return hasFailures(SeverityCount.ERROR_COUNT, fieldName);
}

export function hasWarnings(fieldName?: string): boolean {
return hasFailures(Severity.WARNINGS, fieldName);
return hasFailures(SeverityCount.WARN_COUNT, fieldName);
}

function hasFailures(severityKey: Severity, fieldName?: string): boolean {
function hasFailures(
severityCount: SeverityCount,
fieldName?: string
): boolean {
const summary = useSummary();

const severityCount = countKeyBySeverity(severityKey);

if (fieldName) {
return isPositive(summary.tests[fieldName]?.[severityCount]);
}
Expand Down
24 changes: 11 additions & 13 deletions packages/vest/src/exports/parser.ts
@@ -1,3 +1,4 @@
import defaultTo from 'defaultTo';
import hasOwnProperty from 'hasOwnProperty';
import invariant from 'invariant';
import { isPositive } from 'isPositive';
Expand Down Expand Up @@ -50,26 +51,23 @@ export function parse(res: SuiteSummary): {
}

function isValid(fieldName?: string): boolean {
return Boolean(fieldName ? res.tests?.[fieldName]?.valid : res.valid);
return fieldName ? Boolean(res.tests[fieldName]?.valid) : res.valid;
}

function hasWarnings(fieldName?: string): boolean {
return hasFailures(res, SeverityCount.WARN_COUNT, fieldName);
return hasFailures(SeverityCount.WARN_COUNT, fieldName);
}

function hasErrors(fieldName?: string): boolean {
return hasFailures(res, SeverityCount.ERROR_COUNT, fieldName);
return hasFailures(SeverityCount.ERROR_COUNT, fieldName);
}
}

function hasFailures(
res: SuiteSummary,
countKey: SeverityCount,
fieldName?: string
): boolean {
const failureCount = fieldName
? res.tests?.[fieldName]?.[countKey]
: res[countKey] ?? 0;
function hasFailures(countKey: SeverityCount, fieldName?: string): boolean {
const failureCount = defaultTo(
fieldName ? res.tests[fieldName]?.[countKey] : res[countKey],
0
);

return isPositive(failureCount);
return isPositive(failureCount);
}
}

0 comments on commit 4dd8e5e

Please sign in to comment.