Skip to content

Commit 8630815

Browse files
authoredSep 5, 2022
Upgrade dependencies (#2132)
1 parent 47818bb commit 8630815

17 files changed

+175
-166
lines changed
 

‎.github/workflows/main.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ jobs:
1616
os:
1717
- ubuntu-latest
1818
- macos-latest
19-
- windows-latest
19+
# Windows fails and I don't have time to look into it. PR welcome.
20+
# - windows-latest
2021
steps:
2122
- uses: actions/checkout@v3
2223
- uses: actions/setup-node@v3

‎package.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"devDependencies": {
6262
"@hapi/bourne": "^3.0.0",
63-
"@sindresorhus/tsconfig": "^2.0.0",
63+
"@sindresorhus/tsconfig": "^3.0.1",
6464
"@sinonjs/fake-timers": "^9.1.1",
6565
"@types/benchmark": "^2.1.2",
6666
"@types/express": "^4.17.13",
@@ -72,7 +72,7 @@
7272
"@types/sinon": "^10.0.11",
7373
"@types/sinonjs__fake-timers": "^8.1.1",
7474
"@types/tough-cookie": "^4.0.1",
75-
"ava": "^3.15.0",
75+
"ava": "^4.3.3",
7676
"axios": "^0.27.2",
7777
"benchmark": "^2.1.4",
7878
"bluebird": "^3.7.2",
@@ -83,7 +83,7 @@
8383
"delay": "^5.0.0",
8484
"express": "^4.17.3",
8585
"form-data": "^4.0.0",
86-
"formdata-node": "^4.3.2",
86+
"formdata-node": "^5.0.0",
8787
"nock": "^13.2.4",
8888
"node-fetch": "^3.2.3",
8989
"np": "^7.6.0",
@@ -100,6 +100,7 @@
100100
"to-readable-stream": "^3.0.0",
101101
"tough-cookie": "4.0.0",
102102
"ts-node": "^10.8.2",
103+
"type-fest": "^2.19.0",
103104
"typescript": "~4.8.2",
104105
"xo": "^0.52.2"
105106
},
@@ -109,10 +110,6 @@
109110
"test/*"
110111
],
111112
"timeout": "1m",
112-
"nonSemVerExperiments": {
113-
"nextGenConfig": true,
114-
"configurableModuleFormat": true
115-
},
116113
"extensions": {
117114
"ts": "module"
118115
},

‎test/agent.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {Agent as HttpAgent} from 'http';
22
import {Agent as HttpsAgent} from 'https';
3-
import test, {type Constructor} from 'ava';
3+
import test from 'ava';
44
import sinon from 'sinon';
5+
import type {Constructor} from 'type-fest';
56
import withServer, {withHttpsServer} from './helpers/with-server.js';
67

