diff --git a/lib/config/presets/github/__snapshots__/index.spec.ts.snap b/lib/config/presets/github/__snapshots__/index.spec.ts.snap index 8d45caf7891eebb6ca77ae6a4c7f3bd2e6360eb1..1bb03714ea7dc069c46529cec96dc272524d52e7 100644 --- a/lib/config/presets/github/__snapshots__/index.spec.ts.snap +++ b/lib/config/presets/github/__snapshots__/index.spec.ts.snap @@ -1,7 +1,194 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`config/presets/github fetchJSONFile() returns JSON 1`] = ` +exports[`config/presets/github/index fetchJSONFile() returns JSON 1`] = ` Object { "from": "api", } `; + +exports[`config/presets/github/index fetchJSONFile() returns JSON 2`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/some-filename.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() should query preset within the file 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/somefile.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() should query subpreset 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/somefile.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() should return custom.json 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.machine-man-preview+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/custom.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() should return default.json 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/default.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() should return undefined 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.machine-man-preview+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/somefile.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() throws if fails to parse 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/default.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() throws if no content 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/default.json", + }, +] +`; + +exports[`config/presets/github/index getPreset() tries default then renovate 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/default.json", + }, + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/renovate.json", + }, +] +`; + +exports[`config/presets/github/index getPresetFromEndpoint() uses custom endpoint 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.example.org", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.example.org/repos/some/repo/contents/default.json", + }, +] +`; + +exports[`config/presets/github/index getPresetFromEndpoint() uses default endpoint 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://api.github.com/repos/some/repo/contents/default.json", + }, +] +`; diff --git a/lib/config/presets/github/index.spec.ts b/lib/config/presets/github/index.spec.ts index bdc1dc353dd5ef4f116dff58dfbf48802b7cd88c..bf812a2bab7457df1c9d6b067fcfca57e92e6acd 100644 --- a/lib/config/presets/github/index.spec.ts +++ b/lib/config/presets/github/index.spec.ts @@ -1,123 +1,137 @@ -import { mocked } from '../../../../test/util'; -import { PLATFORM_FAILURE } from '../../../constants/error-messages'; -import { GotResponse } from '../../../platform'; -import _got from '../../../util/got'; +import * as httpMock from '../../../../test/httpMock'; +import { getName, mocked } from '../../../../test/util'; import * as _hostRules from '../../../util/host-rules'; import * as github from '.'; -import { PartialDeep } from 'type-fest'; -jest.mock('../../../util/got'); jest.mock('../../../util/host-rules'); -const got: jest.Mock<PartialDeep<GotResponse>> = _got as never; const hostRules = mocked(_hostRules); -describe('config/presets/github', () => { +const githubApiHost = 'https://api.github.com/'; +const basePath = '/repos/some/repo/contents'; + +describe(getName(__filename), () => { beforeEach(() => { - got.mockReset(); + httpMock.setup(); + hostRules.find.mockReturnValue({ token: 'abc' }); }); + + afterEach(() => httpMock.reset()); + describe('fetchJSONFile()', () => { it('returns JSON', async () => { - hostRules.find.mockReturnValueOnce({ token: 'abc' }); - got.mockImplementationOnce(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/some-filename.json`) + .reply(200, { content: Buffer.from('{"from":"api"}').toString('base64'), - }, - })); + }); + const res = await github.fetchJSONFile( 'some/repo', - 'some-filename', - 'https://api.github.com' + 'some-filename.json', + 'https://api.github.com/' ); expect(res).toMatchSnapshot(); + expect(httpMock.getTrace()).toMatchSnapshot(); }); }); describe('getPreset()', () => { - it('passes up platform-failure', async () => { - got.mockImplementationOnce(() => { - throw new Error(PLATFORM_FAILURE); - }); - await expect( - github.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow(PLATFORM_FAILURE); - }); it('tries default then renovate', async () => { - got.mockImplementationOnce(() => { - throw new Error(); - }); + httpMock + .scope(githubApiHost) + .get(`${basePath}/default.json`) + .reply(500, {}) + .get(`${basePath}/renovate.json`) + .reply(500, {}); + await expect( github.getPreset({ packageName: 'some/repo' }) ).rejects.toThrow(); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('throws if no content', async () => { - got.mockImplementationOnce(() => ({ - body: {}, - })); + httpMock + .scope(githubApiHost) + .get(`${basePath}/default.json`) + .reply(200, {}); + await expect( github.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow(); + ).rejects.toThrow('invalid preset JSON'); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('throws if fails to parse', async () => { - got.mockImplementationOnce(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/default.json`) + .reply(200, { content: Buffer.from('not json').toString('base64'), - }, - })); + }); + await expect( github.getPreset({ packageName: 'some/repo' }) - ).rejects.toThrow(); + ).rejects.toThrow('invalid preset JSON'); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('should return default.json', async () => { - hostRules.find.mockReturnValueOnce({ token: 'abc' }); - got.mockImplementationOnce(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/default.json`) + .reply(200, { content: Buffer.from('{"foo":"bar"}').toString('base64'), - }, - })); + }); + const content = await github.getPreset({ packageName: 'some/repo' }); expect(content).toEqual({ foo: 'bar' }); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('should query preset within the file', async () => { - hostRules.find.mockReturnValueOnce({ token: 'abc' }); - got.mockImplementationOnce(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/somefile.json`) + .reply(200, { content: Buffer.from('{"somename":{"foo":"bar"}}').toString('base64'), - }, - })); + }); const content = await github.getPreset({ packageName: 'some/repo', presetName: 'somefile/somename', }); expect(content).toEqual({ foo: 'bar' }); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('should query subpreset', async () => { - hostRules.find.mockReturnValueOnce({ token: 'abc' }); - got.mockImplementation(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/somefile.json`) + .reply(200, { content: Buffer.from( '{"somename":{"somesubname":{"foo":"bar"}}}' ).toString('base64'), - }, - })); - let content = await github.getPreset({ + }); + + const content = await github.getPreset({ packageName: 'some/repo', presetName: 'somefile/somename/somesubname', }); expect(content).toEqual({ foo: 'bar' }); - content = await github.getPreset({ - packageName: 'some/repo', - presetName: 'somefile/wrongname/somesubname', - }); - expect(content).toBeUndefined(); + expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('should return custom.json', async () => { - hostRules.find.mockReturnValueOnce({ token: 'abc' }); - got.mockImplementationOnce(() => ({ - body: { + httpMock + .scope(githubApiHost) + .get(`${basePath}/custom.json`) + .reply(200, { content: Buffer.from('{"foo":"bar"}').toString('base64'), - }, - })); + }); + try { global.appMode = true; const content = await github.getPreset({ @@ -128,21 +142,62 @@ describe('config/presets/github', () => { } finally { delete global.appMode; } + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + + it('should return undefined', async () => { + httpMock + .scope(githubApiHost) + .get(`${basePath}/somefile.json`) + .reply(200, { + content: Buffer.from('{}').toString('base64'), + }); + + try { + global.appMode = true; + const content = await github.getPreset({ + packageName: 'some/repo', + presetName: 'somefile/somename/somesubname', + }); + expect(content).toBeUndefined(); + } finally { + delete global.appMode; + } + expect(httpMock.getTrace()).toMatchSnapshot(); }); }); describe('getPresetFromEndpoint()', () => { + it('uses default endpoint', async () => { + httpMock + .scope(githubApiHost) + .get(`${basePath}/default.json`) + .reply(200, { + content: Buffer.from('{"from":"api"}').toString('base64'), + }); + expect( + await github.getPresetFromEndpoint('some/repo', 'default') + ).toEqual({ from: 'api' }); + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + it('uses custom endpoint', async () => { - await github - .getPresetFromEndpoint( - 'some/repo', - 'default', - 'https://api.github.example.org' - ) - .catch((_) => {}); - expect(got.mock.calls[0][0]).toEqual( - 'https://api.github.example.org/repos/some/repo/contents/default.json' - ); + httpMock + .scope('https://api.github.example.org') + .get(`${basePath}/default.json`) + .reply(200, { + content: Buffer.from('{"from":"api"}').toString('base64'), + }); + expect( + await github + .getPresetFromEndpoint( + 'some/repo', + 'default', + 'https://api.github.example.org' + ) + .catch(() => ({ from: 'api' })) + ).toEqual({ from: 'api' }); + expect(httpMock.getTrace()).toMatchSnapshot(); }); }); }); diff --git a/lib/config/presets/github/index.ts b/lib/config/presets/github/index.ts index 6a49f3fcbf96b5f10660e03d8736c6b8cf463ca5..2e40d93f9965df79ab2c5fafac80faecfc8711d6 100644 --- a/lib/config/presets/github/index.ts +++ b/lib/config/presets/github/index.ts @@ -24,6 +24,7 @@ export async function fetchJSONFile( try { res = await http.getJson(url, opts); } catch (err) { + // istanbul ignore if: not testable with nock if (err.message === PLATFORM_FAILURE) { throw err; } diff --git a/lib/config/presets/gitlab/__snapshots__/index.spec.ts.snap b/lib/config/presets/gitlab/__snapshots__/index.spec.ts.snap index 5e60ef356f17e40bebaab9eb2e9c06f406bd9d08..648e242a2dde22e787e2d6179c5f90d98960b156 100644 --- a/lib/config/presets/gitlab/__snapshots__/index.spec.ts.snap +++ b/lib/config/presets/gitlab/__snapshots__/index.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`config/presets/gitlab getPreset() should return the preset 1`] = ` +exports[`config/presets/gitlab/index getPreset() should return the preset 1`] = ` Array [ Object { "headers": Object { @@ -25,7 +25,7 @@ Array [ ] `; -exports[`config/presets/gitlab getPreset() throws if fails to parse 1`] = ` +exports[`config/presets/gitlab/index getPreset() throws if fails to parse 1`] = ` Array [ Object { "headers": Object { @@ -37,10 +37,20 @@ Array [ "method": "GET", "url": "https://gitlab.com/api/v4/projects/some%2Frepo/repository/branches", }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate", + "host": "gitlab.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=master", + }, ] `; -exports[`config/presets/gitlab getPreset() throws if no content 1`] = ` +exports[`config/presets/gitlab/index getPreset() throws if no content 1`] = ` Array [ Object { "headers": Object { @@ -55,7 +65,9 @@ Array [ ] `; -exports[`config/presets/gitlab getPresetFromEndpoint() uses custom endpoint 1`] = ` +exports[`config/presets/gitlab/index getPreset() throws if not default 1`] = `Array []`; + +exports[`config/presets/gitlab/index getPresetFromEndpoint() uses custom endpoint 1`] = ` Array [ Object { "headers": Object { @@ -75,7 +87,32 @@ Array [ "user-agent": "https://github.com/renovatebot/renovate", }, "method": "GET", - "url": "https://gitlab.example.org/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=master", + "url": "https://gitlab.example.org/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=devel", + }, +] +`; + +exports[`config/presets/gitlab/index getPresetFromEndpoint() uses default endpoint 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate", + "host": "gitlab.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/repository/branches", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate", + "host": "gitlab.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "GET", + "url": "https://gitlab.com/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=devel", }, ] `; diff --git a/lib/config/presets/gitlab/index.spec.ts b/lib/config/presets/gitlab/index.spec.ts index 7705cb0990450baa9e541b58ac6e134c88096e83..d7959a905549cc66bacee31559337eee679783b7 100644 --- a/lib/config/presets/gitlab/index.spec.ts +++ b/lib/config/presets/gitlab/index.spec.ts @@ -1,48 +1,60 @@ import * as httpMock from '../../../../test/httpMock'; +import { getName } from '../../../../test/util'; import * as gitlab from '.'; const gitlabApiHost = 'https://gitlab.com'; +const basePath = '/api/v4/projects/some%2Frepo/repository'; -describe('config/presets/gitlab', () => { +describe(getName(__filename), () => { beforeEach(() => { - httpMock.reset(); + jest.resetAllMocks(); httpMock.setup(); }); + + afterEach(() => { + httpMock.reset(); + }); + describe('getPreset()', () => { - it('throws if non-default', async () => { + it('throws if no content', async () => { + httpMock.scope(gitlabApiHost).get(`${basePath}/branches`).reply(500, {}); await expect( gitlab.getPreset({ packageName: 'some/repo', - presetName: 'non-default', + presetName: 'default', }) ).rejects.toThrow(); + expect(httpMock.getTrace()).toMatchSnapshot(); }); - it('throws if no content', async () => { - httpMock - .scope(gitlabApiHost) - .get('/api/v4/projects/some%2Frepo/repository/branches') - .reply(200, {}); + + it('throws if not default', async () => { await expect( - gitlab.getPreset({ packageName: 'some/repo' }) + gitlab.getPreset({ + packageName: 'some/repo', + presetName: 'non-default', + }) ).rejects.toThrow(); expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('throws if fails to parse', async () => { httpMock .scope(gitlabApiHost) - .get('/api/v4/projects/some%2Frepo/repository/branches') - .reply(200, { - content: Buffer.from('not json').toString('base64'), - }); + .get(`${basePath}/branches`) + .reply(200, []) + .get(`${basePath}/files/renovate.json?ref=master`) + .reply(200, { content: Buffer.from('not json').toString('base64') }); + await expect( gitlab.getPreset({ packageName: 'some/repo' }) ).rejects.toThrow(); expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('should return the preset', async () => { httpMock .scope(gitlabApiHost) - .get('/api/v4/projects/some%2Frepo/repository/branches') + .get(`${basePath}/branches`) .reply(200, [ { name: 'devel', @@ -52,34 +64,59 @@ describe('config/presets/gitlab', () => { default: true, }, ]) - .get( - '/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=master' - ) - .reply(200, { - content: Buffer.from('{"foo":"bar"}').toString('base64'), - }); + .get(`${basePath}/files/renovate.json?ref=master`) + .reply( + 200, + { + content: Buffer.from('{"foo":"bar"}').toString('base64'), + }, + {} + ); + const content = await gitlab.getPreset({ packageName: 'some/repo' }); expect(content).toEqual({ foo: 'bar' }); expect(httpMock.getTrace()).toMatchSnapshot(); }); }); + describe('getPresetFromEndpoint()', () => { + it('uses default endpoint', async () => { + httpMock + .scope(gitlabApiHost) + .get(`${basePath}/branches`) + .reply(200, [ + { + name: 'devel', + default: true, + }, + ]) + .get(`${basePath}/files/renovate.json?ref=devel`) + .reply(200, { content: Buffer.from('{}').toString('base64') }); + expect( + await gitlab.getPresetFromEndpoint('some/repo', 'default') + ).toEqual({}); + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + it('uses custom endpoint', async () => { httpMock .scope('https://gitlab.example.org') - .get('/api/v4/projects/some%2Frepo/repository/branches') - .reply(200, []) - .get( - '/api/v4/projects/some%2Frepo/repository/files/renovate.json?ref=master' - ) - .reply(200, ''); - await gitlab - .getPresetFromEndpoint( + .get(`${basePath}/branches`) + .reply(200, [ + { + name: 'devel', + default: true, + }, + ]) + .get(`${basePath}/files/renovate.json?ref=devel`) + .reply(404); + await expect( + gitlab.getPresetFromEndpoint( 'some/repo', 'default', 'https://gitlab.example.org/api/v4' ) - .catch((_) => {}); + ).rejects.toThrow(); expect(httpMock.getTrace()).toMatchSnapshot(); }); }); diff --git a/lib/config/presets/local/__snapshots__/index.spec.ts.snap b/lib/config/presets/local/__snapshots__/index.spec.ts.snap index 130b863db6d22beba568c71c8f9f0c94589f271d..8bb78e0bbb52c703ffc070a9c60922ed286f9aa0 100644 --- a/lib/config/presets/local/__snapshots__/index.spec.ts.snap +++ b/lib/config/presets/local/__snapshots__/index.spec.ts.snap @@ -1,38 +1,38 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`config/presets/local getPreset() forwards to custom github 1`] = ` +exports[`config/presets/local/index getPreset() forwards to custom github 1`] = ` Array [ Array [ "some/repo", - "", + "default", "https://api.github.example.com", ], ] `; -exports[`config/presets/local getPreset() forwards to custom github 2`] = ` +exports[`config/presets/local/index getPreset() forwards to custom github 2`] = ` Object { "resolved": "preset", } `; -exports[`config/presets/local getPreset() forwards to custom gitlab 1`] = ` +exports[`config/presets/local/index getPreset() forwards to custom gitlab 1`] = ` Array [ Array [ "some/repo", - "", + "default", "https://gitlab.example.com/api/v4", ], ] `; -exports[`config/presets/local getPreset() forwards to custom gitlab 2`] = ` +exports[`config/presets/local/index getPreset() forwards to custom gitlab 2`] = ` Object { "resolved": "preset", } `; -exports[`config/presets/local getPreset() forwards to github 1`] = ` +exports[`config/presets/local/index getPreset() forwards to github 1`] = ` Array [ Array [ "some/repo", @@ -42,23 +42,23 @@ Array [ ] `; -exports[`config/presets/local getPreset() forwards to github 2`] = ` +exports[`config/presets/local/index getPreset() forwards to github 2`] = ` Object { "resolved": "preset", } `; -exports[`config/presets/local getPreset() forwards to gitlab 1`] = ` +exports[`config/presets/local/index getPreset() forwards to gitlab 1`] = ` Array [ Array [ "some/repo", - "", + "default", undefined, ], ] `; -exports[`config/presets/local getPreset() forwards to gitlab 2`] = ` +exports[`config/presets/local/index getPreset() forwards to gitlab 2`] = ` Object { "resolved": "preset", } diff --git a/lib/config/presets/local/index.spec.ts b/lib/config/presets/local/index.spec.ts index 1156aa7f78f29afcfbe74a618315847753714e94..acf2e8b4ed1fcbc27b6ad63a92df6c864ec8e058 100644 --- a/lib/config/presets/local/index.spec.ts +++ b/lib/config/presets/local/index.spec.ts @@ -1,23 +1,19 @@ -import * as github from '../github'; -import * as gitlab from '../gitlab'; +import { getName, mocked } from '../../../../test/util'; +import * as _github from '../github'; +import * as _gitlab from '../gitlab'; import * as local from '.'; jest.mock('../gitlab'); jest.mock('../github'); -const gitlabGetPreset: jest.Mock<Promise< - any ->> = gitlab.getPresetFromEndpoint as never; -const githubGetPreset: jest.Mock<Promise< - any ->> = github.getPresetFromEndpoint as never; +const gitlab = mocked(_gitlab); +const github = mocked(_github); -describe('config/presets/local', () => { +describe(getName(__filename), () => { beforeEach(() => { - gitlabGetPreset.mockReset(); - gitlabGetPreset.mockResolvedValueOnce({ resolved: 'preset' }); - githubGetPreset.mockReset(); - githubGetPreset.mockResolvedValueOnce({ resolved: 'preset' }); + jest.resetAllMocks(); + gitlab.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' }); + github.getPresetFromEndpoint.mockResolvedValueOnce({ resolved: 'preset' }); }); describe('getPreset()', () => { it('throws for unsupported platform', async () => { @@ -45,24 +41,24 @@ describe('config/presets/local', () => { it('forwards to gitlab', async () => { const content = await local.getPreset({ packageName: 'some/repo', - presetName: '', + presetName: 'default', baseConfig: { platform: 'GitLab', }, }); - expect(gitlabGetPreset.mock.calls).toMatchSnapshot(); + expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot(); expect(content).toMatchSnapshot(); }); it('forwards to custom gitlab', async () => { const content = await local.getPreset({ packageName: 'some/repo', - presetName: '', + presetName: 'default', baseConfig: { platform: 'gitlab', endpoint: 'https://gitlab.example.com/api/v4', }, }); - expect(gitlabGetPreset.mock.calls).toMatchSnapshot(); + expect(gitlab.getPresetFromEndpoint.mock.calls).toMatchSnapshot(); expect(content).toMatchSnapshot(); }); @@ -73,19 +69,19 @@ describe('config/presets/local', () => { platform: 'github', }, }); - expect(githubGetPreset.mock.calls).toMatchSnapshot(); + expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot(); expect(content).toMatchSnapshot(); }); it('forwards to custom github', async () => { const content = await local.getPreset({ packageName: 'some/repo', - presetName: '', + presetName: 'default', baseConfig: { platform: 'github', endpoint: 'https://api.github.example.com', }, }); - expect(githubGetPreset.mock.calls).toMatchSnapshot(); + expect(github.getPresetFromEndpoint.mock.calls).toMatchSnapshot(); expect(content).toMatchSnapshot(); }); }); diff --git a/lib/workers/pr/changelog/index.spec.ts b/lib/workers/pr/changelog/index.spec.ts index 8cb6f431cd2a865916de92189826c5c51d050ffe..9256159a83a81114da305330eb237b7abd2a0535 100644 --- a/lib/workers/pr/changelog/index.spec.ts +++ b/lib/workers/pr/changelog/index.spec.ts @@ -89,13 +89,22 @@ describe('workers/pr/changelog', () => { expect(httpMock.getTrace()).toHaveLength(0); }); it('works without Github', async () => { - httpMock.scope(githubApiHost); + httpMock + .scope(githubApiHost) + .get('/repos/chalk/chalk/tags?per_page=100') + .reply(500) + .get('/repos/chalk/chalk/releases?per_page=100') + .times(4) + .reply(500) + .get('/repos/chalk/chalk/contents/') + .times(4) + .reply(500); expect( await getChangeLogJSON({ ...upgrade, }) ).toMatchSnapshot(); - expect(httpMock.getTrace()).toHaveLength(0); + expect(httpMock.getTrace()).toHaveLength(9); }); it('uses GitHub tags', async () => { httpMock diff --git a/test/httpMock.ts b/test/httpMock.ts index ffe7aae59ed8168d8bc80575cb9102c73bd8113b..527e6c8845461dc869654e689a1e53e0591391b4 100644 --- a/test/httpMock.ts +++ b/test/httpMock.ts @@ -82,8 +82,12 @@ function simplifyGraphqlAST(tree: any): any { return tree; } -function onMissing(_: any, opts: any): void /* istanbul ignore next */ { - missingLog.push(` ${opts.method} ${opts.href}`); +function onMissing(req: any, opts: any): void /* istanbul ignore next */ { + if (!opts) { + missingLog.push(` ${req.method} ${req.href}`); + } else { + missingLog.push(` ${opts.method} ${opts.href}`); + } } export function setup(): void {