forked from ScaCap/action-surefire-report
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action.test.js
172 lines (145 loc) · 5.49 KB
/
action.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
const core = require('@actions/core');
const github = require('@actions/github');
const nock = require('nock');
const action = require('./action');
const {
finishedWithFailures,
finishedSuccess,
nothingFound,
masterSuccess,
nothingFoundSuccess,
masterSuccessWithSkipped
} = require('./action.test.fixtures');
jest.setTimeout(200000);
let inputs = {};
let sortAnnotations = request => {
request.output.annotations = request.output.annotations.sort((a, b) => a.message.localeCompare(b.message));
return request;
};
describe('action should work', () => {
beforeAll(() => {
// https://github.com/actions/checkout/blob/v2.1.0/__test__/input-helper.test.ts
jest.spyOn(core, 'getInput').mockImplementation(name => {
return inputs[name];
});
jest.spyOn(core, 'error').mockImplementation(jest.fn());
jest.spyOn(core, 'warning').mockImplementation(jest.fn());
jest.spyOn(core, 'info').mockImplementation(jest.fn());
jest.spyOn(core, 'debug').mockImplementation(jest.fn());
github.context.payload.pull_request = {
html_url: 'https://github.com/starburstdata/action-testng-report',
head: { sha: 'sha123' }
};
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
return {
owner: 'starburstdata',
repo: 'action-testng-report'
};
});
});
beforeEach(() => {
// Reset inputs
inputs = {
report_paths: '**/surefire-reports/testng-results.xml',
github_token: 'GITHUB_TOKEN',
check_name: 'Test Report',
fail_if_empty: "true",
show_skipped: "false",
update_existing_check: "false",
skip_publishing: 'false'
};
});
afterAll(() => {
jest.restoreAllMocks();
});
it('should parse testng reports and send a check run to GitHub', async () => {
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(sortAnnotations(request)).toMatchObject(sortAnnotations(finishedWithFailures));
});
it('should send all ok if no tests were broken', async () => {
inputs.report_paths = '**/ok/target/surefire-reports/testng-results.xml';
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(request).toMatchObject(finishedSuccess);
});
it('should send failure if no test results were found', async () => {
inputs.report_paths = '**/xxx/*.xml';
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(request).toMatchObject(nothingFound);
});
it('should send success if no test results were found and fail_if_empty is false', async () => {
inputs.report_paths = '**/xxx/*.xml';
inputs.fail_if_empty = "false";
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(request).toMatchObject(nothingFoundSuccess);
});
it('should send reports to sha if no pr detected', async () => {
inputs.report_paths = '**/ok/target/surefire-reports/testng-results.xml';
github.context.payload.pull_request = undefined;
github.context.sha = 'masterSha123';
github.context.ref = 'refs/heads/master';
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(request).toMatchObject(masterSuccess);
});
it('should not send report on skip_publishing', async () => {
inputs.skip_publishing = 'true';
// nock error if the request is sent
await action();
});
it('should send reports with skipped', async () => {
inputs.report_paths = '**/ok/target/surefire-reports/testng-results.xml';
inputs.show_skipped = "true";
github.context.payload.pull_request = undefined;
github.context.sha = 'masterSha123';
github.context.ref = 'refs/heads/master';
let request = null;
const scope = nock('https://api.github.com')
.post('/repos/starburstdata/action-testng-report/check-runs', body => {
request = body;
return body;
})
.reply(200, {});
await action();
scope.done();
expect(request).toMatchObject(masterSuccessWithSkipped);
});
});