From 75df23bfe4f418d4a6c77bf4e0a8d3a9e381e92d Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Sun, 12 Nov 2023 16:05:37 -0300 Subject: [PATCH] test(http): Disable http retries for tests inside constructor (#25711) --- lib/util/http/index.spec.ts | 56 +++++++++++++++++++++---------------- lib/util/http/index.ts | 7 +++-- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/util/http/index.spec.ts b/lib/util/http/index.spec.ts index a2e0f121ba3483..eb9aadd63bac5b 100644 --- a/lib/util/http/index.spec.ts +++ b/lib/util/http/index.spec.ts @@ -193,30 +193,6 @@ describe('util/http/index', () => { ); }); - it('retries', async () => { - const NODE_ENV = process.env.NODE_ENV; - try { - delete process.env.NODE_ENV; - httpMock - .scope(baseUrl) - .head('/') - .reply(500) - .head('/') - .reply(200, undefined, { 'x-some-header': 'abc' }); - expect(await http.head('http://renovate.com')).toEqual({ - authorization: false, - body: '', - headers: { - 'x-some-header': 'abc', - }, - statusCode: 200, - }); - expect(httpMock.allUsed()).toBeTrue(); - } finally { - process.env.NODE_ENV = NODE_ENV; - } - }); - it('limits concurrency by host', async () => { hostRules.add({ matchHost: 'renovate.com', concurrentRequestLimit: 1 }); @@ -314,6 +290,38 @@ describe('util/http/index', () => { expect(res?.body.toString('utf-8')).toBe('test'); }); + describe('retry', () => { + let NODE_ENV: string | undefined; + + beforeAll(() => { + NODE_ENV = process.env.NODE_ENV; + delete process.env.NODE_ENV; + http = new Http('dummy'); + }); + + afterAll(() => { + process.env.NODE_ENV = NODE_ENV; + }); + + it('works', async () => { + httpMock + .scope(baseUrl) + .head('/') + .reply(500) + .head('/') + .reply(200, undefined, { 'x-some-header': 'abc' }); + expect(await http.head('http://renovate.com')).toEqual({ + authorization: false, + body: '', + headers: { + 'x-some-header': 'abc', + }, + statusCode: 200, + }); + expect(httpMock.allUsed()).toBeTrue(); + }); + }); + describe('Schema support', () => { const SomeSchema = z .object({ x: z.number(), y: z.number() }) diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index f0dfd338a6f66b..1f7421a710ac0d 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -132,6 +132,10 @@ export class Http { options: HttpOptions = {}, ) { this.options = merge(options, { context: { hostType } }); + + if (process.env.NODE_ENV === 'test') { + this.options.retry = 0; + } } protected getThrottle(url: string): Throttle | null { @@ -167,9 +171,6 @@ export class Http { }; } - if (process.env.NODE_ENV === 'test') { - options.retry = 0; - } options.hooks = { beforeRedirect: [removeAuthorization], };