Skip to content

Commit f21a50c

Browse files
authored
Merge pull request #11 from dennisameling/node-fetch
Use node-fetch for fetchJSONFromURL
2 parents a852cb9 + 83d11bd commit f21a50c

File tree

5 files changed

+1704
-82
lines changed

5 files changed

+1704
-82
lines changed

__tests__/downloader.test.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {ClientRequest, IncomingMessage} from 'http'
2-
import https from 'https'
3-
import {mocked} from 'ts-jest/utils'
41
import {get} from '../src/downloader'
2+
import {mocked} from 'ts-jest/utils'
3+
import fetch from 'node-fetch'
54

65
const buildIdResponse = {
76
count: 1,
@@ -43,38 +42,14 @@ const buildIdResponse = {
4342
]
4443
}
4544

46-
jest.mock('https')
47-
48-
declare type Callback = (data?: object) => void
45+
jest.mock('node-fetch')
46+
const {Response} = jest.requireActual('node-fetch')
4947

5048
test('can obtain build ID', async () => {
51-
mocked(https.request).mockImplementation(
52-
(
53-
_url,
54-
_options,
55-
callback: ((res: IncomingMessage) => void) | undefined
56-
): ClientRequest => {
57-
const res = {
58-
statusCode: 200,
59-
on: (eventType: string, eventCallback: Callback): object => {
60-
switch (eventType) {
61-
case 'data':
62-
eventCallback(Buffer.from(JSON.stringify(buildIdResponse)))
63-
break
64-
case 'end':
65-
eventCallback()
66-
break
67-
}
68-
return res
69-
}
70-
} as IncomingMessage
71-
expect(callback).not.toBeUndefined()
72-
callback && callback(res)
73-
const req = {} as ClientRequest
74-
Object.assign(req, {on: () => {}})
75-
return req
76-
}
49+
mocked(fetch).mockReturnValue(
50+
Promise.resolve(new Response(JSON.stringify(buildIdResponse)))
7751
)
7852
const {id} = await get('minimal', 'x86_64')
53+
expect(fetch).toHaveBeenCalledTimes(1)
7954
expect(id).toEqual('git-sdk-64-minimal-71000')
8055
})

0 commit comments

Comments
 (0)