Skip to content

Commit

Permalink
fix: added safeguard to async test inference (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jul 23, 2020
1 parent 7978443 commit bb6cc1d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/n4s/dist/ensure.js

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

2 changes: 1 addition & 1 deletion packages/n4s/dist/extended/enforce.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/n4s/dist/extended/ensure.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/n4s/dist/n4s.js

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

4 changes: 1 addition & 3 deletions packages/n4s/src/lib/throwError/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function throwError(message) {
setTimeout(() => {
throw new Error(`[${LIBRARY_NAME}]: ${message}`);
});
throw new Error(`[${LIBRARY_NAME}]: ${message}`);
}

export default throwError;
17 changes: 14 additions & 3 deletions packages/vest/src/core/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ const register = testObject => {
// If a promise is returned, set as async and
// Move to pending list.
const result = sync(testObject);
if (typeof result?.then === 'function') {
testObject.asyncTest = result;
setPending(testObject.suiteId, testObject);

try {
// try catch for safe property access
// in case object is an enforce chain
if (typeof result?.then === 'function') {
testObject.asyncTest = result;
setPending(testObject.suiteId, testObject);
}
} catch {
/* FUTURE: throw an error here in dev mode:
* Your test function ${testObject.fieldName} returned
* a value other than `false` or a Promise. Return values
* are not supported and may cause unexpected behavior.
*/
}
};

Expand Down
43 changes: 29 additions & 14 deletions packages/vest/src/core/test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,55 @@ import faker from 'faker';
import runSpec from '../../../testUtils/runSpec';

runSpec(vest => {
const { validate, test } = vest;
let testObject;

const { validate, test, enforce } = vest;
describe("Test Vest's `test` function", () => {
describe('test callbacks', () => {
describe('Warn hook', () => {
it('Should be marked as warning when the warn hook gets called', () => {
validate(faker.random.word(), () => {
const testObject = test(
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
vest.warn();
}
);
expect(testObject.isWarning).toBe(true);
});
expect(testObject.isWarning).toBe(true);
});
});

describe('Sync', () => {
it('Should be marked as failed after a thrown error', () => {
validate(faker.random.word(), () => {
const testObject = test(
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => {
throw new Error();
}
);
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});

it('Should be marked as failed for an explicit false return', () => {
validate(faker.random.word(), () => {
const testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() => false
);
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
test(faker.random.word(), faker.lorem.sentence(), () => false);
});
expect(testObject.failed).toBe(true);
expect(testObject == false).toBe(true); //eslint-disable-line
});
});

describe('async', () => {
it('Should be marked as failed when a returned promise rejects', () =>
new Promise(done => {
validate(faker.random.word(), () => {
const testObject = test(
testObject = test(
faker.random.word(),
faker.lorem.sentence(),
() =>
Expand All @@ -70,5 +68,22 @@ runSpec(vest => {
}));
});
});

describe('error handling', () => {
describe('When enforce is returned (not thrown)', () => {
it('Should continue without failing', () =>
new Promise(done => {
validate('form_with_enforce', () => {
try {
test('field_with_enforce', () =>
enforce('example').isNotEmpty());
done();
} catch {
/* */
}
});
}));
});
});
});
});

0 comments on commit bb6cc1d

Please sign in to comment.