/
job_status.test.ts
125 lines (115 loc) · 3.92 KB
/
job_status.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
import * as github from '@actions/github'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import {send, ConfigOptions} from '../src/slack'
import {readFileSync} from 'fs'
const url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
const jobName = 'CI Tests'
const jobStatus = 'failure'
const jobSteps = {
'install-deps': {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
hooks: {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
lint: {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
types: {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
'unit-test': {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
'integration-test': {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
}
}
const channel = '#github-ci'
const message = undefined
// mock github context
const dump = JSON.parse(readFileSync('./__tests__/fixtures/push.json', 'utf-8'))
github.context.payload = dump.event
github.context.eventName = dump.event_name
github.context.sha = dump.sha
github.context.ref = dump.ref
github.context.workflow = dump.workflow
github.context.action = dump.action
github.context.actor = dump.actor
process.env.CI = 'true'
process.env.GITHUB_WORKFLOW = 'build-test'
process.env.GITHUB_RUN_ID = '100143423'
process.env.GITHUB_RUN_NUMBER = '8'
process.env.GITHUB_ACTION = 'self2'
process.env.GITHUB_ACTIONS = 'true'
process.env.GITHUB_ACTOR = 'satterly'
process.env.GITHUB_REPOSITORY = 'act10ns/slack'
process.env.GITHUB_EVENT_NAME = 'push'
process.env.GITHUB_EVENT_PATH = '/home/runner/work/_temp/_github_workflow/event.json'
process.env.GITHUB_WORKSPACE = '/home/runner/work/slack/slack'
process.env.GITHUB_SHA = '68d48876e0794fba714cb331a1624af6b20942d8'
process.env.GITHUB_REF = 'refs/heads/master'
process.env.GITHUB_HEAD_REF = ''
process.env.GITHUB_BASE_REF = ''
process.env.GITHUB_SERVER_URL = 'https://github.com'
process.env.GITHUB_API_URL = 'https://github.com'
process.env.GITHUB_GRAPHQL_URL = 'https://api.github.com/graphql'
test('push event to slack', async () => {
const mockAxios = new MockAdapter(axios, {delayResponse: 200})
mockAxios
.onPost()
.reply(config => {
console.log(config.data)
return [200, {status: 'ok'}]
})
.onAny()
.reply(500)
const config: ConfigOptions = {}
const res = await send(url, jobName, jobStatus, jobSteps, channel, message, config)
await expect(res).toStrictEqual({text: {status: 'ok'}})
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
username: 'GitHub Action',
icon_url: 'https://octodex.github.com/images/original.png',
channel: '#github-ci',
attachments: [
{
fallback: '[GitHub]: [act10ns/slack] build-test push failure',
color: 'danger',
author_name: 'satterly',
author_link: 'https://github.com/satterly',
author_icon: 'https://avatars0.githubusercontent.com/u/615057?v=4',
mrkdwn_in: ['pretext', 'text', 'fields'],
pretext: '',
text:
'*<https://github.com/act10ns/slack/actions?query=build-test|Workflow _build-test_ job _CI Tests_ triggered by _push_ is _failure_>* for <https://github.com/act10ns/slack/commits/master|`master`>\n<https://github.com/act10ns/slack/compare/db9fe60430a6...68d48876e079|`68d48876`> - 4 commits',
title: '',
fields: [
{
short: false,
title: 'Job Steps',
value:
':no_entry_sign: install-deps\n:no_entry_sign: hooks\n:no_entry_sign: lint\n:no_entry_sign: types\n:no_entry_sign: unit-test\n:no_entry_sign: integration-test\n'
}
],
footer: '<https://github.com/act10ns/slack|act10ns/slack> #8',
footer_icon: 'https://github.githubassets.com/favicon.ico',
ts: expect.stringMatching(/[0-9]+/)
}
]
})
mockAxios.resetHistory()
mockAxios.reset()
})