Skip to content

Commit 4cdcca3

Browse files
IswaryaSIswarya Sankaran
and
Iswarya Sankaran
authored
Check error instance for arguments test (#2044)
Co-authored-by: Iswarya Sankaran <[email protected]>
1 parent 22d58fb commit 4cdcca3

File tree

5 files changed

+125
-34
lines changed

5 files changed

+125
-34
lines changed

test/arguments.ts

+57-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test('`url` is required', async t => {
1515
// @ts-expect-error No argument on purpose.
1616
got(),
1717
{
18+
instanceOf: RequestError,
1819
message: 'Missing `url` property',
1920
},
2021
);
@@ -30,6 +31,7 @@ test('`url` should be utf-8 encoded', async t => {
3031
await t.throwsAsync(
3132
got('https://example.com/%D2%E0%EB%EB%E8%ED'),
3233
{
34+
instanceOf: RequestError,
3335
message: 'URI malformed',
3436
},
3537
);
@@ -38,12 +40,14 @@ test('`url` should be utf-8 encoded', async t => {
3840
test('throws if no arguments provided', async t => {
3941
// @ts-expect-error Error tests
4042
await t.throwsAsync(got(), {
43+
instanceOf: RequestError,
4144
message: 'Missing `url` property',
4245
});
4346
});
4447

4548
test('throws if the url option is missing', async t => {
4649
await t.throwsAsync(got({}), {
50+
instanceOf: RequestError,
4751
message: 'Missing `url` property',
4852
});
4953
});
@@ -93,17 +97,22 @@ test('throws an error when legacy URL is passed', withServer, async (t, server)
9397
await t.throwsAsync(
9498
// @ts-expect-error Error tests
9599
got(parse(`${server.url}/test`)),
100+
{
101+
instanceOf: RequestError,
102+
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Object`.',
103+
},
96104
);
97105

98-
// TODO: Assert message above.
99-
100106
await t.throwsAsync(
101107
got({
102108
protocol: 'http:',
103109
hostname: 'localhost',
104110
port: server.port,
105111
} as any),
106-
{message: 'Unexpected option: protocol'},
112+
{
113+
instanceOf: RequestError,
114+
message: 'Unexpected option: protocol',
115+
},
107116
);
108117
});
109118

@@ -163,6 +172,7 @@ test('ignores empty searchParams object', withServer, async (t, server, got) =>
163172

164173
test('throws when passing body with a non payload method', async t => {
165174
await t.throwsAsync(got('https://example.com', {body: 'asdf'}), {
175+
instanceOf: RequestError,
166176
message: 'The `GET` method cannot be used with a body',
167177
});
168178
});
@@ -206,6 +216,7 @@ test('throws when `options.hooks` is not an object', async t => {
206216
// @ts-expect-error Error tests
207217
got('https://example.com', {hooks: 'not object'}),
208218
{
219+
instanceOf: RequestError,
209220
message: 'Expected value which is `Object`, received value of type `string`.',
210221
},
211222
);
@@ -215,16 +226,19 @@ test('throws when known `options.hooks` value is not an array', async t => {
215226
await t.throwsAsync(
216227
// @ts-expect-error Error tests
217228
got('https://example.com', {hooks: {beforeRequest: {}}}),
229+
{
230+
instanceOf: RequestError,
231+
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Object`.',
232+
},
218233
);
219-
220-
// TODO: Assert message above.
221234
});
222235

223236
test('throws when known `options.hooks` array item is not a function', async t => {
224237
await t.throwsAsync(
225238
// @ts-expect-error Error tests
226239
got('https://example.com', {hooks: {beforeRequest: [{}]}}),
227240
{
241+
instanceOf: RequestError,
228242
message: 'Expected value which is `Function`, received value of type `Object`.',
229243
},
230244
);
@@ -235,6 +249,7 @@ test('does not allow extra keys in `options.hooks`', withServer, async (t, serve
235249

236250
// @ts-expect-error Error tests
237251
await t.throwsAsync(got('test', {hooks: {extra: []}}), {
252+
instanceOf: RequestError,
238253
message: 'Unexpected hook event: extra',
239254
});
240255
});
@@ -286,9 +301,11 @@ test('throws if the `searchParams` value is invalid', async t => {
286301
// @ts-expect-error Error tests
287302
foo: [],
288303
},
289-
}));
290-
291-
// TODO: Assert message above.
304+
}),
305+
{
306+
instanceOf: RequestError,
307+
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Array`.',
308+
});
292309
});
293310

294311
test.failing('`context` option is enumerable', withServer, async (t, server, got) => {
@@ -365,20 +382,29 @@ test('throws if `options.encoding` is `null`', async t => {
365382
await t.throwsAsync(got('https://example.com', {
366383
// @ts-expect-error For testing purposes
367384
encoding: null,
368-
}), {message: 'To get a Buffer, set `options.responseType` to `buffer` instead'});
385+
}), {
386+
instanceOf: RequestError,
387+
message: 'To get a Buffer, set `options.responseType` to `buffer` instead',
388+
});
369389
});
370390

371391
test('`url` option and input argument are mutually exclusive', async t => {
372392
await t.throwsAsync(got('https://example.com', {
373393
url: 'https://example.com',
374-
}), {message: 'The `url` option is mutually exclusive with the `input` argument'});
394+
}), {
395+
instanceOf: RequestError,
396+
message: 'The `url` option is mutually exclusive with the `input` argument',
397+
});
375398
});
376399

377400
test('throws a helpful error when passing `followRedirects`', async t => {
378401
await t.throwsAsync(got('https://example.com', {
379402
// @ts-expect-error For testing purposes
380403
followRedirects: true,
381-
}), {message: 'The `followRedirects` option does not exist. Use `followRedirect` instead.'});
404+
}), {
405+
instanceOf: RequestError,
406+
message: 'The `followRedirects` option does not exist. Use `followRedirect` instead.',
407+
});
382408
});
383409

384410
test('merges `searchParams` instances', t => {
@@ -400,12 +426,14 @@ test('throws a helpful error when passing `auth`', async t => {
400426
// @ts-expect-error For testing purposes
401427
auth: 'username:password',
402428
}), {
429+
instanceOf: RequestError,
403430
message: 'Parameter `auth` is deprecated. Use `username` / `password` instead.',
404431
});
405432
});
406433

407434
test('throws on leading slashes', async t => {
408435
await t.throwsAsync(got('/asdf', {prefixUrl: 'https://example.com'}), {
436+
instanceOf: RequestError,
409437
message: '`url` must not start with a slash',
410438
});
411439
});
@@ -414,9 +442,11 @@ test('throws on invalid `dnsCache` option', async t => {
414442
await t.throwsAsync(got('https://example.com', {
415443
// @ts-expect-error Error tests
416444
dnsCache: 123,
417-
}));
418-
419-
// TODO: Assert message above.
445+
}),
446+
{
447+
instanceOf: RequestError,
448+
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `number`.',
449+
});
420450
});
421451

422452
test('throws on invalid `agent` option', async t => {
@@ -425,7 +455,10 @@ test('throws on invalid `agent` option', async t => {
425455
// @ts-expect-error Error tests
426456
asdf: 123,
427457
},
428-
}), {message: 'Unexpected agent option: asdf'});
458+
}), {
459+
instanceOf: RequestError,
460+
message: 'Unexpected agent option: asdf',
461+
});
429462
});
430463

431464
test('fallbacks to native http if `request(...)` returns undefined', withServer, async (t, server, got) => {
@@ -557,6 +590,7 @@ test('throws on too large noise', t => {
557590
},
558591
});
559592
}, {
593+
instanceOf: Error,
560594
message: 'The maximum acceptable retry noise is +/- 100ms, got 101',
561595
});
562596

@@ -567,6 +601,7 @@ test('throws on too large noise', t => {
567601
},
568602
});
569603
}, {
604+
instanceOf: Error,
570605
message: 'The maximum acceptable retry noise is +/- 100ms, got -101',
571606
});
572607

@@ -577,6 +612,7 @@ test('throws on too large noise', t => {
577612
},
578613
});
579614
}, {
615+
instanceOf: Error,
580616
message: 'The maximum acceptable retry noise is +/- 100ms, got Infinity',
581617
});
582618

@@ -587,6 +623,7 @@ test('throws on too large noise', t => {
587623
},
588624
});
589625
}, {
626+
instanceOf: Error,
590627
message: 'The maximum acceptable retry noise is +/- 100ms, got -Infinity',
591628
});
592629

@@ -608,6 +645,7 @@ test('options have url even if some are invalid', async t => {
608645
}));
609646

610647
t.is((error.options.url as URL).href, 'https://example.com/');
648+
t.true(error instanceof Error);
611649
});
612650

613651
test('options have url even if some are invalid - got.extend', async t => {
@@ -623,5 +661,8 @@ test('options have url even if some are invalid - got.extend', async t => {
623661
await t.throwsAsync(instance('https://example.com', {
624662
// @ts-expect-error Testing purposes
625663
invalid: true,
626-
}));
664+
}),
665+
{
666+
instanceOf: Error,
667+
});
627668
});

test/cancel.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import delay from 'delay';
66
import {pEvent} from 'p-event';
77
import getStream from 'get-stream';
88
import {Handler} from 'express';
9-
import got, {CancelError, TimeoutError} from '../source/index.js';
9+
import got, {CancelError, TimeoutError, RequestError} from '../source/index.js';
1010
import slowDataStream from './helpers/slow-data-stream.js';
1111
import {GlobalClock} from './helpers/types.js';
1212
import {ExtendedHttpTestServer} from './helpers/create-http-test-server.js';
@@ -180,7 +180,10 @@ test.serial('cancel immediately', withServerAndFakeTimers, async (t, server, got
180180
const gotPromise = got('abort');
181181
gotPromise.cancel();
182182

183-
await t.throwsAsync(gotPromise);
183+
await t.throwsAsync(gotPromise, {
184+
instanceOf: CancelError,
185+
code: 'ERR_CANCELED',
186+
});
184187
await t.notThrowsAsync(promise, 'Request finished instead of aborting.');
185188
});
186189

@@ -230,9 +233,8 @@ test.serial('throws on incomplete (canceled) response - promise', withServerAndF
230233
);
231234
});
232235

233-
// TODO: Use `fakeTimers` here
234-
test.serial('throws on incomplete (canceled) response - promise #2', withServer, async (t, server, got) => {
235-
server.get('/', downloadHandler());
236+
test.serial('throws on incomplete (canceled) response - promise #2', withServerAndFakeTimers, async (t, server, got, clock) => {
237+
server.get('/', downloadHandler(clock));
236238

237239
const promise = got('');
238240

@@ -256,7 +258,10 @@ test.serial('throws on incomplete (canceled) response - stream', withServerAndFa
256258
stream.destroy(new Error(errorString));
257259
});
258260

259-
await t.throwsAsync(getStream(stream), {message: errorString});
261+
await t.throwsAsync(getStream(stream), {
262+
instanceOf: RequestError,
263+
message: errorString,
264+
});
260265
});
261266

262267
test('throws when canceling cached request', withServer, async (t, server, got) => {

test/cookies.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import net from 'net';
22
import test from 'ava';
33
import toughCookie from 'tough-cookie';
44
import delay from 'delay';
5-
import got from '../source/index.js';
5+
import {got, RequestError} from '../source/index.js';
66
import withServer from './helpers/with-server.js';
77

88
test('reads a cookie', withServer, async (t, server, got) => {
@@ -63,7 +63,10 @@ test('throws on invalid cookies', withServer, async (t, server, got) => {
6363

6464
const cookieJar = new toughCookie.CookieJar();
6565

66-
await t.throwsAsync(got({cookieJar}), {message: 'Cookie has domain set to a public suffix'});
66+
await t.throwsAsync(got({cookieJar}), {
67+
instanceOf: RequestError,
68+
message: 'Cookie has domain set to a public suffix',
69+
});
6770
});
6871

6972
test('does not throw on invalid cookies when options.ignoreInvalidCookies is set', withServer, async (t, server, got) => {
@@ -98,7 +101,10 @@ test('catches store errors', async t => {
98101
synchronous: false,
99102
});
100103

101-
await t.throwsAsync(got('https://example.com', {cookieJar}), {message: error});
104+
await t.throwsAsync(got('https://example.com', {cookieJar}), {
105+
instanceOf: RequestError,
106+
message: error,
107+
});
102108
});
103109

104110
test('overrides options.headers.cookie', withServer, async (t, server, got) => {
@@ -139,7 +145,10 @@ test('no unhandled errors', async t => {
139145
},
140146
};
141147

142-
await t.throwsAsync(got(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`, options), {message});
148+
await t.throwsAsync(got(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`, options), {
149+
instanceOf: RequestError,
150+
message,
151+
});
143152
await delay(500);
144153
t.pass();
145154

@@ -177,7 +186,10 @@ test('throws on invalid `options.cookieJar.setCookie`', async t => {
177186
// @ts-expect-error Error tests
178187
setCookie: 123,
179188
},
180-
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
189+
}), {
190+
instanceOf: RequestError,
191+
message: 'Expected value which is `Function`, received value of type `number`.',
192+
});
181193
});
182194

183195
test('throws on invalid `options.cookieJar.getCookieString`', async t => {
@@ -187,7 +199,10 @@ test('throws on invalid `options.cookieJar.getCookieString`', async t => {
187199
// @ts-expect-error Error tests
188200
getCookieString: 123,
189201
},
190-
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
202+
}), {
203+
instanceOf: RequestError,
204+
message: 'Expected value which is `Function`, received value of type `number`.',
205+
});
191206
});
192207

193208
test('cookies are cleared when redirecting to a different hostname (no cookieJar)', withServer, async (t, server, got) => {

test/create.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,10 @@ test('async handlers can throw', async t => {
341341
],
342342
});
343343

344-
await t.throwsAsync(instance('https://example.com'), {message});
344+
await t.throwsAsync(instance('https://example.com'), {
345+
instanceOf: Error,
346+
message,
347+
});
345348
});
346349

347350
test('setting dnsCache to true points to global cache', t => {

0 commit comments

Comments
 (0)