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 1) #6510

Merged
merged 1 commit into from Jun 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
150 changes: 150 additions & 0 deletions lib/datasource/cdnjs/__snapshots__/index.spec.ts.snap
Expand Up @@ -13,6 +13,21 @@ Object {
}
`;

exports[`datasource/cdnjs getReleases filters releases by asset presence 2`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/bulma?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases processes real data 1`] = `
Object {
"homepage": "https://d3js.org/d3-force/",
Expand Down Expand Up @@ -125,3 +140,138 @@ Object {
"sourceUrl": "https://github.com/d3/d3-force.git",
}
`;

exports[`datasource/cdnjs getReleases processes real data 2`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/d3-force?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases returns null for 404 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases returns null for empty 200 OK 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/doesnotexist?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases returns null for unknown error 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases throws for 5xx 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases throws for 401 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases throws for 429 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases throws for empty result 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;

exports[`datasource/cdnjs getReleases throws for error 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "api.cdnjs.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.cdnjs.com/libraries/foo?fields=homepage,repository,assets",
},
]
`;
59 changes: 42 additions & 17 deletions lib/datasource/cdnjs/index.spec.ts
@@ -1,11 +1,8 @@
import fs from 'fs';
import * as httpMock from '../../../test/httpMock';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import _got from '../../util/got';
import { getReleases } from '.';

const got: jest.Mock<any> = _got as any;
jest.mock('../../util/got');

let res1 = fs.readFileSync(
'lib/datasource/cdnjs/__fixtures__/d3-force.json',
'utf8'
Expand All @@ -18,70 +15,98 @@ let res2 = fs.readFileSync(
);
res2 = JSON.parse(res2);

const baseUrl = 'https://api.cdnjs.com/';

const pathFor = (s: string): string =>
`/libraries/${s.split('/').shift()}?fields=homepage,repository,assets`;

describe('datasource/cdnjs', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.clearAllMocks();
httpMock.setup();
});

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

it('throws for empty result', async () => {
got.mockResolvedValueOnce(null);
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(200, null);
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for missing fields', async () => {
got.mockResolvedValueOnce({});
it('throws for error', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
got.mockRejectedValueOnce({ statusCode: 404 });
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(404);
expect(await getReleases({ lookupName: 'foo/bar' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for empty 200 OK', async () => {
got.mockResolvedValueOnce({ body: {} });
httpMock
.scope(baseUrl)
.get(pathFor('doesnotexist/doesnotexist'))
.reply(200, {});
expect(
await getReleases({ lookupName: 'doesnotexist/doesnotexist' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 401', async () => {
got.mockRejectedValueOnce({ statusCode: 401 });
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(401);
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 429', async () => {
got.mockRejectedValueOnce({ statusCode: 429 });
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(429);
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
got.mockRejectedValueOnce({ statusCode: 502 });
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(502);
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
got.mockImplementationOnce(() => {
throw new Error();
});
httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data', async () => {
got.mockResolvedValueOnce({ body: res1 });
httpMock
.scope(baseUrl)
.get(pathFor('d3-force/d3-force.js'))
.reply(200, res1);
const res = await getReleases({ lookupName: 'd3-force/d3-force.js' });
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('filters releases by asset presence', async () => {
got.mockResolvedValueOnce({ body: res2 });
httpMock
.scope(baseUrl)
.get(pathFor('bulma/only/0.7.5/style.css'))
.reply(200, res2);
const res = await getReleases({
lookupName: 'bulma/only/0.7.5/style.css',
});
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});