Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Use nock for tests (Part 2) #6519

Merged
merged 3 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
545 changes: 545 additions & 0 deletions lib/datasource/crate/__snapshots__/index.spec.ts.snap

Large diffs are not rendered by default.

62 changes: 26 additions & 36 deletions lib/datasource/crate/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import fs from 'fs';
import * as httpMock from '../../../test/httpMock';

import _got from '../../util/got';
import { getReleases } from '.';

const got: any = _got;

const res1 = fs.readFileSync('lib/datasource/crate/__fixtures__/libc', 'utf8');
const res2 = fs.readFileSync(
'lib/datasource/crate/__fixtures__/amethyst',
Expand All @@ -15,46 +13,42 @@ const res3 = fs.readFileSync(
'utf8'
);

jest.mock('../../util/got');
const baseUrl =
'https://raw.githubusercontent.com/rust-lang/crates.io-index/master/';

describe('datasource/crate', () => {
describe('getReleases', () => {
it('returns null for empty result', async () => {
got.mockReturnValueOnce(null);
httpMock.scope(baseUrl).get('/no/n_/non_existent_crate').reply(200, {});
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for missing fields', async () => {
got.mockReturnValueOnce({
body: undefined,
});
httpMock
.scope(baseUrl)
.get('/no/n_/non_existent_crate')
.reply(200, undefined);
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for empty list', async () => {
got.mockReturnValueOnce({
body: '\n',
});
httpMock.scope(baseUrl).get('/no/n_/non_existent_crate').reply(200, '\n');
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
got.mockImplementationOnce(() =>
Promise.reject({
statusCode: 404,
})
);
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(404);
expect(await getReleases({ lookupName: 'some_crate' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
got.mockImplementationOnce(() =>
Promise.reject({
statusCode: 502,
})
);
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(502);
let e;
try {
await getReleases({ lookupName: 'some_crate' });
Expand All @@ -63,44 +57,40 @@ describe('datasource/crate', () => {
}
expect(e).toBeDefined();
expect(e).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
got.mockImplementationOnce(() => {
throw new Error();
});
httpMock.scope(baseUrl).get('/so/me/some_crate').replyWithError('');
expect(await getReleases({ lookupName: 'some_crate' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data: libc', async () => {
got.mockReturnValueOnce({
body: res1,
});
httpMock.scope(baseUrl).get('/li/bc/libc').reply(200, res1);
const res = await getReleases({ lookupName: 'libc' });
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data: amethyst', async () => {
got.mockReturnValueOnce({
body: res2,
});
httpMock.scope(baseUrl).get('/am/et/amethyst').reply(200, res2);
const res = await getReleases({ lookupName: 'amethyst' });
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null if crate name is invalid', async () => {
got.mockReturnValueOnce({
body: res2,
});
httpMock.scope(baseUrl).get('/in/va/invalid-crate-name').reply(200, res2);
const res = await getReleases({ lookupName: 'invalid-crate-name' });
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for invalid crate data', async () => {
got.mockReturnValueOnce({
body: res3,
});
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(200, res3);
const res = await getReleases({ lookupName: 'some_crate' });
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});
100 changes: 100 additions & 0 deletions lib/datasource/dart/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,104 @@ Object {
}
`;

exports[`datasource/dart getReleases processes real data 2`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
]
`;

exports[`datasource/dart getReleases returns null for 404 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
]
`;

exports[`datasource/dart getReleases returns null for empty fields 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
]
`;

exports[`datasource/dart getReleases returns null for empty result 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/non_sense",
},
]
`;

exports[`datasource/dart getReleases returns null for unknown error 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
]
`;

exports[`datasource/dart getReleases throws for 5xx 1`] = `[Error: registry-failure]`;

exports[`datasource/dart getReleases throws for 5xx 2`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "pub.dartlang.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://pub.dartlang.org/api/packages/shared_preferences",
},
]
`;
51 changes: 30 additions & 21 deletions lib/datasource/dart/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import fs from 'fs';
import _got from '../../util/got';
import * as httpMock from '../../../test/httpMock';
import { getReleases } from '.';

const got: any = _got;

const body: any = JSON.parse(
fs.readFileSync(
'lib/datasource/dart/__fixtures__/shared_preferences.json',
'utf8'
)
);

jest.mock('../../util/got');
const baseUrl = 'https://pub.dartlang.org/api/packages/';

describe('datasource/dart', () => {
beforeEach(() => {
httpMock.setup();
});

afterEach(() => {
httpMock.reset();
});

describe('getReleases', () => {
it('returns null for empty result', async () => {
got.mockReturnValueOnce(null);
httpMock.scope(baseUrl).get('/non_sense').reply(200, null);
expect(await getReleases({ lookupName: 'non_sense' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for empty fields', async () => {
const withoutVersions = {
...body,
versions: undefined,
};
got.mockReturnValueOnce({ body: withoutVersions });
httpMock
.scope(baseUrl)
.get('/shared_preferences')
.reply(200, withoutVersions);
expect(
await getReleases({ lookupName: 'shared_preferences' })
).toBeNull();
Expand All @@ -33,27 +43,25 @@ describe('datasource/dart', () => {
...body,
latest: undefined,
};
got.mockReturnValueOnce({ body: withoutLatest });
httpMock
.scope(baseUrl)
.get('/shared_preferences')
.reply(200, withoutLatest);
expect(
await getReleases({ lookupName: 'shared_preferences' })
).toBeNull();

expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
got.mockImplementationOnce(() =>
Promise.reject({
statusCode: 404,
})
);
httpMock.scope(baseUrl).get('/shared_preferences').reply(404);
expect(
await getReleases({ lookupName: 'shared_preferences' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
got.mockImplementationOnce(() =>
Promise.reject({
statusCode: 502,
})
);
httpMock.scope(baseUrl).get('/shared_preferences').reply(502);
let e;
try {
await getReleases({ lookupName: 'shared_preferences' });
Expand All @@ -62,21 +70,22 @@ describe('datasource/dart', () => {
}
expect(e).toBeDefined();
expect(e).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
got.mockImplementationOnce(() => {
throw new Error();
});
httpMock.scope(baseUrl).get('/shared_preferences').replyWithError('');
expect(
await getReleases({ lookupName: 'shared_preferences' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data', async () => {
got.mockReturnValueOnce({ body });
httpMock.scope(baseUrl).get('/shared_preferences').reply(200, body);
const res = await getReleases({
lookupName: 'shared_preferences',
});
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});
1 change: 0 additions & 1 deletion lib/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ describe('api/docker', () => {
datasource: docker.id,
depName: 'k8s.gcr.io/kubernetes-dashboard-amd64',
});
httpMock.getTrace();
expect(res.releases).toHaveLength(1);
expect(httpMock.getTrace()).toMatchSnapshot();
});
Expand Down
1 change: 0 additions & 1 deletion lib/datasource/galaxy/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ describe('datasource/galaxy', () => {
.get('/api/v1/roles/?owner__username=foo&name=bar')
.reply(200, empty);
const res = await getReleases({ lookupName: 'foo.bar' });
httpMock.getTrace();
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
Expand Down
1 change: 0 additions & 1 deletion lib/datasource/gitlab-tags/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('datasource/gitlab-tags', () => {
registryUrls: ['https://gitlab.company.com/api/v4/'],
lookupName: 'some/dep2',
});
httpMock.getTrace();
expect(res).toMatchSnapshot();
expect(res.releases).toHaveLength(3);
expect(httpMock.getTrace()).toMatchSnapshot();
Expand Down
Loading