From 4f21eb3db705c90797ef8ee1503704f78fea3c1b Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 15 Apr 2021 15:14:46 +0200 Subject: [PATCH] Throw when `afterResponse` hook returns an invalid value Fixes #1569 --- source/as-promise/index.ts | 4 ++++ test/hooks.ts | 15 +++++++++++++++ test/pagination.ts | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index 86f0e250a..049aab0e3 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -99,6 +99,10 @@ export default function asPromise(firstRequest: Request): CancelableRequest }); }); +test('throws if afterResponse returns an invalid value', withServer, async (t, server, got) => { + server.get('/', echoHeaders); + + await t.throwsAsync(got('', { + hooks: { + afterResponse: [ + // @ts-expect-error + () => {} + ] + } + }), { + message: 'The `afterResponse` hook returned an invalid value' + }); +}); + test('catches afterResponse thrown errors', withServer, async (t, server, got) => { server.get('/', echoHeaders); diff --git a/test/pagination.ts b/test/pagination.ts index 44913302c..a5bb36053 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -716,7 +716,7 @@ test('calls init hooks on pagination', withServer, async (t, server) => { test('throws when transform does not return an array', withServer, async (t, server) => { server.get('/', (_request, response) => { - response.end(JSON.stringify("")); + response.end(JSON.stringify('')); }); await t.throwsAsync(got.paginate.all(server.url));