Skip to content

Commit

Permalink
Check error instance for arguments test (#2044)
Browse files Browse the repository at this point in the history
Co-authored-by: Iswarya Sankaran <[email protected]>
  • Loading branch information
IswaryaS and Iswarya Sankaran authored May 22, 2022
1 parent 22d58fb commit 4cdcca3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 34 deletions.
73 changes: 57 additions & 16 deletions test/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('`url` is required', async t => {
// @ts-expect-error No argument on purpose.
got(),
{
instanceOf: RequestError,
message: 'Missing `url` property',
},
);
Expand All @@ -30,6 +31,7 @@ test('`url` should be utf-8 encoded', async t => {
await t.throwsAsync(
got('https://example.com/%D2%E0%EB%EB%E8%ED'),
{
instanceOf: RequestError,
message: 'URI malformed',
},
);
Expand All @@ -38,12 +40,14 @@ test('`url` should be utf-8 encoded', async t => {
test('throws if no arguments provided', async t => {
// @ts-expect-error Error tests
await t.throwsAsync(got(), {
instanceOf: RequestError,
message: 'Missing `url` property',
});
});

test('throws if the url option is missing', async t => {
await t.throwsAsync(got({}), {
instanceOf: RequestError,
message: 'Missing `url` property',
});
});
Expand Down Expand Up @@ -93,17 +97,22 @@ test('throws an error when legacy URL is passed', withServer, async (t, server)
await t.throwsAsync(
// @ts-expect-error Error tests
got(parse(`${server.url}/test`)),
{
instanceOf: RequestError,
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Object`.',
},
);

// TODO: Assert message above.

await t.throwsAsync(
got({
protocol: 'http:',
hostname: 'localhost',
port: server.port,
} as any),
{message: 'Unexpected option: protocol'},
{
instanceOf: RequestError,
message: 'Unexpected option: protocol',
},
);
});

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

test('throws when passing body with a non payload method', async t => {
await t.throwsAsync(got('https://example.com', {body: 'asdf'}), {
instanceOf: RequestError,
message: 'The `GET` method cannot be used with a body',
});
});
Expand Down Expand Up @@ -206,6 +216,7 @@ test('throws when `options.hooks` is not an object', async t => {
// @ts-expect-error Error tests
got('https://example.com', {hooks: 'not object'}),
{
instanceOf: RequestError,
message: 'Expected value which is `Object`, received value of type `string`.',
},
);
Expand All @@ -215,16 +226,19 @@ test('throws when known `options.hooks` value is not an array', async t => {
await t.throwsAsync(
// @ts-expect-error Error tests
got('https://example.com', {hooks: {beforeRequest: {}}}),
{
instanceOf: RequestError,
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Object`.',
},
);

// TODO: Assert message above.
});

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

// @ts-expect-error Error tests
await t.throwsAsync(got('test', {hooks: {extra: []}}), {
instanceOf: RequestError,
message: 'Unexpected hook event: extra',
});
});
Expand Down Expand Up @@ -286,9 +301,11 @@ test('throws if the `searchParams` value is invalid', async t => {
// @ts-expect-error Error tests
foo: [],
},
}));

// TODO: Assert message above.
}),
{
instanceOf: RequestError,
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `Array`.',
});
});

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

test('`url` option and input argument are mutually exclusive', async t => {
await t.throwsAsync(got('https://example.com', {
url: 'https://example.com',
}), {message: 'The `url` option is mutually exclusive with the `input` argument'});
}), {
instanceOf: RequestError,
message: 'The `url` option is mutually exclusive with the `input` argument',
});
});

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

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

test('throws on leading slashes', async t => {
await t.throwsAsync(got('/asdf', {prefixUrl: 'https://example.com'}), {
instanceOf: RequestError,
message: '`url` must not start with a slash',
});
});
Expand All @@ -414,9 +442,11 @@ test('throws on invalid `dnsCache` option', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-expect-error Error tests
dnsCache: 123,
}));

// TODO: Assert message above.
}),
{
instanceOf: RequestError,
message: 'Expected value which is `predicate returns truthy for any value`, received values of types `number`.',
});
});

