/
index.spec.ts
59 lines (49 loc) · 1.37 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
import nock from 'nock';
import { getName } from '../../../test/util';
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import * as hostRules from '../host-rules';
import { GotJSONOptions } from './common';
import { api } from '.';
const baseUrl = 'https://api.github.com';
describe(getName(__filename), () => {
beforeEach(() => {
nock.disableNetConnect();
});
afterEach(() => {
nock.cleanAll();
hostRules.clear();
nock.enableNetConnect();
});
async function got(opts?: Partial<GotJSONOptions>) {
const { body, request } = (await api('some', {
method: 'GET',
baseUrl,
json: true,
...opts,
})) as any;
return { body, options: request.gotOptions };
}
function mock(opts?: nock.Options, times = 1) {
return nock(baseUrl, opts).get('/some').times(times).reply(200, {});
}
it('gets', async () => {
const req = mock({})
.head('/some')
.reply(200, {})
.get('/some')
.replyWithError('not-found');
expect(
await got({
hostType: PLATFORM_TYPE_GITHUB,
useCache: false,
})
).toMatchSnapshot();
expect(
await got({ hostType: PLATFORM_TYPE_GITHUB, method: 'HEAD' })
).toMatchSnapshot();
await expect(got({ hostType: PLATFORM_TYPE_GITHUB })).rejects.toThrow(
'not-found'
);
expect(req.isDone()).toBe(true);
});
});