This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.test.ts
132 lines (116 loc) · 4.04 KB
/
github.test.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
123
124
125
126
127
128
129
130
131
132
import {expect, test} from '@jest/globals'
import {Job, fetchJobs, fetchLogs} from '../src/github'
import {HttpClient, HttpClientResponse} from '@actions/http-client'
import {IncomingMessage} from 'http'
import {mock, instance, when, anything, reset, capture} from 'ts-mockito'
import jobsJson from './jobs.json'
let mockedHttpClient: HttpClient = mock(HttpClient)
let mockedResponse: HttpClientResponse = mock(HttpClientResponse)
let mockedMessage: IncomingMessage = mock(IncomingMessage)
describe('Test jobs list retrieval', () => {
beforeEach(() => {
reset(mockedHttpClient)
reset(mockedResponse)
reset(mockedMessage)
})
afterEach(() => {})
test('Retrieve the list of jobs', async () => {
when(mockedMessage.statusCode).thenReturn(200)
when(mockedResponse.message).thenReturn(instance(mockedMessage))
when(mockedResponse.readBody()).thenResolve(JSON.stringify(jobsJson))
when(mockedHttpClient.get(anything())).thenResolve(instance(mockedResponse))
let jobs: Job[] = await fetchJobs(
instance(mockedHttpClient),
'masci/foo',
'123',
[]
)
const expected: Job[] = [
{
id: 3734144061,
name: 'test'
},
{
id: 3734144148,
name: 'e2e'
}
]
expect(jobs).toEqual(expected)
// also verify the url used in the HTTP request (first param passed to get)
const url = capture(mockedHttpClient.get).last()[0]
expect(url).toEqual(
'https://api.github.com/repos/masci/foo/actions/runs/123/jobs'
)
})
test('Cannot retrieve the list of jobs', async () => {
when(mockedMessage.statusCode).thenReturn(404)
when(mockedMessage.statusMessage).thenReturn('foo has baz')
when(mockedResponse.message).thenReturn(instance(mockedMessage))
when(mockedHttpClient.get(anything())).thenResolve(instance(mockedResponse))
try {
await fetchJobs(instance(mockedHttpClient), 'masci/foo', '123', [])
} catch (error) {
expect(String(error)).toMatch('HTTP request failed: foo has baz')
}
})
test('Filter jobs by name', async () => {
when(mockedMessage.statusCode).thenReturn(200)
when(mockedResponse.message).thenReturn(instance(mockedMessage))
when(mockedResponse.readBody()).thenResolve(JSON.stringify(jobsJson))
when(mockedHttpClient.get(anything())).thenResolve(instance(mockedResponse))
let jobs: Job[] = await fetchJobs(
instance(mockedHttpClient),
'masci/foo',
'123',
['e2e']
)
const expected: Job[] = [
{
id: 3734144148,
name: 'e2e'
}
]
expect(jobs).toEqual(expected)
})
})
describe('Test logs retrieval', () => {
beforeEach(() => {
reset(mockedHttpClient)
reset(mockedResponse)
reset(mockedMessage)
})
afterEach(() => {})
test('Retrieve the logs', async () => {
const output = `2021-09-28T15:02:00.3780475Z ##[group]Virtual Environment
2021-09-28T15:02:00.3781127Z Environment: ubuntu-20.04
2021-09-28T15:02:00.3781638Z Version: 20210919.1`
when(mockedMessage.statusCode).thenReturn(200)
when(mockedResponse.message).thenReturn(instance(mockedMessage))
when(mockedResponse.readBody()).thenResolve(output)
when(mockedHttpClient.get(anything())).thenResolve(instance(mockedResponse))
const lines: string[] = await fetchLogs(
instance(mockedHttpClient),
'masci/foo',
{
id: 3734144061,
name: 'test'
}
)
expect(lines.length).toBe(3)
expect(lines[2]).toMatch(`2021-09-28T15:02:00.3781638Z Version: 20210919.1`)
})
test('Cannot retrieve the logs', async () => {
when(mockedMessage.statusCode).thenReturn(404)
when(mockedMessage.statusMessage).thenReturn('foo has baz')
when(mockedResponse.message).thenReturn(instance(mockedMessage))
when(mockedHttpClient.get(anything())).thenResolve(instance(mockedResponse))
try {
await fetchLogs(instance(mockedHttpClient), 'masci/foo', {
id: 3734144061,
name: 'test'
})
} catch (error) {
expect(String(error)).toMatch('HTTP request failed: foo has baz')
}
})
})