Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check error instance for arguments test #2044

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 53 additions & 8 deletions test/arguments.ts
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,6 +97,9 @@ 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,
},
);

// TODO: Assert message above.
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -103,7 +110,10 @@ test('throws an error when legacy URL is passed', withServer, async (t, server)
hostname: 'localhost',
port: server.port,
} as any),
{message: 'Unexpected option: protocol'},
{
instanceOf: RequestError,
message: 'Unexpected option: protocol',
},
);
});

Expand Down Expand Up @@ -163,6 +173,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 +217,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,6 +227,9 @@ 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,
},
);

// TODO: Assert message above.
Expand All @@ -225,6 +240,7 @@ test('throws when known `options.hooks` array item is not a function', async t =
// @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 +251,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,7 +303,10 @@ test('throws if the `searchParams` value is invalid', async t => {
// @ts-expect-error Error tests
foo: [],
},
}));
}),
{
instanceOf: RequestError,
});

// TODO: Assert message above.
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
});
Expand Down Expand Up @@ -365,20 +385,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 +429,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,7 +445,10 @@ test('throws on invalid `dnsCache` option', async t => {
await t.throwsAsync(got('https://example.com', {
// @ts-expect-error Error tests
dnsCache: 123,
}));
}),
{
instanceOf: RequestError,
});

// TODO: Assert message above.
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
});
Expand All @@ -425,7 +459,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 +594,7 @@ test('throws on too large noise', t => {
},
});
}, {
instanceOf: Error,
message: 'The maximum acceptable retry noise is +/- 100ms, got 101',
});

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

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

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

Expand All @@ -608,6 +649,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 +665,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,
});
});