diff --git a/source/create.ts b/source/create.ts index 7037cf223..46ea7501d 100644 --- a/source/create.ts +++ b/source/create.ts @@ -54,10 +54,10 @@ const create = (defaults: InstanceDefaults): Got => { const lastHandler = (normalized: Options): GotReturn => { // Note: `options` is `undefined` when `new Options(...)` fails request.options = normalized; - request._noPipe = !normalized.isStream; + request._noPipe = !normalized?.isStream; void request.flush(); - if (normalized.isStream) { + if (normalized?.isStream) { return request; } @@ -72,7 +72,7 @@ const create = (defaults: InstanceDefaults): Got => { const result = handler(newOptions, iterateHandlers) as GotReturn; - if (is.promise(result) && !request.options.isStream) { + if (is.promise(result) && !request.options?.isStream) { promise ||= asPromise(request); if (result !== promise) { diff --git a/test/arguments.ts b/test/arguments.ts index 41b7034e2..b732ddb42 100644 --- a/test/arguments.ts +++ b/test/arguments.ts @@ -42,6 +42,16 @@ test('throws if the url option is missing', async t => { }); }); +test('throws if an invalid argument is passed', async t => { + await t.throwsAsync( + // @ts-expect-error Error tests + got(false), + { + instanceOf: RequestError, + message: 'Expected values which are `string`, `URL`, `Object`, or `undefined`. Received values of type `boolean`.', + }); +}); + test('throws an error if the protocol is not specified', async t => { const error = await t.throwsAsync(got('example.com')); invalidUrl(t, error!, 'example.com');