From 86308155d1bc43c635773183db908af902922549 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 5 Sep 2022 10:46:08 +0700 Subject: [PATCH] Upgrade dependencies (#2132) --- .github/workflows/main.yml | 3 +- package.json | 11 +-- test/agent.ts | 9 +- test/arguments.ts | 8 +- test/error.ts | 46 +++++----- test/gzip.ts | 2 +- test/helpers.ts | 4 +- test/helpers/with-server.ts | 178 +++++++++++++++++++----------------- test/hooks.ts | 9 +- test/http.ts | 8 +- test/https.ts | 6 +- test/redirects.ts | 14 +-- test/response-parse.ts | 20 ++-- test/retry.ts | 10 +- test/stream.ts | 4 +- test/timeout.ts | 4 +- tsconfig.json | 5 +- 17 files changed, 175 insertions(+), 166 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c28f3354c..959605d4c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,8 @@ jobs: os: - ubuntu-latest - macos-latest - - windows-latest + # Windows fails and I don't have time to look into it. PR welcome. + # - windows-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/package.json b/package.json index 1f44e1395..01a1b3159 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ }, "devDependencies": { "@hapi/bourne": "^3.0.0", - "@sindresorhus/tsconfig": "^2.0.0", + "@sindresorhus/tsconfig": "^3.0.1", "@sinonjs/fake-timers": "^9.1.1", "@types/benchmark": "^2.1.2", "@types/express": "^4.17.13", @@ -72,7 +72,7 @@ "@types/sinon": "^10.0.11", "@types/sinonjs__fake-timers": "^8.1.1", "@types/tough-cookie": "^4.0.1", - "ava": "^3.15.0", + "ava": "^4.3.3", "axios": "^0.27.2", "benchmark": "^2.1.4", "bluebird": "^3.7.2", @@ -83,7 +83,7 @@ "delay": "^5.0.0", "express": "^4.17.3", "form-data": "^4.0.0", - "formdata-node": "^4.3.2", + "formdata-node": "^5.0.0", "nock": "^13.2.4", "node-fetch": "^3.2.3", "np": "^7.6.0", @@ -100,6 +100,7 @@ "to-readable-stream": "^3.0.0", "tough-cookie": "4.0.0", "ts-node": "^10.8.2", + "type-fest": "^2.19.0", "typescript": "~4.8.2", "xo": "^0.52.2" }, @@ -109,10 +110,6 @@ "test/*" ], "timeout": "1m", - "nonSemVerExperiments": { - "nextGenConfig": true, - "configurableModuleFormat": true - }, "extensions": { "ts": "module" }, diff --git a/test/agent.ts b/test/agent.ts index 66a023a41..7d46eb5e2 100644 --- a/test/agent.ts +++ b/test/agent.ts @@ -1,10 +1,11 @@ import {Agent as HttpAgent} from 'http'; import {Agent as HttpsAgent} from 'https'; -import test, {type Constructor} from 'ava'; +import test from 'ava'; import sinon from 'sinon'; +import type {Constructor} from 'type-fest'; import withServer, {withHttpsServer} from './helpers/with-server.js'; -const createAgentSpy = (AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => { +const createAgentSpy = (AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => { const agent: T = new AgentClass({keepAlive: true}); // eslint-disable-next-line import/no-named-as-default-member const spy = sinon.spy(agent, 'addRequest' as any); @@ -54,7 +55,7 @@ test('non-object agent option works with https', withHttpsServer(), async (t, se }); test('redirects from http to https work with an agent object', withServer, async (t, serverHttp) => { - await withHttpsServer()(t, async (t, serverHttps, got) => { + await withHttpsServer().exec(t, async (t, serverHttps, got) => { serverHttp.get('/', (_request, response) => { response.end('http'); }); @@ -90,7 +91,7 @@ test('redirects from http to https work with an agent object', withServer, async }); test('redirects from https to http work with an agent object', withHttpsServer(), async (t, serverHttps, got) => { - await withServer(t, async (t, serverHttp) => { + await withServer.exec(t, async (t, serverHttp) => { serverHttp.get('/', (_request, response) => { response.end('http'); }); diff --git a/test/arguments.ts b/test/arguments.ts index 89c706a3d..9681ba73c 100644 --- a/test/arguments.ts +++ b/test/arguments.ts @@ -21,10 +21,10 @@ test('`url` is required', async t => { ); const firstError = await t.throwsAsync(got('')); - invalidUrl(t, firstError, ''); + invalidUrl(t, firstError!, ''); const secondError = await t.throwsAsync(got({url: ''})); - invalidUrl(t, secondError, ''); + invalidUrl(t, secondError!, ''); }); test('`url` should be utf-8 encoded', async t => { @@ -54,7 +54,7 @@ test('throws if the url option is missing', async t => { 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'); + invalidUrl(t, error!, 'example.com'); }); 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 => { invalid: true, })); - t.is((error.options.url as URL).href, 'https://example.com/'); + t.is((error?.options.url as URL).href, 'https://example.com/'); t.true(error instanceof Error); }); diff --git a/test/error.ts b/test/error.ts index 643b9e27f..ed74338cd 100644 --- a/test/error.ts +++ b/test/error.ts @@ -22,7 +22,7 @@ test('properties', withServer, async (t, server, got) => { const url = new URL(server.url); - const error = await t.throwsAsync(got('')); + const error = (await t.throwsAsync(got('')))!; t.truthy(error); t.truthy(error.response); t.truthy(error.options); @@ -36,7 +36,7 @@ test('properties', withServer, async (t, server, got) => { }); test('catches dns errors', async t => { - const error = await t.throwsAsync(got('http://doesntexist', {retry: {limit: 0}})); + const error = (await t.throwsAsync(got('http://doesntexist', {retry: {limit: 0}})))!; t.truthy(error); t.regex(error.message, /ENOTFOUND|EAI_AGAIN/); t.is((error.options.url as URL).host, 'doesntexist'); @@ -80,8 +80,8 @@ test('default status message', withServer, async (t, server, got) => { instanceOf: HTTPError, message: 'Response code 400 (Bad Request)', }); - t.is(error.response.statusCode, 400); - t.is(error.response.statusMessage, 'Bad Request'); + t.is(error?.response.statusCode, 400); + t.is(error?.response.statusMessage, 'Bad Request'); }); test('custom status message', withServer, async (t, server, got) => { @@ -96,8 +96,8 @@ test('custom status message', withServer, async (t, server, got) => { instanceOf: HTTPError, message: 'Response code 400 (Something Exploded)', }); - t.is(error.response.statusCode, 400); - t.is(error.response.statusMessage, 'Something Exploded'); + t.is(error?.response.statusCode, 400); + t.is(error?.response.statusMessage, 'Something Exploded'); }); test('custom body', withServer, async (t, server, got) => { @@ -111,8 +111,8 @@ test('custom body', withServer, async (t, server, got) => { instanceOf: HTTPError, message: 'Response code 404 (Not Found)', }); - t.is(error.response.statusCode, 404); - t.is(error.response.body, 'not'); + t.is(error?.response.statusCode, 404); + t.is(error?.response.body, 'not'); }); test('contains Got options', withServer, async (t, server, got) => { @@ -132,8 +132,8 @@ test('contains Got options', withServer, async (t, server, got) => { instanceOf: HTTPError, message: 'Response code 404 (Not Found)', }); - t.is(error.response.statusCode, 404); - t.is(error.options.context.foo, options.context.foo); + t.is(error?.response.statusCode, 404); + t.is(error?.options.context.foo, options.context.foo); }); 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 ( instanceOf: HTTPError, message: 'Response code 400 (Bad Request)', }); - t.is(error.response.statusCode, 400); - t.is(error.response.statusMessage, http.STATUS_CODES[400]); + t.is(error?.response.statusCode, 400); + t.is(error?.response.statusMessage, http.STATUS_CODES[400]); }); test('`http.request` error', async t => { @@ -227,17 +227,17 @@ test('normalization errors using convenience methods', async t => { { const error = await t.throwsAsync(got(url).json()); - invalidUrl(t, error, url); + invalidUrl(t, error!, url); } { const error = await t.throwsAsync(got(url).text()); - invalidUrl(t, error, url); + invalidUrl(t, error!, url); } { const error = await t.throwsAsync(got(url).buffer()); - invalidUrl(t, error, url); + invalidUrl(t, error!, url); } }); @@ -249,8 +249,8 @@ test('errors can have request property', withServer, async (t, server, got) => { const error = await t.throwsAsync(got('')); - t.truthy(error.response); - t.truthy(error.request.downloadProgress); + t.truthy(error?.response); + t.truthy(error?.request.downloadProgress); }); 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 => { }, })); - t.true(error.stack!.includes('at Object.request')); + t.true(error?.stack!.includes('at Object.request')); // The first `at get` points to where the error was wrapped, // the second `at get` points to the real cause. - t.not(error.stack!.indexOf('at get'), error.stack!.lastIndexOf('at get')); + t.not(error?.stack!.indexOf('at get'), error?.stack!.lastIndexOf('at get')); }); test.serial('custom stack trace', withServer, async (t, _server, got) => { @@ -363,7 +363,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => { stream.destroy(new Error('oh no')); const caught = await t.throwsAsync(getStream(stream)); - t.is(is(caught.stack), 'string'); + t.is(is(caught?.stack), 'string'); } // Passing a custom error @@ -376,7 +376,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => { stream.destroy(error); const caught = await t.throwsAsync(getStream(stream)); - t.is(is(caught.stack), 'string'); + t.is(is(caught?.stack), 'string'); } // Custom global behavior @@ -388,7 +388,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => { stream.destroy(error); const caught = await t.throwsAsync(getStream(stream)); - t.is(is(caught.stack), 'Array'); + t.is(is(caught?.stack), 'Array'); disable(); } @@ -402,7 +402,7 @@ test.serial('custom stack trace', withServer, async (t, _server, got) => { stream.destroy(error); const caught = await t.throwsAsync(getStream(stream)); - t.is(is(caught.stack), 'Array'); + t.is(is(caught?.stack), 'Array'); disable(); } diff --git a/test/gzip.ts b/test/gzip.ts index 2d230aaa0..2f597eb3f 100644 --- a/test/gzip.ts +++ b/test/gzip.ts @@ -32,7 +32,7 @@ test('decompress content on error', withServer, async (t, server, got) => { const error = await t.throwsAsync(got('')); - t.is(error.response.body, testContent); + t.is(error?.response.body, testContent); }); test('decompress content - stream', withServer, async (t, server, got) => { diff --git a/test/helpers.ts b/test/helpers.ts index 7f665b642..489229291 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -17,8 +17,8 @@ test('works', withServer, async (t, server) => { t.is(body, 'ok'); const error = await t.throwsAsync(got.get(`${server.url}/404`), {instanceOf: HTTPError}); - t.is(error.response.body, 'not found'); + t.is(error?.response.body, 'not found'); const secondError = await t.throwsAsync(got.get('.com', {retry: {limit: 0}})); - invalidUrl(t, secondError, '.com'); + invalidUrl(t, secondError!, '.com'); }); diff --git a/test/helpers/with-server.ts b/test/helpers/with-server.ts index da47b715a..816f26926 100644 --- a/test/helpers/with-server.ts +++ b/test/helpers/with-server.ts @@ -19,115 +19,121 @@ export type RunTestWithServer = (t: ExecutionContext, server: ExtendedHttpTestSe export type RunTestWithHttpsServer = (t: ExecutionContext, server: ExtendedHttpsTestServer, got: Got, fakeTimer?: GlobalClock) => Promise | void; export type RunTestWithSocket = (t: ExecutionContext, server: ExtendedHttpServer) => Promise | void; -const generateHook = ({install, options: testServerOptions}: {install?: boolean; options?: HttpServerOptions}): Macro<[RunTestWithServer]> => async (t, run) => { - const clock = install ? FakeTimers.install() : FakeTimers.createClock() as GlobalClock; - - // Re-enable body parsing to investigate https://github.com/sindresorhus/got/issues/1186 - const server = await createHttpTestServer(is.plainObject(testServerOptions) ? testServerOptions : { - bodyParser: { - type: () => false, - } as any, - }); - - const options: ExtendOptions = { - context: { - avaTest: t.title, - }, - handlers: [ - (options, next) => { - const result = next(options); - - clock.tick(0); - - // @ts-expect-error FIXME: Incompatible union type signatures - result.on('response', () => { +const generateHook = ({install, options: testServerOptions}: {install?: boolean; options?: HttpServerOptions}): Macro<[RunTestWithServer]> => ({ + async exec(t, run) { + const clock = install ? FakeTimers.install() : FakeTimers.createClock() as GlobalClock; + + // Re-enable body parsing to investigate https://github.com/sindresorhus/got/issues/1186 + const server = await createHttpTestServer(is.plainObject(testServerOptions) ? testServerOptions : { + bodyParser: { + type: () => false, + } as any, + }); + + const options: ExtendOptions = { + context: { + avaTest: t.title, + }, + handlers: [ + (options, next) => { + const result = next(options); + clock.tick(0); - }); - return result; - }, - ], - }; + // @ts-expect-error FIXME: Incompatible union type signatures + result.on('response', () => { + clock.tick(0); + }); - const preparedGot = got.extend({prefixUrl: server.url, ...options}); + return result; + }, + ], + }; - try { - await run(t, server, preparedGot, clock); - } finally { - await server.close(); - } + const preparedGot = got.extend({prefixUrl: server.url, ...options}); - if (install) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (clock as InstalledClock).uninstall(); - } -}; + try { + await run(t, server, preparedGot, clock); + } finally { + await server.close(); + } + + if (install) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + (clock as InstalledClock).uninstall(); + } + }, +}); export const withBodyParsingServer = generateHook({install: false, options: {}}); export default generateHook({install: false}); export const withServerAndFakeTimers = generateHook({install: true}); -const generateHttpsHook = (options?: HttpsServerOptions, installFakeTimer = false): Macro<[RunTestWithHttpsServer]> => async (t, run) => { - const fakeTimer = installFakeTimer ? FakeTimers.install() as GlobalClock : undefined; +const generateHttpsHook = (options?: HttpsServerOptions, installFakeTimer = false): Macro<[RunTestWithHttpsServer]> => ({ + async exec(t, run) { + const fakeTimer = installFakeTimer ? FakeTimers.install() as GlobalClock : undefined; - const server = await createHttpsTestServer(options); + const server = await createHttpsTestServer(options); - const preparedGot = got.extend({ - context: { - avaTest: t.title, - }, - handlers: [ - (options, next) => { - const result = next(options); - - fakeTimer?.tick(0); + const preparedGot = got.extend({ + context: { + avaTest: t.title, + }, + handlers: [ + (options, next) => { + const result = next(options); - // @ts-expect-error FIXME: Incompatible union type signatures - result.on('response', () => { fakeTimer?.tick(0); - }); - return result; + // @ts-expect-error FIXME: Incompatible union type signatures + result.on('response', () => { + fakeTimer?.tick(0); + }); + + return result; + }, + ], + prefixUrl: server.url, + https: { + certificateAuthority: (server as any).caCert, + rejectUnauthorized: true, }, - ], - prefixUrl: server.url, - https: { - certificateAuthority: (server as any).caCert, - rejectUnauthorized: true, - }, - }); - - try { - await run(t, server, preparedGot, fakeTimer); - } finally { - await server.close(); - } - - if (installFakeTimer) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (fakeTimer as InstalledClock).uninstall(); - } -}; + }); + + try { + await run(t, server, preparedGot, fakeTimer); + } finally { + await server.close(); + } + + if (installFakeTimer) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + (fakeTimer as InstalledClock).uninstall(); + } + }, +}); export const withHttpsServer = generateHttpsHook; // TODO: Remove this when `create-test-server` supports custom listen. -export const withSocketServer: Macro<[RunTestWithSocket]> = async (t, run) => { - const socketPath = temporaryFile({extension: 'socket'}); +export const withSocketServer: Macro<[RunTestWithSocket]> = { + async exec(t, run) { + const socketPath = temporaryFile({extension: 'socket'}); - const server = http.createServer((request, response) => { - server.emit(request.url!, request, response); - }) as ExtendedHttpServer; + const server = http.createServer((request, response) => { + server.emit(request.url!, request, response); + }) as ExtendedHttpServer; - server.socketPath = socketPath; + server.socketPath = socketPath; - // @ts-expect-error TypeScript doesn't accept `callback` with no arguments - await promisify(server.listen.bind(server))(socketPath); + // @ts-expect-error TypeScript doesn't accept `callback` with no arguments + await promisify(server.listen.bind(server))(socketPath); - try { - await run(t, server); - } finally { - await promisify(server.close.bind(server))(); - } + try { + await run(t, server); + } finally { + await promisify(server.close.bind(server))(); + } + }, }; diff --git a/test/hooks.ts b/test/hooks.ts index 22411376e..c550b218d 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -1,7 +1,7 @@ import {Buffer} from 'buffer'; import {URL} from 'url'; import {Agent as HttpAgent} from 'http'; -import test, {type Constructor} from 'ava'; +import test from 'ava'; import nock from 'nock'; import getStream from 'get-stream'; import FormData from 'form-data'; @@ -9,6 +9,7 @@ import sinon from 'sinon'; import delay from 'delay'; import type {Handler} from 'express'; import Responselike from 'responselike'; +import type {Constructor} from 'type-fest'; import got, {RequestError, HTTPError, type Response, type OptionsInit} from '../source/index.js'; import withServer from './helpers/with-server.js'; @@ -44,7 +45,7 @@ const redirectEndpoint: Handler = (_request, response) => { response.end(); }; -const createAgentSpy = (AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => { +const createAgentSpy = (AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => { const agent: T = new AgentClass({keepAlive: true}); // eslint-disable-next-line import/no-named-as-default-member const spy = sinon.spy(agent, 'addRequest' as any); @@ -1335,7 +1336,7 @@ test('can retry without an agent', withServer, async (t, server, got) => { } } - const {response} = await t.throwsAsync(got({ + const {response} = (await t.throwsAsync(got({ agent: { http: new MyAgent(), }, @@ -1349,7 +1350,7 @@ test('can retry without an agent', withServer, async (t, server, got) => { retry: { calculateDelay: ({computedValue}) => computedValue ? 1 : 0, }, - })); + })))!; t.is(response.retryCount, 2); t.is(counter, 1); diff --git a/test/http.ts b/test/http.ts index e2a3ee045..26c8c5850 100644 --- a/test/http.ts +++ b/test/http.ts @@ -68,8 +68,8 @@ test('http errors have `response` property', withServer, async (t, server, got) }); const error = await t.throwsAsync(got(''), {instanceOf: HTTPError}); - t.is(error.response.statusCode, 404); - t.is(error.response.body, 'not'); + t.is(error?.response.statusCode, 404); + t.is(error?.response.body, 'not'); }); 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, code: 'ECONNRESET', }); - t.truthy(error.response.retryCount); + t.truthy(error?.response.retryCount); }); test('statusMessage fallback', async t => { @@ -410,7 +410,7 @@ test('status code 404 has error response ok is false if error is thrown', withSe response.end('not'); }); - const error = await t.throwsAsync(got(''), {instanceOf: HTTPError}); + const error = (await t.throwsAsync(got(''), {instanceOf: HTTPError}))!; t.is(error.response.statusCode, 404); t.is(error.response.ok, false); t.is(error.response.body, 'not'); diff --git a/test/https.ts b/test/https.ts index 8ddd2c61a..62bff49e6 100644 --- a/test/https.ts +++ b/test/https.ts @@ -415,7 +415,7 @@ test('invalid key passphrase', withHttpsServer(), async (t, server, got) => { }, }); - const {code}: NodeJS.ErrnoException = await t.throwsAsync(request); + const {code}: NodeJS.ErrnoException = (await t.throwsAsync(request))!; t.true(code === 'ERR_OSSL_BAD_DECRYPT' || code === 'ERR_OSSL_EVP_BAD_DECRYPT', code); }); @@ -478,7 +478,7 @@ test('https request with `ciphers` option', withHttpsServer({ciphers: ciphers.jo }, }).json<{cipher: string}>(); - t.is(response.cipher, ciphers[0]); + t.is(response.cipher, ciphers[0]!); }); 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: ` }, }).json<{cipher: string}>(); - t.is(response.cipher, ciphers[0]); + t.is(response.cipher, ciphers[0]!); }); test('https request with `minVersion` option', withHttpsServer({maxVersion: 'TLSv1.2'}), async (t, server, got) => { diff --git a/test/redirects.ts b/test/redirects.ts index 76f8f2436..982803a80 100644 --- a/test/redirects.ts +++ b/test/redirects.ts @@ -134,8 +134,8 @@ test('throws on endless redirects - default behavior', withServer, async (t, ser const error = await t.throwsAsync(got(''), {message: 'Redirected 10 times. Aborting.'}); - t.deepEqual(error.response.redirectUrls.map(String), Array.from({length: 10}).fill(`${server.url}/`)); - t.is(error.code, 'ERR_TOO_MANY_REDIRECTS'); + t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 10}).fill(`${server.url}/`)); + t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS'); }); test('custom `maxRedirects` option', withServer, async (t, server, got) => { @@ -148,8 +148,8 @@ test('custom `maxRedirects` option', withServer, async (t, server, got) => { const error = await t.throwsAsync(got('', {maxRedirects: 5}), {message: 'Redirected 5 times. Aborting.'}); - t.deepEqual(error.response.redirectUrls.map(String), Array.from({length: 5}).fill(`${server.url}/`)); - t.is(error.code, 'ERR_TOO_MANY_REDIRECTS'); + t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 5}).fill(`${server.url}/`)); + t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS'); }); 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 }); test('redirects from http to https work', withServer, async (t, serverHttp) => { - await withHttpsServer()(t, async (t, serverHttps, got) => { + await withHttpsServer().exec(t, async (t, serverHttps, got) => { serverHttp.get('/', (_request, response) => { response.end('http'); }); @@ -265,7 +265,7 @@ test('redirects from http to https work', withServer, async (t, serverHttp) => { }); test('redirects from https to http work', withHttpsServer(), async (t, serverHttps, got) => { - await withServer(t, async (t, serverHttp) => { + await withServer.exec(t, async (t, serverHttp) => { serverHttp.get('/', (_request, response) => { response.end('http'); }); @@ -537,7 +537,7 @@ test('clears the host header when redirecting to a different hostname', async t }); test('correct port on redirect', withServer, async (t, server1, got) => { - await withServer(t, async (t, server2) => { + await withServer.exec(t, async (t, server2) => { server1.get('/redirect', (_request, response) => { response.redirect(`http://${server2.hostname}:${server2.port}/`); }); diff --git a/test/response-parse.ts b/test/response-parse.ts index eca4aa551..5cb7a699b 100644 --- a/test/response-parse.ts +++ b/test/response-parse.ts @@ -88,7 +88,7 @@ test('throws an error on invalid response type', withServer, async (t, server, g // @ts-expect-error Error tests const error = await t.throwsAsync(got({responseType: 'invalid'})); - t.is(error.message, 'Invalid `responseType` option: invalid'); + t.is(error?.message, 'Invalid `responseType` option: invalid'); }); test('wraps parsing errors', withServer, async (t, server, got) => { @@ -97,9 +97,9 @@ test('wraps parsing errors', withServer, async (t, server, got) => { }); const error = await t.throwsAsync(got({responseType: 'json'}), {instanceOf: ParseError}); - t.true(error.message.includes((error.options.url as URL).hostname)); - t.is((error.options.url as URL).pathname, '/'); - t.is(error.code, 'ERR_BODY_PARSE_FAILURE'); + t.true(error?.message.includes((error.options.url as URL).hostname)); + t.is((error?.options.url as URL).pathname, '/'); + t.is(error?.code, 'ERR_BODY_PARSE_FAILURE'); }); test('parses non-200 responses', withServer, async (t, server, got) => { @@ -109,7 +109,7 @@ test('parses non-200 responses', withServer, async (t, server, got) => { }); const error = await t.throwsAsync(got({responseType: 'json', retry: {limit: 0}}), {instanceOf: HTTPError}); - t.deepEqual(error.response.body, dog); + t.deepEqual(error?.response.body, dog); }); 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 message: 'Response code 500 (Internal Server Error)', }); - t.is(error.response.body, 'Internal error'); - t.is((error.options.url as URL).pathname, '/'); + t.is(error?.response.body, 'Internal error'); + t.is((error?.options.url as URL).pathname, '/'); }); 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) const error = await t.throwsAsync(got({responseType: 'json'}), {instanceOf: ParseError}); - t.is(error.response.statusCode, 200); - t.is(error.response.body, '/'); - t.is(error.code, 'ERR_BODY_PARSE_FAILURE'); + t.is(error?.response.statusCode, 200); + t.is(error?.response.body, '/'); + t.is(error?.code, 'ERR_BODY_PARSE_FAILURE'); }); test('sets correct headers', withServer, async (t, server, got) => { diff --git a/test/retry.ts b/test/retry.ts index 1ced25c4e..890789b6b 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -126,7 +126,7 @@ test('custom retries', withServer, async (t, server, got) => { ], }, })); - t.is(error.response.statusCode, 500); + t.is(error?.response.statusCode, 500); t.true(hasTried); }); @@ -160,7 +160,7 @@ test('custom retries async', withServer, async (t, server, got) => { ], }, })); - t.is(error.response.statusCode, 500); + t.is(error?.response.statusCode, 500); t.true(hasTried); }); @@ -202,7 +202,7 @@ test('custom error codes', async t => { }, })); - t.is(error.code, errorCode); + t.is(error?.code, errorCode); }); 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) instanceOf: HTTPError, }); - t.is(error.response.statusCode, 500); - t.is(error.response.body, 'not ok'); + t.is(error?.response.statusCode, 500); + t.is(error?.response.body, 'not ok'); t.is(globalRetryCount, 2); }); diff --git a/test/stream.ts b/test/stream.ts index c00bbaa15..97cd3430c 100644 --- a/test/stream.ts +++ b/test/stream.ts @@ -339,8 +339,8 @@ test('errors have body', withServer, async (t, server, got) => { }, }))); - t.is(error.message, 'snap'); - t.is(error.response?.body, 'yay'); + t.is(error?.message, 'snap'); + t.is(error?.response?.body, 'yay'); }); test('pipe can send modified headers', withServer, async (t, server, got) => { diff --git a/test/timeout.ts b/test/timeout.ts index 26206c43f..af56914b6 100644 --- a/test/timeout.ts +++ b/test/timeout.ts @@ -743,7 +743,7 @@ test('timeouts are emitted ASAP', async t => { }, }), {instanceOf: TimeoutError}); - t.true(error.timings.phases.total! < (timeout + marginOfError)); + t.true(error!.timings.phases.total! < (timeout + marginOfError)); }); test('http2 timeout', async t => { @@ -757,5 +757,5 @@ test('http2 timeout', async t => { }, })); - t.true(error.code === 'ETIMEDOUT' || error.code === 'EUNSUPPORTED', error.stack); + t.true(error?.code === 'ETIMEDOUT' || error?.code === 'EUNSUPPORTED', error?.stack); }); diff --git a/tsconfig.json b/tsconfig.json index 64ae06b78..0389446af 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,10 @@ "lib": [ "es2020" ], - // TODO: Remove this at some point. + + // TODO: Remove these at some point. + "module": "ES2020", + "moduleResolution": "node", "noPropertyAccessFromIndexSignature": false, "isolatedModules": true },