Skip to content

Commit

Permalink
patch(vest): getFailures uses static summary object
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 28, 2022
1 parent 31727a0 commit 742d340
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
13 changes: 11 additions & 2 deletions packages/vest/src/core/suite/produce/__tests__/done.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('done', () => {
});

describe('When suite lags and callbacks are registered again', () => {
it('It should only run most recent registered callbacks', async () => {
it('should only run most recent registered callbacks', async () => {
const test = [];
const suite = vest.create(() => {
test.push(dummyTest.failingAsync('test', { time: 100 }));
Expand Down Expand Up @@ -105,7 +105,13 @@ describe('done', () => {

result
.done('field_2', res => {
expect(res.getErrors()).toEqual({ field_1: ['error message'] });
expect(res.getErrors()).toEqual({
field_1: ['error message'],
field_2: [],
field_3: [],
field_4: [],
field_5: [],
});
expect(res).toMatchObject({
errorCount: 1,
groups: {},
Expand Down Expand Up @@ -180,7 +186,10 @@ describe('done', () => {
.done('field_4', res => {
expect(res.getErrors()).toEqual({
field_1: ['error message'],
field_2: [],
field_3: [],
field_4: ['error_message'],
field_5: [],
});
expect(res).toMatchObject({
errorCount: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ describe.each(Object.keys(methods))('produce method: %s', methodName => {
field_1: ['message', 'failure_message', 'failure_message with group'],
field_2: [],
field_3: ['msg'],
field_4: [],
field_5: [],
});
expect(produceMethod().getWarnings()).toEqual({
field_1: [],
field_2: ['warning test', 'another warning test'],
field_3: [],
field_4: [],
field_5: ['warning message'],
});
expect(produceMethod().getErrors()).toEqual({
field_1: ['message', 'failure_message', 'failure_message with group'],
field_2: [],
field_3: ['msg'],
field_4: [],
field_5: [],
});
expect(produceMethod().hasErrorsByGroup('group_1')).toBe(true);
expect(produceMethod().hasErrorsByGroup('group_1', 'field_1')).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { Severity } from 'Severity';
import { TestGroup } from 'genTestsSummary';

export function getByFieldName(
// calls collectAll or getByFieldName depending on whether fieldName is provided

export function gatherFailures(
testGroup: TestGroup,
severityKey: Severity,
fieldName?: string
): string[] | Record<string, string[]> {
return fieldName
? getByFieldName(testGroup, severityKey, fieldName)
: collectAll(testGroup, severityKey);
}

function getByFieldName(
testGroup: TestGroup,
severityKey: Severity,
fieldName: string
): string[] {
return testGroup?.[fieldName]?.[severityKey] || [];
}

export function collectAll(
function collectAll(
testGroup: TestGroup,
severityKey: Severity
): Record<string, string[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import invariant from 'invariant';

import { Severity } from 'Severity';
import { getByFieldName, collectAll } from 'collectFailures';
import { gatherFailures } from 'collectFailures';
import ctx from 'ctx';

export function getErrors(): Record<string, string[]>;
Expand All @@ -26,7 +26,6 @@ export function getWarnings(
function getFailures(severityKey: Severity, fieldName?: string) {
const { summary } = ctx.useX();
invariant(summary);
return fieldName
? getByFieldName(summary.tests, severityKey, fieldName)
: collectAll(summary.tests, severityKey);

return gatherFailures(summary.tests, severityKey, fieldName);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import invariant from 'invariant';

import { Severity } from 'Severity';
import { collectAll, getByFieldName } from 'collectFailures';
import { gatherFailures } from 'collectFailures';
import ctx from 'ctx';
// import getFailuresArrayOrObject from 'getFailuresArrayOrObject';
// import { useTestsFlat } from 'stateHooks';

export function getErrorsByGroup(groupName: string): Record<string, string[]>;
export function getErrorsByGroup(
Expand All @@ -15,12 +13,7 @@ export function getErrorsByGroup(
groupName: string,
fieldName?: string
): string[] | Record<string, string[]> {
const { summary } = ctx.useX();
invariant(summary);

return fieldName
? getByFieldName(summary.groups[groupName], Severity.ERRORS, fieldName)
: collectAll(summary.groups[groupName], Severity.ERRORS);
return getFailuresByGroup(groupName, Severity.ERRORS, fieldName);
}

export function getWarningsByGroup(groupName: string): Record<string, string[]>;
Expand All @@ -31,11 +24,17 @@ export function getWarningsByGroup(
export function getWarningsByGroup(
groupName: string,
fieldName?: string
): string[] | Record<string, string[]> {
return getFailuresByGroup(groupName, Severity.WARNINGS, fieldName);
}

function getFailuresByGroup(
groupName: string,
severityKey: Severity,
fieldName?: string
): string[] | Record<string, string[]> {
const { summary } = ctx.useX();
invariant(summary);

return fieldName
? getByFieldName(summary.groups[groupName], Severity.WARNINGS, fieldName)
: collectAll(summary.groups[groupName], Severity.WARNINGS);
return gatherFailures(summary.groups[groupName], severityKey, fieldName);
}
6 changes: 0 additions & 6 deletions tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 742d340

Please sign in to comment.