Skip to content

Commit

Permalink
patch(vest): support suite summary in parser module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 20, 2022
1 parent d648cb5 commit 67e698b
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 20 deletions.
224 changes: 224 additions & 0 deletions packages/vest/src/exports/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as suiteDummy from '../../../testUtils/suiteDummy';
import { ser } from '../../../testUtils/suiteDummy';

import { parse } from 'parser';
import * as vest from 'vest';
Expand All @@ -24,6 +25,28 @@ describe('parser.parse', () => {
false
);
});

describe('Serialized Result', () => {
it('Should return true when provided suite result is failing and no field name is provided', () => {
expect(parse(ser(suiteDummy.failing())).invalid()).toBe(true);
});

it('Should return false when provided suite result is passing and no field name is provided', () => {
expect(parse(ser(suiteDummy.passing())).invalid()).toBe(false);
});

it('Should return true when provided field is failing', () => {
expect(
parse(ser(suiteDummy.failing('username'))).invalid('username')
).toBe(true);
});

it('Should return false when provided field is passing', () => {
expect(
parse(ser(suiteDummy.passing('username'))).invalid('username')
).toBe(false);
});
});
});

describe('parse().tested', () => {
Expand All @@ -42,6 +65,24 @@ describe('parser.parse', () => {
true
);
});

describe('Serialized Result', () => {
it('Should return true if any field is tested but no field is provided', () => {
expect(parse(ser(suiteDummy.passing())).tested()).toBe(true);
});
it('Should return true if no field is tested', () => {
expect(parse(ser(suiteDummy.untested())).tested()).toBe(false);
expect(parse(ser(suiteDummy.untested())).tested('field')).toBe(false);
});
it('Should return true if provided field is tested', () => {
expect(
parse(ser(suiteDummy.passing('username'))).tested('username')
).toBe(true);
expect(
parse(ser(suiteDummy.failing('username'))).tested('username')
).toBe(true);
});
});
});

describe('parse().untested', () => {
Expand Down Expand Up @@ -72,6 +113,39 @@ describe('parser.parse', () => {
false
);
});

describe('Serialized Result', () => {
it('Should return true if no field is tested', () => {
expect(parse(ser(suiteDummy.untested())).untested()).toBe(true);
});

it('Should return true if provided field is untested while others are', () => {
expect(
parse(
ser(
vest.create(() => {
vest.test('x', () => {});
vest.skipWhen(true, () => {
vest.test('untested', () => {});
});
})()
)
).untested('untested')
).toBe(true);
});

it('Should return false if any field is tested', () => {
expect(parse(ser(suiteDummy.passing('username'))).untested()).toBe(
false
);
});

it('Should return false if provided field is tested', () => {
expect(
parse(ser(suiteDummy.passing('username'))).untested('username')
).toBe(false);
});
});
});

describe('parse().valid', () => {
Expand Down Expand Up @@ -194,6 +268,146 @@ describe('parser.parse', () => {
).toBe(true);
});
});

describe('Serialized Result', () => {
it('Should return true if all fields are passing', () => {
expect(parse(ser(suiteDummy.passing(['f1', 'f2', 'f3']))).valid()).toBe(
true
);
expect(
parse(ser(suiteDummy.passing(['f1', 'f2', 'f3']))).valid('f2')
).toBe(true);
});

it('Should return true if all required fields have been tested and are passing', () => {
expect(
parse(ser(suiteDummy.passingWithUntestedOptional('optional'))).valid()
).toBe(true);
});

it('Should return true if all fields, including optional, pass', () => {
expect(
parse(ser(suiteDummy.passingWithOptional('optional'))).valid()
).toBe(true);
});

it('Should return false if suite has errors', () => {
expect(parse(ser(suiteDummy.failing())).valid()).toBe(false);
});

it('Should return false if suite has failing optional tests', () => {
expect(parse(ser(suiteDummy.failingOptional())).valid()).toBe(false);
});

it('Should return true if suite only has warnings', () => {
expect(parse(ser(suiteDummy.warning(['f1', 'f2', 'f3']))).valid()).toBe(
true
);
});

it('Should return false if no tests ran', () => {
expect(parse(ser(suiteDummy.untested())).valid()).toBe(false);
});

it('should return false if not all required fields ran', () => {
expect(
parse(
ser(
vest.create(() => {
vest.test('x', () => {});
vest.test('untested', () => {});
vest.skipWhen(true, () => {
vest.test('untested', () => {});
});
})()
)
).valid()
).toBe(false);
});

describe('With field name', () => {
it('Should return false when field is untested', () => {
expect(
parse(
ser(
vest.create(() => {
vest.skipWhen(true, () => {
vest.test('f1', () => {});
});
})()
)
).valid('f1')
).toBe(false);
});

it('Should return true if optional field is untested', () => {
expect(
parse(
ser(
vest.create(() => {
vest.optional('f1');
vest.skipWhen(true, () => {
vest.test('f1', () => {});
});
})()
)
).valid('f1')
).toBe(true);
});

it('Should return true if field is passing', () => {
expect(
parse(
ser(
vest.create(() => {
vest.test('f1', () => {});
vest.test('f2', () => false);
})()
)
).valid('f1')
).toBe(true);
});

it('Should return false if field is failing', () => {
expect(
parse(
ser(
vest.create(() => {
vest.test('f1', () => {});
vest.test('f2', () => false);
})()
)
).valid('f2')
).toBe(false);
expect(
parse(
ser(
vest.create(() => {
vest.test('f1', () => {});
vest.test('f2', () => {});
vest.test('f2', () => false);
})()
)
).valid('f2')
).toBe(false);
});

it('Should return true if field is warning', () => {
expect(
parse(
ser(
vest.create(() => {
vest.test('f1', () => {
vest.warn();
return false;
});
})()
)
).valid('f1')
).toBe(true);
});
});
});
});

