From 5eda549dfce95be225b0eb6dbe3cbe7bcd5d3347 Mon Sep 17 00:00:00 2001 From: Daniel Rearden Date: Sun, 4 Apr 2021 16:40:33 -0400 Subject: [PATCH] fix: prevent unhandled Promise rejection when returning rejected Promise inside test function (#1327) --- src/util/createValidation.ts | 2 +- test/mixed.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/util/createValidation.ts b/src/util/createValidation.ts index a6cefefee..3df3728be 100644 --- a/src/util/createValidation.ts +++ b/src/util/createValidation.ts @@ -120,7 +120,7 @@ export default function createValidation(config: { if (ValidationError.isError(validOrError)) cb(validOrError); else if (!validOrError) cb(createError()); else cb(null, validOrError); - }); + }).catch(cb); } catch (err) { cb(err); } diff --git a/test/mixed.js b/test/mixed.js index 01ae82a8b..f422405ae 100644 --- a/test/mixed.js +++ b/test/mixed.js @@ -529,6 +529,16 @@ describe('Mixed Types ', () => { }); }); + it('should fail when the test function returns a rejected Promise', async () => { + let inst = string().test(() => { + return Promise.reject(new Error('oops an error occurred')) + }); + + return inst + .validate('joe') + .should.be.rejected(); + }); + describe('withMutation', () => { it('should pass the same instance to a provided function', () => { let inst = mixed();