-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
266 lines (215 loc) · 8.31 KB
/
index.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @fileOverview
*
* ___ ___ _ ___ __ __ ___
* | _ \ __| /_\ | \| \/ | __|
* | / _| / _ \| |) | |\/| | _|
* |_|_\___/_/ \_\___/|_| |_|___|
*
* This is the reporter for debugging collections using Newman.
*
* @note FOR CONTRIBUTORS
*
* The easiest way to read this file is to understand a couple of things
* 1. The act of breaking the flow is orchestrated by `break-manager` and the same
* is setup at the beginning to connect to a newman run's pause/resume functions
* 2. This module needs `run` object from start event of newman. This may not be available
* on older newman versions. The run object is used everywhere as if it exists, and it's
* actual value though is set at the far end of this code in the `start` event listener
* 3. This project uses `semistandard` linting module.
*
* In short,
* (a) actions are attached to break manager using `brk.add`
* (b) upon break these actions are presented and then decided to move or not
* (c) read break manager inline documentation to understand how `brk.add` works!
*
* To add a new action should be very easy:
* 1. Add a new function and option using brk.add (note that the definition point in code sets
* the order of display, hence keep it somewhere appropriate and not just plonk it at the
* end!)
* 2. This option will be presented on break. Do something in the function you added to brk.add
* and when done, call `done`.
* 3. If you want your action to cause continue or stay in paused state, set it as boolean
* value of the 2nd parameter to the `done` function.
*/
const path = require('path');
const colors = require('colors');
const consola = require('consola');
const filesize = require('filesize');
const util = require('./util');
const runtimeUtils = require('./runtime-utils');
const bannerText = require('./banner.js');
const VarTracker = require('./vartracker');
const BreakManager = require('./break-manager');
const print = util.print;
const printlf = util.printlf;
// Standard newman reporter construction interface
module.exports = function JerryReporter (newman, reporterOptions, options) {
// Announce Jerry in CLI
// @note that we are not respecting any "silent" flag for Jerry
printlf(colors.rainbow(bannerText));
consola.info(`Loaded Newman Jerry Reporter v${require(path.join(__dirname, '/package.json')).version}`);
consola.log(` You can press ${colors.bold('CTRL+C')} any moment to break the run.\n`);
const brk = new BreakManager();
const trk = new VarTracker(); // \_(ツ)_/¯ on name!
let run, // is assigned only after `start` event
latestNetwork;
brk.add(`Break on ${colors.bold('next request')}`, function (done) {
newman.once('request', function () {
brk.break('request');
});
done(null, true);
});
brk.add(`Break on ${colors.bold('next iteration')}`, function (done) {
newman.once('beforeIteration', function () {
brk.break('iteration start');
});
done(null, true);
});
brk.add(`Break on ${colors.bold('next console log')}`, function (done) {
newman.once('console', function () {
brk.break('console log');
});
done(null, true);
});
brk.add(`Break on ${colors.bold('the end of run')}`, function (done) {
newman.on('iteration', function (arg0, args) { // arg0 is error, we have no business handling
if (args.cursor.eof) {
brk.break('end of run');
}
});
done(null, true);
});
brk.add(`Break on ${colors.bold('variable change')}`, function (done) {
const onChangeTracker = function () {
const col = trk.track('Collection');
const env = trk.track('Environment');
const glb = trk.track('Global');
if (env.modified || glb.modified || col.modified) {
newman.off('item', onChangeTracker);
VarTracker.printStatusLists(col, env, glb);
brk.break('change of variable');
}
};
newman.on('item', onChangeTracker);
done(null, true);
});
brk.add(`Break on ${colors.bold('setNextRequest')}`, function (done) {
let latestSNR;
const sniffSNR = function (err, o) {
if (err) {
return;
}
const snr = runtimeUtils.extractSNR(o.executions);
if (snr) {
latestSNR = snr;
}
};
const reactToSNRChange = function (arg0, o) { // arg0 is error, we have no business handling
if (latestSNR) {
newman.off('test', sniffSNR);
newman.off('item', reactToSNRChange);
brk.break('change of execution order');
}
};
newman.on('test', sniffSNR);
newman.on('item', reactToSNRChange);
done(null, true);
});
brk.add('Inspect all variables', function (done) {
VarTracker.printLists(VarTracker.list(run.state.collectionVariables),
VarTracker.list(run.state.environment), VarTracker.list(run.state.globals));
done(null, false);
});
brk.add('Show last network activity', function (done) {
consola.log('');
if (!latestNetwork) {
consola.info('No last recorded network activity');
return done(null, false);
}
if (latestNetwork.err) {
consola.error(latestNetwork.err);
return done(null, false);
}
const req = latestNetwork.req;
const res = latestNetwork.res;
const mime = res.contentInfo();
const SEP = colors.gray('★ ');
print(`${req.method} ${req.url}\n`);
print(`${res.code} ${res.reason()} ${SEP}`);
print(`${util.prettyms(res.responseTime)} ${colors.gray('time')} ${SEP}`);
print(`${filesize(req.size().total, { spacer: '' })}${colors.gray('↑')} ${filesize(res.size().total, { spacer: '' })}${colors.gray('↓')} ${colors.gray('size')} ${SEP}`);
print(`${req.headers.members.length}${colors.gray('↑')} ${res.headers.members.length}${colors.gray('↓')} ${colors.gray('headers')} ${SEP}`);
print(`${res.cookies.members.length} ${colors.gray('cookies')}\n\n`);
// @todo add auth 🔒 snippet
print(`${mime.fileName} ${SEP}`);
print(`${mime.contentType} ${SEP}`);
print(`${mime.mimeType} ${SEP}`);
print(`${mime.mimeFormat} ${SEP}`);
print(`${mime.charset}\n`);
print(colors.gray(res.text()) + '\n');
return done(null, false);
});
newman.on('request', function (err, args) {
latestNetwork = {
err: err,
req: args.request,
res: args.response
};
});
newman.on('done', function () {
latestNetwork = null;
});
brk.add('Abort run', function (done) {
if (run) {
run.abort();
}
done(null, true);
});
brk.add('Force abort run (press ESC)', function () {
process.exit(1);
});
newman.on('start', function (err, args) {
if (err) {
consola.warn('Unable to initialise Jerry. There was an error during run start.\n' +
colors.gray('This is unlikely because of Jerry and most likely because of a bug in some reporter or ' +
'a bug in Newman itself. Having said that, the error described below could be a clue ' +
'leading to the cause.'));
consola.error(err);
return;
}
if (!(args && args.run)) {
consola.warn('Unable to initialise Jerry. Could not integrate accurately with Newman.\n' +
colors.gray('This is likely because you are running an older version of Newman that is not capable ' +
'of exposing appropriate internal interfaces. Try re-running by upgrading Newman to the latest ' +
'version using `npm i newman@latest` command.'));
return;
}
run = args.run;
// break manager is required to be setup before use. whenever continue or break
// is triggered, we pause or resume the run.
brk.setup({
onBreak: function (done) {
run.pause(done);
},
onContinue: function (done) {
run.resume(done);
},
signal: true // mark that ctrl+c will be hijacked
});
// setup all the trackers for variables in the run (setting `true` does one initial tracking)
trk.attach('Collection', run.state.collectionVariables, true);
trk.attach('Environment', run.state.environment, true);
trk.attach('Global', run.state.globals, true);
if (!reporterOptions.continueOnStart) {
brk.break('execution start');
}
});
newman.on('done', function (err) {
if (!run) { return; } // implies newman was older than needed
if (err) {
consola.error(err);
}
consola.info('Newman execution completed.');
});
};