describe('parse().warning', () => {
Expand All @@ -204,5 +418,15 @@ describe('parser.parse', () => {
it('Should return false when the suite is not warnings', () => {
expect(parse(suiteDummy.failing()).warning()).toBe(false);
});

describe('serialized result', () => {
it('Should return true when the suite has warnings', () => {
expect(parse(ser(suiteDummy.warning())).warning()).toBe(true);
});

it('Should return false when the suite is not warnings', () => {
expect(parse(ser(suiteDummy.failing())).warning()).toBe(false);
});
});
});
});
72 changes: 52 additions & 20 deletions packages/vest/src/exports/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { greaterThan } from 'greaterThan';
import hasOwnProperty from 'hasOwnProperty';

import { SeverityCount } from 'Severity';
import { SuiteSummary } from 'genTestsSummary';
import type { SuiteResult } from 'produceSuiteResult';
import type { SuiteRunResult } from 'produceSuiteRunResult';

export function parse(res: SuiteRunResult | SuiteResult): {
// eslint-disable-next-line max-lines-per-function
export function parse(res: SuiteRunResult | SuiteResult | SuiteSummary): {
valid: (fieldName?: string) => boolean;
tested: (fieldName?: string) => boolean;
invalid: (fieldName?: string) => boolean;
Expand All @@ -14,26 +17,55 @@ export function parse(res: SuiteRunResult | SuiteResult): {
const testedStorage: Record<string, boolean> = {};

const selectors = {
invalid: res.hasErrors,
tested: (fieldName?: string): boolean => {
if (!fieldName) {
return greaterThan(res.testCount, 0);
}

if (hasOwnProperty(testedStorage, fieldName))
return testedStorage[fieldName];

testedStorage[fieldName] =
hasOwnProperty(res.tests, fieldName) &&
greaterThan(res.tests[fieldName].testCount, 0);

return selectors.tested(fieldName);
},
untested: (fieldName?: string): boolean =>
res.testCount === 0 || !selectors.tested(fieldName),
valid: res.isValid,
warning: res.hasWarnings,
invalid: hasErrors,
tested: isTested,
untested: isUntested,
valid: isValid,
warning: hasWarnings,
};

return selectors;

function isTested(fieldName?: string): boolean {
if (!fieldName) {
return greaterThan(res.testCount, 0);
}

if (hasOwnProperty(testedStorage, fieldName))
return testedStorage[fieldName];

testedStorage[fieldName] =
hasOwnProperty(res.tests, fieldName) &&
greaterThan(res.tests[fieldName].testCount, 0);

return selectors.tested(fieldName);
}

function isUntested(fieldName?: string): boolean {
return res.testCount === 0 || !selectors.tested(fieldName);
}

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

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

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

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

return greaterThan(failureCount, 0);
}
5 changes: 5 additions & 0 deletions packages/vest/testUtils/suiteDummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import asArray from '../../shared/src/asArray';

import { dummyTest } from './testDummy';

import type { SuiteResult } from 'vest';
import { optional, create, skip } from 'vest';

export function failing(failingFields?: string | string[]) {
Expand Down Expand Up @@ -102,3 +103,7 @@ function createSuite(
asArray(fieldNames).forEach(fieldName => callback(fieldName));
});
}

export function ser(res: SuiteResult) {
return JSON.parse(JSON.stringify(res));
}

0 comments on commit 67e698b

Please sign in to comment.