@elastic/synthetics 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dist/bundles/lib/index.js +432 -0
  2. package/dist/cli.js +3 -3
  3. package/dist/cli.js.map +1 -1
  4. package/dist/common_types.d.ts +12 -2
  5. package/dist/common_types.d.ts.map +1 -1
  6. package/dist/config.d.ts +1 -1
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +7 -1
  9. package/dist/config.js.map +1 -1
  10. package/dist/core/browser-service.js +2 -2
  11. package/dist/core/browser-service.js.map +1 -1
  12. package/dist/core/expect.d.ts +1 -152
  13. package/dist/core/expect.d.ts.map +1 -1
  14. package/dist/core/expect.js +14 -5
  15. package/dist/core/expect.js.map +1 -1
  16. package/dist/core/gatherer.d.ts +1 -1
  17. package/dist/core/gatherer.d.ts.map +1 -1
  18. package/dist/core/gatherer.js +5 -5
  19. package/dist/core/gatherer.js.map +1 -1
  20. package/dist/core/runner.d.ts.map +1 -1
  21. package/dist/core/runner.js +8 -7
  22. package/dist/core/runner.js.map +1 -1
  23. package/dist/dsl/journey.d.ts +1 -1
  24. package/dist/dsl/journey.d.ts.map +1 -1
  25. package/dist/dsl/monitor.d.ts +4 -0
  26. package/dist/dsl/monitor.d.ts.map +1 -1
  27. package/dist/dsl/monitor.js.map +1 -1
  28. package/dist/helpers.d.ts +0 -5
  29. package/dist/helpers.d.ts.map +1 -1
  30. package/dist/helpers.js +1 -24
  31. package/dist/helpers.js.map +1 -1
  32. package/dist/index.d.ts +1 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/options.d.ts +2 -1
  35. package/dist/options.d.ts.map +1 -1
  36. package/dist/options.js +13 -4
  37. package/dist/options.js.map +1 -1
  38. package/dist/plugins/browser-console.d.ts +3 -2
  39. package/dist/plugins/browser-console.d.ts.map +1 -1
  40. package/dist/plugins/browser-console.js +33 -7
  41. package/dist/plugins/browser-console.js.map +1 -1
  42. package/dist/plugins/network.d.ts +1 -0
  43. package/dist/plugins/network.d.ts.map +1 -1
  44. package/dist/plugins/network.js +15 -2
  45. package/dist/plugins/network.js.map +1 -1
  46. package/dist/push/kibana_api.d.ts.map +1 -1
  47. package/dist/push/kibana_api.js +1 -1
  48. package/dist/push/kibana_api.js.map +1 -1
  49. package/dist/push/monitor.d.ts.map +1 -1
  50. package/dist/push/monitor.js +10 -2
  51. package/dist/push/monitor.js.map +1 -1
  52. package/dist/push/request.d.ts.map +1 -1
  53. package/dist/push/request.js +4 -3
  54. package/dist/push/request.js.map +1 -1
  55. package/dist/push/utils.js +1 -1
  56. package/dist/push/utils.js.map +1 -1
  57. package/dist/reporters/base.d.ts +3 -0
  58. package/dist/reporters/base.d.ts.map +1 -1
  59. package/dist/reporters/base.js +20 -22
  60. package/dist/reporters/base.js.map +1 -1
  61. package/dist/reporters/index.d.ts +1 -0
  62. package/dist/reporters/index.d.ts.map +1 -1
  63. package/dist/reporters/index.js.map +1 -1
  64. package/dist/reporters/json.d.ts.map +1 -1
  65. package/dist/reporters/json.js +34 -1
  66. package/dist/reporters/json.js.map +1 -1
  67. package/dist/reporters/junit.d.ts.map +1 -1
  68. package/dist/reporters/junit.js +4 -4
  69. package/dist/reporters/junit.js.map +1 -1
  70. package/dist/reporters/reporter-util.d.ts +35 -0
  71. package/dist/reporters/reporter-util.d.ts.map +1 -0
  72. package/dist/reporters/reporter-util.js +192 -0
  73. package/dist/reporters/reporter-util.js.map +1 -0
  74. package/package.json +16 -8
  75. package/src/cli.ts +3 -3
  76. package/src/common_types.ts +15 -2
  77. package/src/config.ts +12 -1
  78. package/src/core/browser-service.ts +1 -1
  79. package/src/core/expect.ts +13 -191
  80. package/src/core/gatherer.ts +1 -1
  81. package/src/core/runner.ts +15 -9
  82. package/src/dsl/journey.ts +1 -1
  83. package/src/dsl/monitor.ts +4 -0
  84. package/src/helpers.ts +0 -24
  85. package/src/index.ts +1 -1
  86. package/src/options.ts +16 -4
  87. package/src/plugins/browser-console.ts +39 -8
  88. package/src/plugins/network.ts +16 -3
  89. package/src/push/kibana_api.ts +4 -3
  90. package/src/push/monitor.ts +13 -2
  91. package/src/push/request.ts +12 -4
  92. package/src/push/utils.ts +1 -1
  93. package/src/reporters/base.ts +26 -33
  94. package/src/reporters/index.ts +5 -1
  95. package/src/reporters/json.ts +39 -3
  96. package/src/reporters/junit.ts +5 -5
  97. package/src/reporters/reporter-util.ts +217 -0
