forked from renovatebot/renovate
/
index.spec.ts
122 lines (115 loc) · 3.94 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import fs from 'fs';
import { getPkgReleases } from '..';
import * as httpMock from '../../../test/httpMock';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import { id as datasource } from '.';
let res1 = fs.readFileSync(
'lib/datasource/cdnjs/__fixtures__/d3-force.json',
'utf8'
);
res1 = JSON.parse(res1);
let res2 = fs.readFileSync(
'lib/datasource/cdnjs/__fixtures__/bulma.json',
'utf8'
);
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 () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(200, null);
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for error', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(404);
expect(
await getPkgReleases({ datasource, depName: 'foo/bar' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for empty 200 OK', async () => {
httpMock
.scope(baseUrl)
.get(pathFor('doesnotexist/doesnotexist'))
.reply(200, {});
expect(
await getPkgReleases({
datasource,
depName: 'doesnotexist/doesnotexist',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 401', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(401);
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 429', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(429);
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).reply(502);
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
httpMock.scope(baseUrl).get(pathFor('foo/bar')).replyWithError('error');
await expect(
getPkgReleases({ datasource, depName: 'foo/bar' })
).rejects.toThrow(DATASOURCE_FAILURE);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data', async () => {
httpMock
.scope(baseUrl)
.get(pathFor('d3-force/d3-force.js'))
.reply(200, res1);
const res = await getPkgReleases({
datasource,
depName: 'd3-force/d3-force.js',
});
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('filters releases by asset presence', async () => {
httpMock
.scope(baseUrl)
.get(pathFor('bulma/only/0.7.5/style.css'))
.reply(200, res2);
const res = await getPkgReleases({
datasource,
depName: 'bulma/only/0.7.5/style.css',
});
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});