test('throws on invalid `agent` option', async t => {
Expand All @@ -425,7 +455,10 @@ test('throws on invalid `agent` option', async t => {
// @ts-expect-error Error tests
asdf: 123,
},
}), {message: 'Unexpected agent option: asdf'});
}), {
instanceOf: RequestError,
message: 'Unexpected agent option: asdf',
});
});

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

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

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

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

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

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

test('options have url even if some are invalid - got.extend', async t => {
Expand All @@ -623,5 +661,8 @@ test('options have url even if some are invalid - got.extend', async t => {
await t.throwsAsync(instance('https://example.com', {
// @ts-expect-error Testing purposes
invalid: true,
}));
}),
{
instanceOf: Error,
});
});
17 changes: 11 additions & 6 deletions test/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import delay from 'delay';
import {pEvent} from 'p-event';
import getStream from 'get-stream';
import {Handler} from 'express';
import got, {CancelError, TimeoutError} from '../source/index.js';
import got, {CancelError, TimeoutError, RequestError} from '../source/index.js';
import slowDataStream from './helpers/slow-data-stream.js';
import {GlobalClock} from './helpers/types.js';
import {ExtendedHttpTestServer} from './helpers/create-http-test-server.js';
Expand Down Expand Up @@ -180,7 +180,10 @@ test.serial('cancel immediately', withServerAndFakeTimers, async (t, server, got
const gotPromise = got('abort');
gotPromise.cancel();

await t.throwsAsync(gotPromise);
await t.throwsAsync(gotPromise, {
instanceOf: CancelError,
code: 'ERR_CANCELED',
});
await t.notThrowsAsync(promise, 'Request finished instead of aborting.');
});

Expand Down Expand Up @@ -230,9 +233,8 @@ test.serial('throws on incomplete (canceled) response - promise', withServerAndF
);
});

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

const promise = got('');

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

await t.throwsAsync(getStream(stream), {message: errorString});
await t.throwsAsync(getStream(stream), {
instanceOf: RequestError,
message: errorString,
});
});

test('throws when canceling cached request', withServer, async (t, server, got) => {
Expand Down
27 changes: 21 additions & 6 deletions test/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import net from 'net';
import test from 'ava';
import toughCookie from 'tough-cookie';
import delay from 'delay';
import got from '../source/index.js';
import {got, RequestError} from '../source/index.js';
import withServer from './helpers/with-server.js';

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

const cookieJar = new toughCookie.CookieJar();

await t.throwsAsync(got({cookieJar}), {message: 'Cookie has domain set to a public suffix'});
await t.throwsAsync(got({cookieJar}), {
instanceOf: RequestError,
message: 'Cookie has domain set to a public suffix',
});
});

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

await t.throwsAsync(got('https://example.com', {cookieJar}), {message: error});
await t.throwsAsync(got('https://example.com', {cookieJar}), {
instanceOf: RequestError,
message: error,
});
});

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

await t.throwsAsync(got(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`, options), {message});
await t.throwsAsync(got(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`, options), {
instanceOf: RequestError,
message,
});
await delay(500);
t.pass();

Expand Down Expand Up @@ -177,7 +186,10 @@ test('throws on invalid `options.cookieJar.setCookie`', async t => {
// @ts-expect-error Error tests
setCookie: 123,
},
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
}), {
instanceOf: RequestError,
message: 'Expected value which is `Function`, received value of type `number`.',
});
});

test('throws on invalid `options.cookieJar.getCookieString`', async t => {
Expand All @@ -187,7 +199,10 @@ test('throws on invalid `options.cookieJar.getCookieString`', async t => {
// @ts-expect-error Error tests
getCookieString: 123,
},
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
}), {
instanceOf: RequestError,
message: 'Expected value which is `Function`, received value of type `number`.',
});
});

test('cookies are cleared when redirecting to a different hostname (no cookieJar)', withServer, async (t, server, got) => {
Expand Down
5 changes: 4 additions & 1 deletion test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ test('async handlers can throw', async t => {
],
});

await t.throwsAsync(instance('https://example.com'), {message});
await t.throwsAsync(instance('https://example.com'), {
instanceOf: Error,
message,
});
});

test('setting dnsCache to true points to global cache', t => {
Expand Down
Loading

0 comments on commit 4cdcca3

Please sign in to comment.