Skip to content

Commit

Permalink
fix(vest): retain lagging list after reset
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 15, 2020
1 parent b187bc5 commit 81f3a4d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
22 changes: 22 additions & 0 deletions packages/vest/docs/result.md
Expand Up @@ -208,3 +208,25 @@ const validationResult = validate(data)
promptUserQuestionnaire(output);
});
```

!> **IMPORTANT** .done calls must not be used conditionally - especially when involving async tests. This might cause unexpected behavior or missed callbacks. Instead, if needed, perform your conditional logic within your callback.

```js
// 🚨 This might not work as expected when working with async validations

if (field === 'username') {
result.done(() => {
/*do something*/
});
}
```

```js
// ✅ Instead, perform your checks within your done callback

result.done(() => {
if (field === 'username') {
/*do something*/
}
});
```
33 changes: 33 additions & 0 deletions packages/vest/src/core/produce/__tests__/produce.test.js
@@ -1,4 +1,5 @@
import _ from 'lodash';
import wait from 'wait';

import collector from '../../../../testUtils/collector';
import testDummy from '../../../../testUtils/testDummy';
Expand Down Expand Up @@ -586,6 +587,38 @@ describe('module: produce', () => {
});
});
});

describe('With lagging run', () => {
const TEST_TIMEOUT = 100;
let suite, cb;
beforeEach(() => {
cb = jest.fn();
suite = vest.create(fieldName => {
vest.only(fieldName);

vest.test('asyncTest', async () => {
await wait(TEST_TIMEOUT);
throw new Error();
});

vest.test('syncTest', () => {});
});
});
it('Should call `done` callback when async test is finished', async () => {
suite('asyncTest').done(cb);
expect(cb).not.toHaveBeenCalled();
await wait(TEST_TIMEOUT);
expect(cb).toHaveBeenCalled();
});

it('Should call `done` callback when async test is lagging', async () => {
suite('asyncTest').done(cb);
suite('syncTest').done(cb);
expect(cb).not.toHaveBeenCalled();
await wait(TEST_TIMEOUT);
expect(cb).toHaveBeenCalled();
});
});
});
describe('method: getErrorsByGroup', () => {
let res, validate;
Expand Down
8 changes: 8 additions & 0 deletions packages/vest/src/core/suite/createSuite.js
Expand Up @@ -41,10 +41,18 @@ const createSuite = withArgs(args => {
return Object.defineProperties(
context.bind({ stateRef }, function () {
const [previousTestObjects] = useTestObjects();
const [{ pending }, setPending] = usePending();
stateRef.reset();

// Move all the active pending tests to the lagging array
setPending({ lagging: pending, pending: [] });

// Run the consumer's callback
tests.apply(null, arguments);

// Merge all the skipped tests with their previous results
mergeExcludedTests(previousTestObjects);

return produce();
}),
{
Expand Down
Expand Up @@ -87,13 +87,12 @@ Object {
],
"pending": Array [
VestTest {
"canceled": true,
"failed": false,
"fieldName": "test_0",
"groupName": "group_name",
"id": "10",
"id": "15",
"isWarning": false,
"statement": "Some statement string",
"statement": "failure message",
"testFn": [MockFunction],
},
],
Expand Down
18 changes: 16 additions & 2 deletions packages/vest/src/core/test/lib/__tests__/pending.test.js
Expand Up @@ -137,9 +137,16 @@ describe('module: pending', () => {
const [pendingState] = usePending();
expect(pendingState.lagging).toContain(testObjects[0]);
}
setPending(testObjects[0]);
const added = new VestTest({
fieldName: testObjects[0].fieldName,
group: testObjects[0].groupName,
statement: 'failure message',
testFn: jest.fn(),
});
setPending(added);
{
const [pendingState] = usePending();
expect(pendingState.pending).toContain(added);
expect(pendingState.lagging).not.toContain(testObjects[0]);
}
expect(stateRef.current()).toMatchSnapshot();
Expand All @@ -159,7 +166,14 @@ describe('module: pending', () => {

it.ctx('Should set test as canceled', () => {
expect(testObjects[0].canceled).toBeUndefined();
setPending(testObjects[0]);
setPending(
new VestTest({
fieldName: testObjects[0].fieldName,
group: testObjects[0].groupName,
statement: 'failure message',
testFn: jest.fn(),
})
);
expect(testObjects[0].canceled).toBe(true);
});
});
Expand Down
7 changes: 5 additions & 2 deletions packages/vest/src/core/test/lib/pending.js
Expand Up @@ -6,7 +6,7 @@ import usePending from 'usePending';
* @param {VestTest} testObject
*/
export const setPending = testObject => {
const { fieldName, groupName } = testObject;
const { fieldName, groupName, id } = testObject;

const [pendingState, setPending] = usePending();

Expand All @@ -18,7 +18,10 @@ export const setPending = testObject => {
*/
if (
testObject.fieldName === fieldName &&
testObject.groupName === groupName
testObject.groupName === groupName &&
// This last case handles memoized tests
// because that retain their od across runs
testObject.id !== id
) {
testObject.cancel();
} else {
Expand Down

0 comments on commit 81f3a4d

Please sign in to comment.