-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.test.js
213 lines (193 loc) · 5.32 KB
/
robot.test.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const {
SEMAPHORE_CONTEXT,
SUCCESS,
FAILURE,
// ERROR,
// STATES,
parseEventMeta,
isSemaphoreEvent,
getPullRequestNumber,
getSemaphoreBranchNumber,
getSemaphoreProblemCommands,
formatSemaphoreLog,
getSemaphoreBuildLog,
getCommentBody,
onStatusHook,
createRobot
} = require('./robot')
const request = require('request-promise-native')
jest.mock('request-promise-native')
jest.mock('./hidden', () => ({
projectHash: 'b217cd26b913456fb3836a8e9e87f0ef',
authToken: '093c28a53b18408aa7da66c52e460ae7'
}))
const testContext = {
repo: () => ({ owner: 'HKRGH', repo: 'test-repo' }),
payload: {
target_url: 'https://example.com/build/25',
context: SEMAPHORE_CONTEXT,
state: SUCCESS,
commit: {
sha: '57afc8ccede244a88a219d6ad2ca1110',
html_url: 'https://example.com/57afc8ccede244a88a219d6ad2ca1110'
},
sender: {
login: 'HKRGH'
},
branches: [{
name: 'test-branch'
}]
},
github: {
pullRequests: {
async getAll () {
return { data: [{ number: '16' }] }
}
},
issues: {
createComment: jest.fn()
}
},
log: jest.fn()
}
const mockBranchNumberResult = [{
name: 'test-branch',
id: '25'
}]
const mockCommandResult = {
threads: [{
commands: [{
name: 'npm install',
result: '0'
}]
}, {
commands: [{
name: 'npm test',
result: '256'
}, {
name: 'npm lint',
result: '32'
}]
}]
}
describe('Semaphore CI Reporter', () => {
describe('#parseEventMeta', () => {
test('format data from the context that we need', () => {
expect(parseEventMeta(testContext)).toMatchSnapshot()
})
})
describe('#isSemaphoreEvent', () => {
test('true if semaphore and done', () => {
expect(isSemaphoreEvent({
eventContext: SEMAPHORE_CONTEXT,
state: SUCCESS
})).toBe(true)
})
test('false if semaphore and not done', () => {
expect(isSemaphoreEvent({
eventContext: SEMAPHORE_CONTEXT,
state: 'pending'
})).toBe(false)
})
test('false if not semaphore', () => {
expect(isSemaphoreEvent({
eventContext: 'travisci',
state: SUCCESS
})).toBe(false)
})
})
describe('#getPullRequestNumber', () => {
test('get the pull request number', async () => {
const meta = parseEventMeta(testContext)
const number = await getPullRequestNumber(meta)
expect(number).toEqual('16')
})
})
describe('#getSemaphoreBranchNumber', () => {
test('get the branch number', async () => {
const branchName = 'test-branch'
request.mockReturnValueOnce(mockBranchNumberResult)
const branchNumber = await getSemaphoreBranchNumber(branchName)
expect(branchNumber).toBe('25')
})
})
describe('#getSemaphoreProblemCommands', () => {
test('get the problem commands', async () => {
const branchNumber = '25'
const buildNumber = '16'
request.mockReturnValueOnce(mockCommandResult)
const commands = await getSemaphoreProblemCommands(branchNumber, buildNumber)
expect(commands).toMatchSnapshot()
})
})
describe('#formatSemaphoreLog', () => {
test('format the failed commands', () => {
const commands = [{
name: 'npm test',
result: '256',
output: '> Your test failed.'
}]
expect(formatSemaphoreLog(commands)).toMatchSnapshot()
})
test('give empty string if no failed commands', () => {
expect(formatSemaphoreLog([])).toEqual('')
})
})
describe('#getSemaphoreBuildLog', () => {
test('it should get the build log', async () => {
const branchName = 'test-branch'
const buildNumber = '16'
request
.mockReturnValueOnce(mockBranchNumberResult)
.mockReturnValueOnce(mockCommandResult)
const log = await getSemaphoreBuildLog(branchName, buildNumber)
expect(log).toMatchSnapshot()
})
})
describe('#getCommentBody', () => {
test('get a success comment', async () => {
const meta = parseEventMeta(testContext)
const body = await getCommentBody(meta)
expect(body).toMatchSnapshot()
})
test('get a failed comment', async () => {
let meta = parseEventMeta(testContext)
meta.state = FAILURE
request
.mockReturnValueOnce(mockBranchNumberResult)
.mockReturnValueOnce(mockCommandResult)
const body = await getCommentBody(meta)
expect(body).toMatchSnapshot()
})
})
describe('#onStatusHook', () => {
test('do nothing with events that arent Semaphore', async () => {
const context = {
...testContext,
payload: {
...testContext.payload,
context: 'travisci'
}
}
await onStatusHook(context)
expect(context.log).not.toBeCalled()
expect(context.github.issues.createComment).not.toBeCalled()
})
test('create a comment on status update', async () => {
await onStatusHook(testContext)
expect(testContext.log).toMatchSnapshot()
expect(testContext.github.issues.createComment).toMatchSnapshot()
})
})
describe('#createRobot', () => {
test('create a status listening robot', () => {
const robot = {
log: jest.fn(),
on: jest.fn()
}
createRobot(robot)
expect(robot.log).toMatchSnapshot()
expect(robot.on).toMatchSnapshot()
})
})
})