Skip to content

Commit

Permalink
patch(vest): prevent skipped tests from persisting after override
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 11, 2021
1 parent c0180d1 commit d53d56d
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Stateful async tests', () => {
expect(res).toMatchSnapshot();
setTimeout(() => {
const res = validate.get();

expect(res.testCount).toBe(3);
expect(res.errorCount).toBe(2);
expect(res.warnCount).toBe(0);
Expand Down
40 changes: 25 additions & 15 deletions packages/vest/src/core/produce/genTestsSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,47 @@ import {
SEVERITY_GROUP_ERROR,
TEST_COUNT,
} from 'resultKeys';
import { useSuiteId , useTestObjects } from 'stateHooks';

import { useSuiteId, useTestObjects, useSkippedTests } from 'stateHooks';

/**
* Reads the testObjects list and gets full validation result from it.
*/
const genTestsSummary = () => {
const [testObjects] = useTestObjects();
const [suiteIdState] = useSuiteId();
const [skippedTests] = useSkippedTests();

const summary = {
tests: {},
groups: {},
name: suiteIdState.name,
};

testObjects.forEach(testObject => {
const { fieldName, groupName } = testObject;
appendSummary(testObjects);
appendSummary(skippedTests, true);

return countFailures(summary);

summary.tests[fieldName] = genTestObject(summary.tests, testObject);
function appendSummary(testObject, skipped) {
testObject.forEach(testObject => {
const { fieldName, groupName } = testObject;

if (groupName) {
summary.groups[groupName] = summary.groups[groupName] || {};
summary.groups[groupName][fieldName] = genTestObject(
summary.groups[groupName],
testObject
summary.tests[fieldName] = genTestObject(
summary.tests,
testObject,
skipped
);
}
});

return countFailures(summary);
if (groupName) {
summary.groups[groupName] = summary.groups[groupName] || {};
summary.groups[groupName][fieldName] = genTestObject(
summary.groups[groupName],
testObject,
skipped
);
}
});
}
};

/**
Expand Down Expand Up @@ -63,8 +73,8 @@ export default genTestsSummary;
* @param {VestTest} testObject
* @returns {Object} Test result summary
*/
const genTestObject = (summaryKey, testObject) => {
const { fieldName, isWarning, failed, statement, skipped } = testObject;
const genTestObject = (summaryKey, testObject, skipped) => {
const { fieldName, isWarning, failed, statement } = testObject;

summaryKey[fieldName] = summaryKey[fieldName] || {
[SEVERITY_COUNT_ERROR]: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/vest/src/core/state/createStateRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default function createStateRef(state, { suiteId, name }) {
pending: [],
lagging: [],
})),
skippedTests: state.registerStateKey(() => []),
suiteId: state.registerStateKey(() => ({ id: suiteId, name })),
testCallbacks: state.registerStateKey(() => ({
fieldCallbacks: [],
Expand Down
3 changes: 3 additions & 0 deletions packages/vest/src/core/state/stateHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ export function useTestCallbacks() {
export function useTestObjects() {
return getStateRef().testObjects();
}
export function useSkippedTests() {
return getStateRef().skippedTests();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`suite.subscribe Should call handler on suite subscription initializatio
Object {
"suiteState": Object {
"pending": [Function],
"skippedTests": [Function],
"suiteId": [Function],
"testCallbacks": [Function],
"testObjects": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Object {
"fieldName": "field_1",
"id": "0",
"isWarning": false,
"skipped": false,
"statement": "some statement string",
"testFn": [Function],
},
],
},
"skippedTests": Array [],
"suiteId": Object {
"id": 1000,
"name": "suite_name",
Expand Down
6 changes: 0 additions & 6 deletions packages/vest/src/core/test/lib/VestTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ function VestTest({ fieldName, statement, testFn, group }) {
testFn,
});

this.skipped = false;

if (group) {
this.groupName = group;
}
Expand Down Expand Up @@ -57,8 +55,4 @@ VestTest.prototype.cancel = function () {
removeTestFromState(this);
};

VestTest.prototype.markSkipped = function () {
this.skipped = true;
};

export default VestTest;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ VestTest {
"fieldName": "unicycle",
"id": "0",
"isWarning": false,
"skipped": false,
"statement": "I am Root.",
"testFn": [MockFunction],
}
Expand All @@ -18,7 +17,6 @@ VestTest {
"fieldName": "unicycle",
"id": "102",
"isWarning": true,
"skipped": false,
"statement": "I am Root.",
"testFn": [MockFunction],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object {
"lagging": Array [],
"pending": Array [],
},
"skippedTests": Array [],
"suiteId": Object {
"id": 1000,
"name": "suite_name",
Expand All @@ -24,6 +25,7 @@ Object {
"lagging": Array [],
"pending": Array [],
},
"skippedTests": Array [],
"suiteId": Object {
"id": 1000,
"name": "suite_name",
Expand All @@ -47,12 +49,12 @@ Object {
"groupName": "group_name",
"id": "4",
"isWarning": false,
"skipped": false,
"statement": "Some statement string",
"testFn": [MockFunction],
},
],
},
"skippedTests": Array [],
"suiteId": Object {
"id": 1000,
"name": "suite_name",
Expand All @@ -74,7 +76,6 @@ Object {
"fieldName": "test_2",
"id": "12",
"isWarning": false,
"skipped": false,
"statement": "Some statement string",
"testFn": [MockFunction],
},
Expand All @@ -83,7 +84,6 @@ Object {
"fieldName": "test_1",
"id": "11",
"isWarning": false,
"skipped": false,
"statement": "Some statement string",
"testFn": [MockFunction],
},
Expand All @@ -95,12 +95,12 @@ Object {
"groupName": "group_name",
"id": "15",
"isWarning": false,
"skipped": false,
"statement": "failure message",
"testFn": [MockFunction],
},
],
},
"skippedTests": Array [],
"suiteId": Object {
"id": 1000,
"name": "suite_name",
Expand Down
14 changes: 8 additions & 6 deletions packages/vest/src/core/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import isStringValue from 'isStringValue';
import { isUndefined } from 'isUndefined';
import { setPending } from 'pending';
import runAsyncTest from 'runAsyncTest';
import { useSkippedTests } from 'stateHooks';
import bindTestEach from 'test.each';
import bindTestMemo from 'test.memo';
import throwError from 'throwError';
Expand Down Expand Up @@ -44,11 +45,6 @@ const sync = testObject =>
const register = testObject => {
addTestToState(testObject);

if (isExcluded(testObject)) {
testObject.markSkipped();
return;
}

// Run test callback.
// If a promise is returned, set as async and
// Move to pending list.
Expand Down Expand Up @@ -85,6 +81,7 @@ const register = testObject => {
*/
function test(fieldName, args) {
const [testFn, statement] = args.reverse();
const [, setSkippedTests] = useSkippedTests();

const { groupName } = context.use();
const testObject = new VestTest({
Expand All @@ -94,8 +91,13 @@ function test(fieldName, args) {
testFn,
});

if (isExcluded(testObject)) {
setSkippedTests(skippedTests => skippedTests.concat(testObject));
return testObject;
}

if (!isFunction(testFn)) {
return;
return testObject;
}

register(testObject);
Expand Down
9 changes: 1 addition & 8 deletions packages/vest/src/hooks/exclusive.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ skip.group = item =>
* @returns {Boolean}
*/
const isExcluded = testObject => {
const { fieldName, groupName, skipped } = testObject;

// this only happens after the test registration
// first time this gets registered, and in the follow
// up runs we can bail quickly
if (skipped) {
return true;
}
const { fieldName, groupName } = testObject;

const { exclusion } = context.use();

Expand Down

0 comments on commit d53d56d

Please sign in to comment.