|
1 |
| -import {ClientRequest, IncomingMessage} from 'http' |
2 |
| -import https from 'https' |
3 |
| -import {mocked} from 'ts-jest/utils' |
4 | 1 | import {get} from '../src/downloader'
|
| 2 | +import {mocked} from 'ts-jest/utils' |
| 3 | +import fetch from 'node-fetch' |
5 | 4 |
|
6 | 5 | const buildIdResponse = {
|
7 | 6 | count: 1,
|
@@ -43,38 +42,14 @@ const buildIdResponse = {
|
43 | 42 | ]
|
44 | 43 | }
|
45 | 44 |
|
46 |
| -jest.mock('https') |
47 |
| - |
48 |
| -declare type Callback = (data?: object) => void |
| 45 | +jest.mock('node-fetch') |
| 46 | +const {Response} = jest.requireActual('node-fetch') |
49 | 47 |
|
50 | 48 | 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))) |
77 | 51 | )
|
78 | 52 | const {id} = await get('minimal', 'x86_64')
|
| 53 | + expect(fetch).toHaveBeenCalledTimes(1) |
79 | 54 | expect(id).toEqual('git-sdk-64-minimal-71000')
|
80 | 55 | })
|
0 commit comments