Skip to content

Commit

Permalink
test: fix nock usage (renovatebot#10451)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jun 16, 2021
1 parent c7113f3 commit 5546d60
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 183 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ module.exports = {
},
],

// disallow direct `nock` module usage as it causes memory issues.
'no-restricted-imports': [2, { paths: ['nock'] }],

// Makes no sense to allow type inference for expression parameters, but require typing the response
'@typescript-eslint/explicit-function-return-type': [
'error',
Expand Down
14 changes: 8 additions & 6 deletions lib/config/presets/npm/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import nock from 'nock';
import * as httpMock from '../../../../test/http-mock';
import { getName } from '../../../../test/util';
import { setAdminConfig } from '../../admin';
import * as npm from '.';
Expand All @@ -10,13 +10,12 @@ describe(getName(), () => {
beforeEach(() => {
jest.resetAllMocks();
setAdminConfig();
nock.cleanAll();
});
afterEach(() => {
delete process.env.RENOVATE_CACHE_NPM_MINUTES;
});
it('should throw if no package', async () => {
nock('https://registry.npmjs.org').get('/nopackage').reply(404);
httpMock.scope('https://registry.npmjs.org').get('/nopackage').reply(404);
await expect(
npm.getPreset({ packageName: 'nopackage', presetName: 'default' })
).rejects.toThrow(/dep not found/);
Expand Down Expand Up @@ -45,7 +44,8 @@ describe(getName(), () => {
'0.0.2': '2018-05-07T07:21:53+02:00',
},
};
nock('https://registry.npmjs.org')
httpMock
.scope('https://registry.npmjs.org')
.get('/norenovateconfig')
.reply(200, presetPackage);
await expect(
Expand Down Expand Up @@ -77,7 +77,8 @@ describe(getName(), () => {
'0.0.2': '2018-05-07T07:21:53+02:00',
},
};
nock('https://registry.npmjs.org')
httpMock
.scope('https://registry.npmjs.org')
.get('/presetnamenotfound')
.reply(200, presetPackage);
await expect(
Expand Down Expand Up @@ -112,7 +113,8 @@ describe(getName(), () => {
'0.0.2': '2018-05-07T07:21:53+02:00',
},
};
nock('https://registry.npmjs.org')
httpMock
.scope('https://registry.npmjs.org')
.get('/workingpreset')
.reply(200, presetPackage);
const res = await npm.getPreset({ packageName: 'workingpreset' });
Expand Down
80 changes: 51 additions & 29 deletions lib/datasource/sbt-package/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import nock from 'nock';
import { getPkgReleases } from '..';
import * as httpMock from '../../../test/http-mock';
import { getName, loadFixture } from '../../../test/util';
import * as mavenVersioning from '../../versioning/maven';
import { MAVEN_REPO } from '../maven/common';
import { parseIndexDir } from '../sbt-plugin/util';
import * as sbtPlugin from '.';
import * as sbtPackage from '.';

const mavenIndexHtml = loadFixture(`maven-index.html`);
const sbtPluginIndex = loadFixture(`sbt-plugins-index.html`);
Expand All @@ -13,21 +13,27 @@ describe(getName(), () => {
it('parses Maven index directory', () => {
expect(parseIndexDir(mavenIndexHtml)).toMatchSnapshot();
});

it('parses sbt index directory', () => {
expect(parseIndexDir(sbtPluginIndex)).toMatchSnapshot();
});

describe('getPkgReleases', () => {
beforeEach(() => {
nock.disableNetConnect();
nock('https://failed_repo').get('/maven/org/scalatest/').reply(404, null);
nock('https://repo.maven.apache.org')
httpMock
.scope('https://failed_repo')
.get('/maven/org/scalatest/')
.reply(404, null);
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/com/example/')
.reply(200, '<a href="empty/">empty_2.12/</a>\n');
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/com/example/empty/')
.reply(200, '');
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/')
.times(3)
.reply(
Expand All @@ -40,22 +46,28 @@ describe(getName(), () => {
'<a href="scalatest-flatspec_2.12/">scalatest-flatspec_2.12</a>' +
'<a href="scalatest-matchers-core_2.12/">scalatest-matchers-core_2.12</a>'
);
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest/')
.reply(200, "<a href='1.2.0/'>1.2.0/</a>");
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest_2.12/')
.reply(200, "<a href='1.2.3/'>4.5.6/</a>");
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest-app_2.12/')
.reply(200, "<a href='6.5.4/'>3.2.1/</a>");
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest-flatspec_2.12/')
.reply(200, "<a href='6.5.4/'>3.2.1/</a>");
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest-matchers-core_2.12/')
.reply(200, "<a href='6.5.4/'>3.2.1/</a>");
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get(
'/maven2/org/scalatest/scalatest-app_2.12/6.5.4/scalatest-app_2.12-6.5.4.pom'
)
Expand All @@ -68,7 +80,8 @@ describe(getName(), () => {
'</scm>' +
'</project>'
);
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get(
'/maven2/org/scalatest/scalatest-flatspec_2.12/6.5.4/scalatest-flatspec_2.12-6.5.4.pom'
)
Expand All @@ -80,7 +93,8 @@ describe(getName(), () => {
'</scm>' +
'</project>'
);
nock('https://repo.maven.apache.org')
httpMock
.scope('https://repo.maven.apache.org')
.get(
'/maven2/org/scalatest/scalatest-matchers-core_2.12/6.5.4/scalatest-matchers-core_2.12-6.5.4.pom'
)
Expand All @@ -91,10 +105,12 @@ describe(getName(), () => {
'</project>'
);

nock('https://dl.bintray.com')
httpMock
.scope('https://dl.bintray.com')
.get('/sbt/sbt-plugin-releases/com.github.gseitz/')
.reply(200, '');
nock('https://dl.bintray.com')
httpMock
.scope('https://dl.bintray.com')
.get('/sbt/sbt-plugin-releases/org.foundweekends/sbt-bintray/')
.reply(
200,
Expand All @@ -106,7 +122,8 @@ describe(getName(), () => {
'</body>\n' +
'</html>'
);
nock('https://dl.bintray.com')
httpMock
.scope('https://dl.bintray.com')
.get(
'/sbt/sbt-plugin-releases/org.foundweekends/sbt-bintray/scala_2.12/'
)
Expand All @@ -121,7 +138,8 @@ describe(getName(), () => {
'</body>\n' +
'</html>\n'
);
nock('https://dl.bintray.com')
httpMock
.scope('https://dl.bintray.com')
.get(
'/sbt/sbt-plugin-releases/org.foundweekends/sbt-bintray/scala_2.12/sbt_1.0/'
)
Expand All @@ -138,35 +156,36 @@ describe(getName(), () => {
);
});

afterEach(() => {
nock.enableNetConnect();
});
// TODO: fix mocks
afterEach(() => httpMock.clear(false));

it('returns null in case of errors', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest',
registryUrls: ['https://failed_repo/maven'],
})
).toBeNull();
});

it('returns null if there is no version', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'com.example:empty',
registryUrls: [],
})
).toBeNull();
});

it('fetches releases from Maven', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest',
registryUrls: ['https://failed_repo/maven', MAVEN_REPO],
})
Expand All @@ -175,10 +194,13 @@ describe(getName(), () => {
registryUrl: 'https://repo.maven.apache.org/maven2',
releases: [{ version: '1.2.0' }, { version: '1.2.3' }],
});
});

it('fetches releases from Maven 2', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest_2.12',
registryUrls: [],
})
Expand All @@ -193,7 +215,7 @@ describe(getName(), () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest-app_2.12',
registryUrls: [],
})
Expand All @@ -207,7 +229,7 @@ describe(getName(), () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest-flatspec_2.12',
registryUrls: [],
})
Expand All @@ -220,7 +242,7 @@ describe(getName(), () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: sbtPlugin.id,
datasource: sbtPackage.id,
depName: 'org.scalatest:scalatest-matchers-core_2.12',
registryUrls: [],
})
Expand Down
Loading

0 comments on commit 5546d60

Please sign in to comment.