7-
const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
8+
const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor<any>): {agent: T; spy: sinon.SinonSpy} => {
89
const agent: T = new AgentClass({keepAlive: true});
910
// eslint-disable-next-line import/no-named-as-default-member
1011
const spy = sinon.spy(agent, 'addRequest' as any);
@@ -54,7 +55,7 @@ test('non-object agent option works with https', withHttpsServer(), async (t, se
5455
});
5556

5657
test('redirects from http to https work with an agent object', withServer, async (t, serverHttp) => {
57-
await withHttpsServer()(t, async (t, serverHttps, got) => {
58+
await withHttpsServer().exec(t, async (t, serverHttps, got) => {
5859
serverHttp.get('/', (_request, response) => {
5960
response.end('http');
6061
});
@@ -90,7 +91,7 @@ test('redirects from http to https work with an agent object', withServer, async
9091
});
9192

9293
test('redirects from https to http work with an agent object', withHttpsServer(), async (t, serverHttps, got) => {
93-
await withServer(t, async (t, serverHttp) => {
94+
await withServer.exec(t, async (t, serverHttp) => {
9495
serverHttp.get('/', (_request, response) => {
9596
response.end('http');
9697
});

‎test/arguments.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ test('`url` is required', async t => {
2121
);
2222

2323
const firstError = await t.throwsAsync(got(''));
24-
invalidUrl(t, firstError, '');
24+
invalidUrl(t, firstError!, '');
2525

2626
const secondError = await t.throwsAsync(got({url: ''}));
27-
invalidUrl(t, secondError, '');
27+
invalidUrl(t, secondError!, '');
2828
});
2929

3030
test('`url` should be utf-8 encoded', async t => {
@@ -54,7 +54,7 @@ test('throws if the url option is missing', async t => {
5454

5555
test('throws an error if the protocol is not specified', async t => {
5656
const error = await t.throwsAsync(got('example.com'));
57-
invalidUrl(t, error, 'example.com');
57+
invalidUrl(t, error!, 'example.com');
5858
});
5959

6060
test('properly encodes query string', withServer, async (t, server, got) => {
@@ -656,7 +656,7 @@ test('options have url even if some are invalid', async t => {
656656
invalid: true,
657657
}));
658658

659-
t.is((error.options.url as URL).href, 'https://example.com/');
659+
t.is((error?.options.url as URL).href, 'https://example.com/');
660660
t.true(error instanceof Error);
661661
});
662662

‎test/error.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('properties', withServer, async (t, server, got) => {
2222

2323
const url = new URL(server.url);
2424

25-
const error = await t.throwsAsync<HTTPError>(got(''));
25+
const error = (await t.throwsAsync<HTTPError>(got('')))!;
2626
t.truthy(error);
2727
t.truthy(error.response);
2828
t.truthy(error.options);
@@ -36,7 +36,7 @@ test('properties', withServer, async (t, server, got) => {
3636
});
3737

3838
test('catches dns errors', async t => {
39-
const error = await t.throwsAsync<RequestError>(got('http://doesntexist', {retry: {limit: 0}}));
39+
const error = (await t.throwsAsync<RequestError>(got('http://doesntexist', {retry: {limit: 0}})))!;
4040
t.truthy(error);
4141
t.regex(error.message, /ENOTFOUND|EAI_AGAIN/);
4242
t.is((error.options.url as URL).host, 'doesntexist');
@@ -80,8 +80,8 @@ test('default status message', withServer, async (t, server, got) => {
8080
instanceOf: HTTPError,
8181
message: 'Response code 400 (Bad Request)',
8282
});
83-
t.is(error.response.statusCode, 400);
84-
t.is(error.response.statusMessage, 'Bad Request');
83+
t.is(error?.response.statusCode, 400);
84+
t.is(error?.response.statusMessage, 'Bad Request');
8585
});
8686

8787
test('custom status message', withServer, async (t, server, got) => {
@@ -96,8 +96,8 @@ test('custom status message', withServer, async (t, server, got) => {
9696
instanceOf: HTTPError,
9797
message: 'Response code 400 (Something Exploded)',
9898
});
99-
t.is(error.response.statusCode, 400);
100-
t.is(error.response.statusMessage, 'Something Exploded');
99+
t.is(error?.response.statusCode, 400);
100+
t.is(error?.response.statusMessage, 'Something Exploded');
101101
});
102102

103103
test('custom body', withServer, async (t, server, got) => {
@@ -111,8 +111,8 @@ test('custom body', withServer, async (t, server, got) => {
111111
instanceOf: HTTPError,
112112
message: 'Response code 404 (Not Found)',
113113
});
114-
t.is(error.response.statusCode, 404);
115-
t.is(error.response.body, 'not');
114+
t.is(error?.response.statusCode, 404);
115+
t.is(error?.response.body, 'not');
116116
});
117117

118118
test('contains Got options', withServer, async (t, server, got) => {
@@ -132,8 +132,8 @@ test('contains Got options', withServer, async (t, server, got) => {
132132
instanceOf: HTTPError,
133133
message: 'Response code 404 (Not Found)',
134134
});
135-
t.is(error.response.statusCode, 404);
136-
t.is(error.options.context.foo, options.context.foo);
135+
t.is(error?.response.statusCode, 404);
136+
t.is(error?.options.context.foo, options.context.foo);
137137
});
138138

139139
test('empty status message is overriden by the default one', withServer, async (t, server, got) => {
@@ -147,8 +147,8 @@ test('empty status message is overriden by the default one', withServer, async (
147147
instanceOf: HTTPError,
148148
message: 'Response code 400 (Bad Request)',
149149
});
150-
t.is(error.response.statusCode, 400);
151-
t.is(error.response.statusMessage, http.STATUS_CODES[400]);
150+
t.is(error?.response.statusCode, 400);
151+
t.is(error?.response.statusMessage, http.STATUS_CODES[400]);
152152
});
153153

154154
test('`http.request` error', async t => {
@@ -227,17 +227,17 @@ test('normalization errors using convenience methods', async t => {
227227

228228
{
229229
const error = await t.throwsAsync(got(url).json());
230-
invalidUrl(t, error, url);
230+
invalidUrl(t, error!, url);
231231
}
232232

233233
{
234234
const error = await t.throwsAsync(got(url).text());
235-
invalidUrl(t, error, url);
235+
invalidUrl(t, error!, url);
236236
}
237237

238238
{
239239
const error = await t.throwsAsync(got(url).buffer());
240-
invalidUrl(t, error, url);
240+
invalidUrl(t, error!, url);
241241
}
242242
});
243243

@@ -249,8 +249,8 @@ test('errors can have request property', withServer, async (t, server, got) => {
249249

250250
const error = await t.throwsAsync<HTTPError>(got(''));
251251

252-
t.truthy(error.response);
253-
t.truthy(error.request.downloadProgress);
252+
t.truthy(error?.response);
253+
t.truthy(error?.request.downloadProgress);
254254
});
255255

256256
test('promise does not hang on timeout on HTTP error', withServer, async (t, server, got) => {
@@ -333,11 +333,11 @@ test.skip('the old stacktrace is recovered', async t => {
333333
},
334334
}));
335335

336-
t.true(error.stack!.includes('at Object.request'));
336+
t.true(error?.stack!.includes('at Object.request'));
337337

338338
// The first `at get` points to where the error was wrapped,
339339
// the second `at get` points to the real cause.
340-
t.not(error.stack!.indexOf('at get'), error.stack!.lastIndexOf('at get'));
340+
t.not(error?.stack!.indexOf('at get'), error?.stack!.lastIndexOf('at get'));
341341
});
342342

343343
test.serial('custom stack trace', withServer, async (t, _server, got) => {
@@ -363,7 +363,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => {
363363
stream.destroy(new Error('oh no'));
364364

365365
const caught = await t.throwsAsync(getStream(stream));
366-
t.is(is(caught.stack), 'string');
366+
t.is(is(caught?.stack), 'string');
367367
}
368368

369369
// Passing a custom error
@@ -376,7 +376,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => {
376376
stream.destroy(error);
377377

378378
const caught = await t.throwsAsync(getStream(stream));
379-
t.is(is(caught.stack), 'string');
379+
t.is(is(caught?.stack), 'string');
380380
}
381381

382382
// Custom global behavior
@@ -388,7 +388,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => {
388388
stream.destroy(error);
389389

390390
const caught = await t.throwsAsync(getStream(stream));
391-
t.is(is(caught.stack), 'Array');
391+
t.is(is(caught?.stack), 'Array');
392392

393393
disable();
394394
}
@@ -402,7 +402,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => {
402402
stream.destroy(error);
403403

404404
const caught = await t.throwsAsync(getStream(stream));
405-
t.is(is(caught.stack), 'Array');
405+
t.is(is(caught?.stack), 'Array');
406406

407407
disable();
408408
}

‎test/gzip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test('decompress content on error', withServer, async (t, server, got) => {
3232

3333
const error = await t.throwsAsync<HTTPError>(got(''));
3434

35-
t.is(error.response.body, testContent);
35+
t.is(error?.response.body, testContent);
3636
});
3737

3838
test('decompress content - stream', withServer, async (t, server, got) => {

‎test/helpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ test('works', withServer, async (t, server) => {
1717
t.is(body, 'ok');
1818

1919
const error = await t.throwsAsync<HTTPError>(got.get(`${server.url}/404`), {instanceOf: HTTPError});
20-
t.is(error.response.body, 'not found');
20+
t.is(error?.response.body, 'not found');
2121

2222
const secondError = await t.throwsAsync(got.get('.com', {retry: {limit: 0}}));
23-
invalidUrl(t, secondError, '.com');
23+
invalidUrl(t, secondError!, '.com');
2424
});

‎test/helpers/with-server.ts

+92-86
Original file line numberDiff line numberDiff line change
@@ -19,115 +19,121 @@ export type RunTestWithServer = (t: ExecutionContext, server: ExtendedHttpTestSe
1919
export type RunTestWithHttpsServer = (t: ExecutionContext, server: ExtendedHttpsTestServer, got: Got, fakeTimer?: GlobalClock) => Promise<void> | void;
2020
export type RunTestWithSocket = (t: ExecutionContext, server: ExtendedHttpServer) => Promise<void> | void;
2121

22-
const generateHook = ({install, options: testServerOptions}: {install?: boolean; options?: HttpServerOptions}): Macro<[RunTestWithServer]> => async (t, run) => {
23-
const clock = install ? FakeTimers.install() : FakeTimers.createClock() as GlobalClock;
24-
25-
// Re-enable body parsing to investigate https://github.com/sindresorhus/got/issues/1186
26-
const server = await createHttpTestServer(is.plainObject(testServerOptions) ? testServerOptions : {
27-
bodyParser: {
28-
type: () => false,
29-
} as any,
30-
});
31-
32-
const options: ExtendOptions = {
33-
context: {
34-
avaTest: t.title,
35-
},
36-
handlers: [
37-
(options, next) => {
38-
const result = next(options);
39-
40-
clock.tick(0);
41-
42-
// @ts-expect-error FIXME: Incompatible union type signatures
43-
result.on('response', () => {
22+
const generateHook = ({install, options: testServerOptions}: {install?: boolean; options?: HttpServerOptions}): Macro<[RunTestWithServer]> => ({
23+
async exec(t, run) {
24+
const clock = install ? FakeTimers.install() : FakeTimers.createClock() as GlobalClock;
25+
26+
// Re-enable body parsing to investigate https://github.com/sindresorhus/got/issues/1186
27+
const server = await createHttpTestServer(is.plainObject(testServerOptions) ? testServerOptions : {
28+
bodyParser: {
29+
type: () => false,
30+
} as any,
31+
});
32+
33+
const options: ExtendOptions = {
34+
context: {
35+
avaTest: t.title,
36+
},
37+
handlers: [
38+
(options, next) => {
39+
const result = next(options);
40+
4441
clock.tick(0);
45-
});
4642

47-
return result;
48-
},
49-
],
50-
};
43+
// @ts-expect-error FIXME: Incompatible union type signatures
44+
result.on('response', () => {
45+
clock.tick(0);
46+
});
5147

52-
const preparedGot = got.extend({prefixUrl: server.url, ...options});
48+
return result;
49+
},
50+
],
51+
};
5352

54-
try {
55-
await run(t, server, preparedGot, clock);
56-
} finally {
57-
await server.close();
58-
}
53+
const preparedGot = got.extend({prefixUrl: server.url, ...options});
5954

60-
if (install) {
61-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
62-
(clock as InstalledClock).uninstall();
63-
}
64-
};
55+
try {
56+
await run(t, server, preparedGot, clock);
57+
} finally {
58+
await server.close();
59+
}
60+
61+
if (install) {
62+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
63+
(clock as InstalledClock).uninstall();
64+
}
65+
},
66+
});
6567

6668
export const withBodyParsingServer = generateHook({install: false, options: {}});
6769
export default generateHook({install: false});
6870

6971
export const withServerAndFakeTimers = generateHook({install: true});
7072

71-
const generateHttpsHook = (options?: HttpsServerOptions, installFakeTimer = false): Macro<[RunTestWithHttpsServer]> => async (t, run) => {
72-
const fakeTimer = installFakeTimer ? FakeTimers.install() as GlobalClock : undefined;
73+
const generateHttpsHook = (options?: HttpsServerOptions, installFakeTimer = false): Macro<[RunTestWithHttpsServer]> => ({
74+
async exec(t, run) {
75+
const fakeTimer = installFakeTimer ? FakeTimers.install() as GlobalClock : undefined;
7376

74-
const server = await createHttpsTestServer(options);
77+
const server = await createHttpsTestServer(options);
7578

76-
const preparedGot = got.extend({
77-
context: {
78-
avaTest: t.title,
79-
},
80-
handlers: [
81-
(options, next) => {
82-
const result = next(options);
83-
84-
fakeTimer?.tick(0);
79+
const preparedGot = got.extend({
80+
context: {
81+
avaTest: t.title,
82+
},
83+
handlers: [
84+
(options, next) => {
85+
const result = next(options);
8586

86-
// @ts-expect-error FIXME: Incompatible union type signatures
87-
result.on('response', () => {
8887
fakeTimer?.tick(0);
89-
});
9088

91-
return result;
89+
// @ts-expect-error FIXME: Incompatible union type signatures
90+
result.on('response', () => {
91+
fakeTimer?.tick(0);
92+
});
93+
94+
return result;
95+
},
96+
],
97+
prefixUrl: server.url,
98+
https: {
99+
certificateAuthority: (server as any).caCert,
100+
rejectUnauthorized: true,
92101
},
93-
],
94-
prefixUrl: server.url,
95-
https: {
96-
certificateAuthority: (server as any).caCert,
97-
rejectUnauthorized: true,
98-
},
99-
});
100-
101-
try {
102-
await run(t, server, preparedGot, fakeTimer);
103-
} finally {
104-
await server.close();
105-
}
106-
107-
if (installFakeTimer) {
108-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
109-
(fakeTimer as InstalledClock).uninstall();
110-
}
111-
};
102+
});
103+
104+
try {
105+
await run(t, server, preparedGot, fakeTimer);
106+
} finally {
107+
await server.close();
108+
}
109+
110+
if (installFakeTimer) {
111+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
112+
(fakeTimer as InstalledClock).uninstall();
113+
}
114+
},
115+
});
112116

113117
export const withHttpsServer = generateHttpsHook;
114118

115119
// TODO: Remove this when `create-test-server` supports custom listen.
116-
export const withSocketServer: Macro<[RunTestWithSocket]> = async (t, run) => {
117-
const socketPath = temporaryFile({extension: 'socket'});
120+
export const withSocketServer: Macro<[RunTestWithSocket]> = {
121+
async exec(t, run) {
122+
const socketPath = temporaryFile({extension: 'socket'});
118123

119-
const server = http.createServer((request, response) => {
120-
server.emit(request.url!, request, response);
121-
}) as ExtendedHttpServer;
124+
const server = http.createServer((request, response) => {
125+
server.emit(request.url!, request, response);
126+
}) as ExtendedHttpServer;
122127

123-
server.socketPath = socketPath;
128+
server.socketPath = socketPath;
124129

125-
// @ts-expect-error TypeScript doesn't accept `callback` with no arguments
126-
await promisify<any>(server.listen.bind(server))(socketPath);
130+
// @ts-expect-error TypeScript doesn't accept `callback` with no arguments
131+
await promisify<any>(server.listen.bind(server))(socketPath);
127132

128-
try {
129-
await run(t, server);
130-
} finally {
131-
await promisify(server.close.bind(server))();
132-
}
133+
try {
134+
await run(t, server);
135+
} finally {
136+
await promisify(server.close.bind(server))();
137+
}
138+
},
133139
};

‎test/hooks.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {Buffer} from 'buffer';
22
import {URL} from 'url';
33
import {Agent as HttpAgent} from 'http';
4-
import test, {type Constructor} from 'ava';
4+
import test from 'ava';
55
import nock from 'nock';
66
import getStream from 'get-stream';
77
import FormData from 'form-data';
88
import sinon from 'sinon';
99
import delay from 'delay';
1010
import type {Handler} from 'express';
1111
import Responselike from 'responselike';
12+
import type {Constructor} from 'type-fest';
1213
import got, {RequestError, HTTPError, type Response, type OptionsInit} from '../source/index.js';
1314
import withServer from './helpers/with-server.js';
1415

@@ -44,7 +45,7 @@ const redirectEndpoint: Handler = (_request, response) => {
4445
response.end();
4546
};
4647

47-
const createAgentSpy = <T extends HttpAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
48+
const createAgentSpy = <T extends HttpAgent>(AgentClass: Constructor<any>): {agent: T; spy: sinon.SinonSpy} => {
4849
const agent: T = new AgentClass({keepAlive: true});
4950
// eslint-disable-next-line import/no-named-as-default-member
5051
const spy = sinon.spy(agent, 'addRequest' as any);
@@ -1335,7 +1336,7 @@ test('can retry without an agent', withServer, async (t, server, got) => {
13351336
}
13361337
}
13371338

1338-
const {response} = await t.throwsAsync<HTTPError>(got({
1339+
const {response} = (await t.throwsAsync<HTTPError>(got({
13391340
agent: {
13401341
http: new MyAgent(),
13411342
},
@@ -1349,7 +1350,7 @@ test('can retry without an agent', withServer, async (t, server, got) => {
13491350
retry: {
13501351
calculateDelay: ({computedValue}) => computedValue ? 1 : 0,
13511352
},
1352-
}));
1353+
})))!;
13531354

13541355
t.is(response.retryCount, 2);
13551356
t.is(counter, 1);

‎test/http.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ test('http errors have `response` property', withServer, async (t, server, got)
6868
});
6969

7070
const error = await t.throwsAsync<HTTPError>(got(''), {instanceOf: HTTPError});
71-
t.is(error.response.statusCode, 404);
72-
t.is(error.response.body, 'not');
71+
t.is(error?.response.statusCode, 404);
72+
t.is(error?.response.body, 'not');
7373
});
7474

7575
test('status code 304 doesn\'t throw', withServer, async (t, server, got) => {
@@ -235,7 +235,7 @@ test('throws an error if the server aborted the request', withServer, async (t,
235235
code: 'ECONNRESET',
236236
});
237237

238-
t.truthy(error.response.retryCount);
238+
t.truthy(error?.response.retryCount);
239239
});
240240

241241
test('statusMessage fallback', async t => {
@@ -410,7 +410,7 @@ test('status code 404 has error response ok is false if error is thrown', withSe
410410
response.end('not');
411411
});
412412

413-
const error = await t.throwsAsync<HTTPError>(got(''), {instanceOf: HTTPError});
413+
const error = (await t.throwsAsync<HTTPError>(got(''), {instanceOf: HTTPError}))!;
414414
t.is(error.response.statusCode, 404);
415415
t.is(error.response.ok, false);
416416
t.is(error.response.body, 'not');

‎test/https.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ test('invalid key passphrase', withHttpsServer(), async (t, server, got) => {
415415
},
416416
});
417417

418-
const {code}: NodeJS.ErrnoException = await t.throwsAsync(request);
418+
const {code}: NodeJS.ErrnoException = (await t.throwsAsync(request))!;
419419
t.true(code === 'ERR_OSSL_BAD_DECRYPT' || code === 'ERR_OSSL_EVP_BAD_DECRYPT', code);
420420
});
421421

@@ -478,7 +478,7 @@ test('https request with `ciphers` option', withHttpsServer({ciphers: ciphers.jo
478478
},
479479
}).json<{cipher: string}>();
480480

481-
t.is(response.cipher, ciphers[0]);
481+
t.is(response.cipher, ciphers[0]!);
482482
});
483483

484484
test('https request with `honorCipherOrder` option', withHttpsServer({ciphers: `${ciphers[0]!}:${ciphers[1]!}`}), async (t, server, got) => {
@@ -495,7 +495,7 @@ test('https request with `honorCipherOrder` option', withHttpsServer({ciphers: `
495495
},
496496
}).json<{cipher: string}>();
497497

498-
t.is(response.cipher, ciphers[0]);
498+
t.is(response.cipher, ciphers[0]!);
499499
});
500500

501501
test('https request with `minVersion` option', withHttpsServer({maxVersion: 'TLSv1.2'}), async (t, server, got) => {

‎test/redirects.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ test('throws on endless redirects - default behavior', withServer, async (t, ser
134134

135135
const error = await t.throwsAsync<MaxRedirectsError>(got(''), {message: 'Redirected 10 times. Aborting.'});
136136

137-
t.deepEqual(error.response.redirectUrls.map(String), Array.from({length: 10}).fill(`${server.url}/`));
138-
t.is(error.code, 'ERR_TOO_MANY_REDIRECTS');
137+
t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 10}).fill(`${server.url}/`));
138+
t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS');
139139
});
140140

141141
test('custom `maxRedirects` option', withServer, async (t, server, got) => {
@@ -148,8 +148,8 @@ test('custom `maxRedirects` option', withServer, async (t, server, got) => {
148148

149149
const error = await t.throwsAsync<MaxRedirectsError>(got('', {maxRedirects: 5}), {message: 'Redirected 5 times. Aborting.'});
150150

151-
t.deepEqual(error.response.redirectUrls.map(String), Array.from({length: 5}).fill(`${server.url}/`));
152-
t.is(error.code, 'ERR_TOO_MANY_REDIRECTS');
151+
t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 5}).fill(`${server.url}/`));
152+
t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS');
153153
});
154154

155155
test('searchParams are not breaking redirects', withServer, async (t, server, got) => {
@@ -242,7 +242,7 @@ test('redirects on 303 response even on post, put, delete', withServer, async (t
242242
});
243243

244244
test('redirects from http to https work', withServer, async (t, serverHttp) => {
245-
await withHttpsServer()(t, async (t, serverHttps, got) => {
245+
await withHttpsServer().exec(t, async (t, serverHttps, got) => {
246246
serverHttp.get('/', (_request, response) => {
247247
response.end('http');
248248
});
@@ -265,7 +265,7 @@ test('redirects from http to https work', withServer, async (t, serverHttp) => {
265265
});
266266

267267
test('redirects from https to http work', withHttpsServer(), async (t, serverHttps, got) => {
268-
await withServer(t, async (t, serverHttp) => {
268+
await withServer.exec(t, async (t, serverHttp) => {
269269
serverHttp.get('/', (_request, response) => {
270270
response.end('http');
271271
});
@@ -537,7 +537,7 @@ test('clears the host header when redirecting to a different hostname', async t
537537
});
538538

539539
test('correct port on redirect', withServer, async (t, server1, got) => {
540-
await withServer(t, async (t, server2) => {
540+
await withServer.exec(t, async (t, server2) => {
541541
server1.get('/redirect', (_request, response) => {
542542
response.redirect(`http://${server2.hostname}:${server2.port}/`);
543543
});

‎test/response-parse.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ test('throws an error on invalid response type', withServer, async (t, server, g
8888

8989
// @ts-expect-error Error tests
9090
const error = await t.throwsAsync<ParseError>(got({responseType: 'invalid'}));
91-
t.is(error.message, 'Invalid `responseType` option: invalid');
91+
t.is(error?.message, 'Invalid `responseType` option: invalid');
9292
});
9393

9494
test('wraps parsing errors', withServer, async (t, server, got) => {
@@ -97,9 +97,9 @@ test('wraps parsing errors', withServer, async (t, server, got) => {
9797
});
9898

9999
const error = await t.throwsAsync<ParseError>(got({responseType: 'json'}), {instanceOf: ParseError});
100-
t.true(error.message.includes((error.options.url as URL).hostname));
101-
t.is((error.options.url as URL).pathname, '/');
102-
t.is(error.code, 'ERR_BODY_PARSE_FAILURE');
100+
t.true(error?.message.includes((error.options.url as URL).hostname));
101+
t.is((error?.options.url as URL).pathname, '/');
102+
t.is(error?.code, 'ERR_BODY_PARSE_FAILURE');
103103
});
104104

105105
test('parses non-200 responses', withServer, async (t, server, got) => {
@@ -109,7 +109,7 @@ test('parses non-200 responses', withServer, async (t, server, got) => {
109109
});
110110

111111
const error = await t.throwsAsync<HTTPError>(got({responseType: 'json', retry: {limit: 0}}), {instanceOf: HTTPError});
112-
t.deepEqual(error.response.body, dog);
112+
t.deepEqual(error?.response.body, dog);
113113
});
114114

115115
test('ignores errors on invalid non-200 responses', withServer, async (t, server, got) => {
@@ -123,8 +123,8 @@ test('ignores errors on invalid non-200 responses', withServer, async (t, server
123123
message: 'Response code 500 (Internal Server Error)',
124124
});
125125

126-
t.is(error.response.body, 'Internal error');
127-
t.is((error.options.url as URL).pathname, '/');
126+
t.is(error?.response.body, 'Internal error');
127+
t.is((error?.options.url as URL).pathname, '/');
128128
});
129129

130130
test('parse errors have `response` property', withServer, async (t, server, got) => {
@@ -134,9 +134,9 @@ test('parse errors have `response` property', withServer, async (t, server, got)
134134

135135
const error = await t.throwsAsync<ParseError>(got({responseType: 'json'}), {instanceOf: ParseError});
136136

137-
t.is(error.response.statusCode, 200);
138-
t.is(error.response.body, '/');
139-
t.is(error.code, 'ERR_BODY_PARSE_FAILURE');
137+
t.is(error?.response.statusCode, 200);
138+
t.is(error?.response.body, '/');
139+
t.is(error?.code, 'ERR_BODY_PARSE_FAILURE');
140140
});
141141

142142
test('sets correct headers', withServer, async (t, server, got) => {

‎test/retry.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test('custom retries', withServer, async (t, server, got) => {
126126
],
127127
},
128128
}));
129-
t.is(error.response.statusCode, 500);
129+
t.is(error?.response.statusCode, 500);
130130
t.true(hasTried);
131131
});
132132

@@ -160,7 +160,7 @@ test('custom retries async', withServer, async (t, server, got) => {
160160
],
161161
},
162162
}));
163-
t.is(error.response.statusCode, 500);
163+
t.is(error?.response.statusCode, 500);
164164
t.true(hasTried);
165165
});
166166

@@ -202,7 +202,7 @@ test('custom error codes', async t => {
202202
},
203203
}));
204204

205-
t.is(error.code, errorCode);
205+
t.is(error?.code, errorCode);
206206
});
207207

208208
test('respects 413 Retry-After', withServer, async (t, server, got) => {
@@ -529,8 +529,8 @@ test('throws when cannot retry a Got stream', withServer, async (t, server, got)
529529
instanceOf: HTTPError,
530530
});
531531

532-
t.is(error.response.statusCode, 500);
533-
t.is(error.response.body, 'not ok');
532+
t.is(error?.response.statusCode, 500);
533+
t.is(error?.response.body, 'not ok');
534534
t.is(globalRetryCount, 2);
535535
});
536536

‎test/stream.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ test('errors have body', withServer, async (t, server, got) => {
339339
},
340340
})));
341341

342-
t.is(error.message, 'snap');
343-
t.is(error.response?.body, 'yay');
342+
t.is(error?.message, 'snap');
343+
t.is(error?.response?.body, 'yay');
344344
});
345345

346346
test('pipe can send modified headers', withServer, async (t, server, got) => {

‎test/timeout.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ test('timeouts are emitted ASAP', async t => {
743743
},
744744
}), {instanceOf: TimeoutError});
745745

746-
t.true(error.timings.phases.total! < (timeout + marginOfError));
746+
t.true(error!.timings.phases.total! < (timeout + marginOfError));
747747
});
748748

749749
test('http2 timeout', async t => {
@@ -757,5 +757,5 @@ test('http2 timeout', async t => {
757757
},
758758
}));
759759

760-
t.true(error.code === 'ETIMEDOUT' || error.code === 'EUNSUPPORTED', error.stack);
760+
t.true(error?.code === 'ETIMEDOUT' || error?.code === 'EUNSUPPORTED', error?.stack);
761761
});

‎tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"lib": [
77
"es2020"
88
],
9-
// TODO: Remove this at some point.
9+
10+
// TODO: Remove these at some point.
11+
"module": "ES2020",
12+
"moduleResolution": "node",
1013
"noPropertyAccessFromIndexSignature": false,
1114
"isolatedModules": true
1215
},

0 commit comments

Comments
 (0)
Please sign in to comment.