Skip to content

Commit

Permalink
test(http): Disable http retries for tests inside constructor (#25711)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Nov 12, 2023
1 parent 0373661 commit 75df23b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
56 changes: 32 additions & 24 deletions lib/util/http/index.spec.ts
Expand Up @@ -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 });

Expand Down Expand Up @@ -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() })
Expand Down
7 changes: 4 additions & 3 deletions lib/util/http/index.ts
Expand Up @@ -132,6 +132,10 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
options: HttpOptions = {},
) {
this.options = merge<GotOptions>(options, { context: { hostType } });

if (process.env.NODE_ENV === 'test') {
this.options.retry = 0;
}
}

protected getThrottle(url: string): Throttle | null {
Expand Down Expand Up @@ -167,9 +171,6 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
};
}

if (process.env.NODE_ENV === 'test') {
options.retry = 0;
}
options.hooks = {
beforeRedirect: [removeAuthorization],
};
Expand Down

0 comments on commit 75df23b

Please sign in to comment.