From 9c04a7cf28e679b5eb627738e18182554b8ea057 Mon Sep 17 00:00:00 2001 From: In Date: Fri, 21 Jun 2019 15:27:29 +0800 Subject: [PATCH] Fix default `retry` option value when specifying a number (#809) Co-authored-by: Szymon Marczak --- source/merge.ts | 19 ++++++++++++++++++- test/retry.ts | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/source/merge.ts b/source/merge.ts index 069c9a25a..e9882c966 100644 --- a/source/merge.ts +++ b/source/merge.ts @@ -43,7 +43,24 @@ export default function merge, Source ext } export function mergeOptions(...sources: T[]): T & {hooks: Partial} { - const mergedOptions = merge({} as T & {hooks: Partial}, ...sources.map(source => source || {})); + sources = sources.map(source => { + if (!source) { + return {}; + } + + if (is.object(source.retry)) { + return source; + } + + return { + ...source, + retry: { + retries: source.retry + } + }; + }) as T[]; + + const mergedOptions = merge({} as T & {hooks: Partial}, ...sources); const hooks = knownHookEvents.reduce((accumulator, current) => ({...accumulator, [current]: []}), {}) as Record; diff --git a/test/retry.ts b/test/retry.ts index dd6ae63d9..e89a60fb0 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -274,7 +274,7 @@ test('doesn\'t retry when set to false', withServer, async (t, server, got) => { t.is(retryCount, 0); }); -test('works when defaults.options.retry is not an object', withServer, async (t, server, got) => { +test('works when defaults.options.retry is a number', withServer, async (t, server, got) => { server.get('/', handler413); const instance = got.extend({ @@ -284,7 +284,7 @@ test('works when defaults.options.retry is not an object', withServer, async (t, const {retryCount} = await instance({ throwHttpErrors: false }); - t.is(retryCount, 0); + t.is(retryCount, 2); }); test('retry function can throw', withServer, async (t, server, got) => {