generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.test.js
247 lines (197 loc) · 8.24 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const process = require('process');
const cp = require('child_process');
const path = require('path');
const tools = require('./src/tools');
const TIMEOUT = 10000;
const cleanEnvs = function () {
delete process.env['INPUT_MODE'];
delete process.env['INPUT_OUTPUT'];
delete process.env['INPUT_SCRIPTBEFORE'];
}
const extractOutput = function (result) {
console.info('--------------------------------');
console.info('result: ');
console.info(result);
console.info('--------------------------------');
// TODO XXX: this is a workaround for the test to be able to run both locally and on GitHub runner
// Because the action output is not visible in the console when run in a workflow (GitHub runner)
// const outputEntries = result.split(/\r?\n/).filter(line => line.startsWith('::set-output'));
const outputEntries = result.split(/\r?\n/).filter(line => line.startsWith('--action-result::'));
console.info('outputEntries: ')
console.info(outputEntries);
// there should be only one line with output
expect(outputEntries.length).toBe(1);
const output = outputEntries[0];
const name = output.substring(output.indexOf('name=') + 5, output.indexOf('::', 15));
const value = output.substring(output.indexOf('::', 15) + 2);
return {name: name, value: value}
}
test('default mode', async () => {
console.log("default mode");
cleanEnvs();
const mode = await tools.getMode();
console.log("mode: " + mode);
await expect(mode).toBe('wholePage');
});
test('non-default mode', async () => {
console.log('non-default mode');
cleanEnvs();
process.env['INPUT_MODE'] = 'page';
const mode = await tools.getMode();
console.log("mode: " + mode);
await expect(mode).not.toBe('wholePage');
});
test('malformed URL input', async () => {
console.log('malformed URL input');
cleanEnvs();
process.env['INPUT_URL'] = 'malformed';
const parameters = await tools.getParameters()
await expect(tools.validateParameters(parameters)).rejects.toThrow('Please, provide a valid URL.');
});
test('Fail without URL', async () => {
console.log("Fail without URL");
cleanEnvs();
const ip = path.join(__dirname, 'index.js');
try {
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
throw new Error('Should have failed');
} catch (error) {
console.info("Expected fail.");
}
});
test('test parameter for element', () => {
console.log('test parameter for element');
cleanEnvs();
process.env['INPUT_URL'] = 'https://google.com';
process.env['INPUT_MODE'] = 'element';
const ip = path.join(__dirname, 'index.js');
try {
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
} catch (error) {
console.info("Expected fail.");
console.log("Error: " + error.message);
}
});
test('test given output name', async () => {
console.log('test given output name');
cleanEnvs();
const url = 'https://github.com/karol-brejna-i/webpage-screenshot-action/blob/main/README.md';
const screenshot = 'readme-wholepage-screenshot.png';
process.env['INPUT_URL'] = url;
process.env['INPUT_OUTPUT'] = screenshot;
const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
const output = extractOutput(result);
console.log("output: " + output);
const expectedJson = {url: url, screenshot: screenshot};
await expect(output.value).toEqual(expect.stringContaining(JSON.stringify(expectedJson)));
});
test('test run without scriptBefore', async () => {
console.log('test run without scriptBefore');
cleanEnvs();
const url = 'https://google.com';
const screenshot = 'screenshot.png';
process.env['INPUT_URL'] = url;
const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
const output = extractOutput(result);
console.log("output: " + output);
const expectedJson = {url: url, screenshot: screenshot};
await expect(output.value).toEqual(expect.stringContaining(JSON.stringify(expectedJson)));
});
test('test run with scriptBefore', async () => {
console.log('test run with scriptBefore');
cleanEnvs();
const url = 'https://google.com';
const screenshot = 'screenshot.png';
process.env['INPUT_URL'] = url;
process.env['INPUT_SCRIPTBEFORE'] = 'result = 42;';
const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
const expectedJson = {url: url, screenshot: screenshot, scriptResult: 42};
const output = extractOutput(result);
console.log("output: " + JSON.stringify(output));
await expect(output.value).toEqual(expect.stringContaining(JSON.stringify(expectedJson)));
});
test('test run with faulty scriptBefore', async () => {
console.log('test run with faulty scriptBefore');
cleanEnvs();
process.env['INPUT_URL'] = 'https://google.com';
process.env['INPUT_SCRIPTBEFORE'] = 'return 42;';
delete process.env['INPUT_MODE']
const ip = path.join(__dirname, 'index.js');
const options = {
timeout: TIMEOUT,
killSignal: 'SIGKILL',
env: process.env
}
cp.exec(`node ${ip}`, options, function (err, stout, stderr) {
if (err) {
console.log('Child process exited with error code', err);
console.log('stderr: ' + stderr);
console.log('stdout: ' + stout);
if (err.killed) {
throw new Error('Command failed. Probably timeouted.');
}
} else {
console.debug('Child process exited with success code!');
const output = extractOutput(stout);
expect(output.value).toEqual(expect.stringContaining('SyntaxError: Illegal return statement'));
console.info("Expected fail.");
}
});
});
test('test run with faulty url', async () => {
console.log('test run with faulty url');
cleanEnvs();
// the URL is technically valid, but it's not reachable
process.env['INPUT_URL'] = 'https://ąśćżź.pl';
const ip = path.join(__dirname, 'index.js');
const options = {
timeout: TIMEOUT,
killSignal: 'SIGKILL',
env: process.env
}
cp.exec(`node ${ip}`, options, function (err, stout, stderr) {
if (err) {
console.log('Child process exited with error code', err);
console.log('stderr: ' + stderr);
console.log('stdout: ' + stout);
if (err.killed) {
throw new Error('Command failed. Probably timeouted.');
}
} else {
console.debug('Child process exited with success code!');
const output = extractOutput(stout);
expect(output.value).toEqual(expect.stringContaining('Error navigating to'));
}
});
console.log('waiting for timeout');
await new Promise(resolve => setTimeout(resolve, TIMEOUT + 500));
}, TIMEOUT + 1000);
test('test multipleUrls', async () => {
console.log('test run with multiple Urls');
cleanEnvs();
const urls = ['https://google.com', 'https://github.com/karol-brejna-i/webpage-screenshot-action/blob/main/README.md'];
// create a string with newlines from urls
process.env['INPUT_URL'] = urls.join('\n');
const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, {env: process.env}).toString();
console.log(result);
const output = extractOutput(result);
console.log("output: " + JSON.stringify(output));
// convert output.value to JSON
const outputJson = JSON.parse(output.value);
console.log("outputJson: " + JSON.stringify(outputJson));
// expect that output.value doesn't contain "Error"
await expect(output.value).not.toEqual(expect.stringContaining('Error'));
await expect(output.value).toEqual(expect.stringContaining('screenshot_1.png'));
await expect(output.value).toEqual(expect.stringContaining('screenshot_2.png'));
// expect that outputJson is an array of two elements
await expect(outputJson).toHaveLength(2);
});