@@ -0,0 +1,217 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { fileURLToPath } from 'url';
27
+ import { resolve, sep } from 'path';
28
+ import { readFileSync } from 'fs';
29
+ import { inspect } from 'util';
30
+ import { gray } from 'kleur/colors';
31
+ import { codeFrameColumns } from '@babel/code-frame';
32
+ import StackUtils from 'stack-utils';
33
+ import { Location, StackFrame, TestError } from '../common_types';
34
+
35
+ const stackUtils = new StackUtils({ internals: StackUtils.nodeInternals() });
36
+
37
+ const SYNTHETICS_PATH = resolve(__dirname, '..', '..');
38
+ const PW_CORE_PATH = require.resolve('playwright-core');
39
+
40
+ function prepareStackFrame(line: string): StackFrame {
41
+ const frame = stackUtils.parseLine(line);
42
+ if (!frame) {
43
+ return;
44
+ }
45
+ // ignore node internals
46
+ if (frame.file?.startsWith('internal') || frame.file?.startsWith('node:')) {
47
+ return;
48
+ }
49
+
50
+ // handle relative URLs
51
+ let file = frame.file?.startsWith('file://')
52
+ ? fileURLToPath(frame.file)
53
+ : resolve(process.cwd(), frame.file);
54
+
55
+ // ignore node_modules
56
+ if (file.includes(`${sep}node_modules${sep}`)) {
57
+ return;
58
+ }
59
+
60
+ // filter library and PW files
61
+ if (!filterLibInternals(file)) {
62
+ return;
63
+ }
64
+
65
+ // When we bundle the journeys, we store the absolute path of the journey
66
+ // files and write the sourcemap relative to these files. When we try to
67
+ // extract the sourcemap file location, the stacktrace will be relatively
68
+ // resolved to these files which would be under `journeys` folder. So we strip
69
+ // these extra `journeys` from the path to get the correct file location.
70
+ if (frame.file?.includes('journeys/journeys')) {
71
+ file = file.replace('journeys/journeys', 'journeys');
72
+ }
73
+
74
+ return {
75
+ file,
76
+ line: frame.line || 0,
77
+ column: frame.column || 0,
78
+ function: frame.function,
79
+ };
80
+ }
81
+
82
+ function filterLibInternals(file: string) {
83
+ // To ignore filtering the stack trace on tests
84
+ if (process.env.TEST_OVERRIDE) {
85
+ return true;
86
+ }
87
+ if (file.startsWith(SYNTHETICS_PATH)) {
88
+ return false;
89
+ }
90
+ // should have been filtered by node_modules, but just in case
91
+ if (file.startsWith(PW_CORE_PATH)) {
92
+ return false;
93
+ }
94
+ }
95
+
96
+ function constructStackFromFrames(frames: StackFrame[]) {
97
+ const stackLines: string[] = [];
98
+ for (const frame of frames) {
99
+ stackLines.push(
100
+ ` at ${frame.function ? frame.function + ' ' : ''}(${frame.file}:${
101
+ frame.line
102
+ }:${frame.column})`
103
+ );
104
+ }
105
+ return stackLines;
106
+ }
107
+
108
+ export function prepareError(error: Error) {
109
+ const stack = error.stack;
110
+ const lines = stack.split('\n');
111
+ let startAt = lines.findIndex(line => line.startsWith(' at '));
112
+ if (startAt === -1) {
113
+ startAt = lines.length;
114
+ }
115
+ const message = lines.slice(0, startAt).join('\n');
116
+ const stackLines = lines.slice(startAt);
117
+ // figure out the location of the journey/step that throws the error and
118
+ // correct stack line corresponding to that error
119
+ const stackFrames: StackFrame[] = [];
120
+ for (const line of stackLines) {
121
+ const frame = prepareStackFrame(line);
122
+ if (!frame || !frame.file) {
123
+ continue;
124
+ }
125
+ stackFrames.push(frame);
126
+ }
127
+
128
+ let location: Location | undefined;
129
+ if (stackFrames.length) {
130
+ const frame = stackFrames[0];
131
+ location = {
132
+ file: frame.file,
133
+ column: frame.column || 0,
134
+ line: frame.line || 0,
135
+ };
136
+ }
137
+
138
+ return {
139
+ message,
140
+ stack: constructStackFromFrames(stackFrames).join('\n'),
141
+ location,
142
+ };
143
+ }
144
+
145
+ export function highLightSource(location: Location): string {
146
+ if (!location) {
147
+ return;
148
+ }
149
+
150
+ try {
151
+ const source = readFileSync(location.file, 'utf-8');
152
+ const code = codeFrameColumns(
153
+ source,
154
+ { start: location },
155
+ { highlightCode: true }
156
+ );
157
+ return code;
158
+ } catch (_) {
159
+ // ignore error
160
+ }
161
+ }
162
+
163
+ // serializeError prefers receiving proper Errors, but since at runtime
164
+ // non Error exceptions can be thrown, it tolerates though. The
165
+ // redundant type Error | any expresses that.
166
+ export function serializeError(
167
+ error: Error | any,
168
+ highlightCode = true
169
+ ): TestError {
170
+ if (!(error instanceof Error) || !error.stack) {
171
+ return { message: `thrown: ${inspect(error)}` };
172
+ }
173
+
174
+ const testErr: TestError = { message: error.message };
175
+ const { message, stack, location } = prepareError(error);
176
+ testErr.message = message;
177
+ if (stack) {
178
+ testErr.stack = stack;
179
+ }
180
+ if (location && highlightCode) {
181
+ testErr.location = location;
182
+ testErr.source = highLightSource(testErr.location);
183
+ }
184
+ return testErr;
185
+ }
186
+
187
+ export function renderError(error: TestError) {
188
+ const summary = [];
189
+ // push empty string to add a new line before rendering error
190
+ summary.push('');
191
+ summary.push(error.message);
192
+
193
+ if (error.source) {
194
+ summary.push('');
195
+ summary.push(error.source);
196
+ }
197
+
198
+ if (error.stack) {
199
+ summary.push('');
200
+ summary.push(gray(error.stack));
201
+ }
202
+ summary.join('');
203
+
204
+ return summary.join('\n');
205
+ }
206
+
207
+ // strip color codes and ansi escape codes
208
+ const ansiRegex = new RegExp(
209
+ [
210
+ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
211
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
212
+ ].join('|'),
213
+ 'g'
214
+ );
215
+ export function stripAnsiCodes(msg = '') {
216
+ return msg.replace(ansiRegex, '');
217
+ }