@elastic/synthetics 1.0.0-beta.25 → 1.0.0-beta.26

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 (116) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +42 -5
  3. package/dist/cli.js.map +1 -1
  4. package/dist/common_types.d.ts +59 -5
  5. package/dist/common_types.d.ts.map +1 -1
  6. package/dist/core/gatherer.d.ts +2 -2
  7. package/dist/core/gatherer.d.ts.map +1 -1
  8. package/dist/core/gatherer.js.map +1 -1
  9. package/dist/core/index.d.ts +4 -0
  10. package/dist/core/index.d.ts.map +1 -1
  11. package/dist/core/index.js +15 -1
  12. package/dist/core/index.js.map +1 -1
  13. package/dist/core/runner.d.ts +10 -58
  14. package/dist/core/runner.d.ts.map +1 -1
  15. package/dist/core/runner.js +88 -49
  16. package/dist/core/runner.js.map +1 -1
  17. package/dist/dsl/journey.d.ts +3 -0
  18. package/dist/dsl/journey.d.ts.map +1 -1
  19. package/dist/dsl/journey.js +15 -0
  20. package/dist/dsl/journey.js.map +1 -1
  21. package/dist/dsl/monitor.d.ts +64 -0
  22. package/dist/dsl/monitor.d.ts.map +1 -0
  23. package/dist/dsl/monitor.js +62 -0
  24. package/dist/dsl/monitor.js.map +1 -0
  25. package/dist/generator/index.d.ts +13 -0
  26. package/dist/generator/index.d.ts.map +1 -0
  27. package/dist/generator/index.js +123 -0
  28. package/dist/generator/index.js.map +1 -0
  29. package/dist/{reporters/reporter.js → generator/utils.d.ts} +3 -3
  30. package/dist/generator/utils.d.ts.map +1 -0
  31. package/dist/generator/utils.js +44 -0
  32. package/dist/generator/utils.js.map +1 -0
  33. package/dist/helpers.d.ts +12 -6
  34. package/dist/helpers.d.ts.map +1 -1
  35. package/dist/helpers.js +33 -43
  36. package/dist/helpers.js.map +1 -1
  37. package/dist/index.d.ts +2 -7
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +4 -3
  40. package/dist/index.js.map +1 -1
  41. package/dist/loader.js +4 -4
  42. package/dist/loader.js.map +1 -1
  43. package/dist/options.d.ts +12 -0
  44. package/dist/options.d.ts.map +1 -1
  45. package/dist/options.js +66 -4
  46. package/dist/options.js.map +1 -1
  47. package/dist/plugins/performance.d.ts +1 -16
  48. package/dist/plugins/performance.d.ts.map +1 -1
  49. package/dist/plugins/performance.js.map +1 -1
  50. package/dist/plugins/plugin-manager.d.ts +1 -1
  51. package/dist/push/bundler.d.ts +34 -0
  52. package/dist/push/bundler.d.ts.map +1 -0
  53. package/dist/push/bundler.js +119 -0
  54. package/dist/push/bundler.js.map +1 -0
  55. package/{src/reporters/reporter.ts → dist/push/index.d.ts} +4 -11
  56. package/dist/push/index.d.ts.map +1 -0
  57. package/dist/push/index.js +58 -0
  58. package/dist/push/index.js.map +1 -0
  59. package/dist/{reporters/reporter.d.ts → push/monitor.d.ts} +7 -8
  60. package/dist/push/monitor.d.ts.map +1 -0
  61. package/dist/push/monitor.js +67 -0
  62. package/dist/push/monitor.js.map +1 -0
  63. package/dist/push/plugin.d.ts +33 -0
  64. package/dist/push/plugin.d.ts.map +1 -0
  65. package/dist/push/plugin.js +123 -0
  66. package/dist/push/plugin.js.map +1 -0
  67. package/dist/push/request.d.ts +43 -0
  68. package/dist/push/request.d.ts.map +1 -0
  69. package/dist/push/request.js +95 -0
  70. package/dist/push/request.js.map +1 -0
  71. package/dist/reporters/base.d.ts +14 -6
  72. package/dist/reporters/base.d.ts.map +1 -1
  73. package/dist/reporters/base.js +45 -47
  74. package/dist/reporters/base.js.map +1 -1
  75. package/dist/reporters/index.d.ts +18 -8
  76. package/dist/reporters/index.d.ts.map +1 -1
  77. package/dist/reporters/index.js.map +1 -1
  78. package/dist/reporters/json.d.ts +7 -3
  79. package/dist/reporters/json.d.ts.map +1 -1
  80. package/dist/reporters/json.js +118 -118
  81. package/dist/reporters/json.js.map +1 -1
  82. package/dist/reporters/junit.d.ts +7 -1
  83. package/dist/reporters/junit.d.ts.map +1 -1
  84. package/dist/reporters/junit.js +91 -86
  85. package/dist/reporters/junit.js.map +1 -1
  86. package/dist/tsconfig.tsbuildinfo +1 -1
  87. package/package.json +12 -8
  88. package/src/cli.ts +65 -9
  89. package/src/common_types.ts +69 -5
  90. package/src/core/gatherer.ts +2 -2
  91. package/src/core/index.ts +15 -0
  92. package/src/core/runner.ts +86 -113
  93. package/src/dsl/journey.ts +18 -0
  94. package/src/dsl/monitor.ts +83 -0
  95. package/src/generator/index.ts +159 -0
  96. package/src/generator/utils.ts +41 -0
  97. package/src/helpers.ts +36 -52
  98. package/src/index.ts +9 -1
  99. package/src/loader.ts +4 -4
  100. package/src/options.ts +71 -8
  101. package/src/plugins/performance.ts +1 -17
  102. package/src/push/bundler.ts +99 -0
  103. package/src/push/index.ts +63 -0
  104. package/src/push/monitor.ts +77 -0
  105. package/src/push/plugin.ts +113 -0
  106. package/src/push/request.ts +123 -0
  107. package/src/reporters/base.ts +57 -56
  108. package/src/reporters/index.ts +26 -3
  109. package/src/reporters/json.ts +153 -145
  110. package/src/reporters/junit.ts +98 -93
  111. package/templates/journeys/example.journey.ts +18 -0
  112. package/templates/synthetics.config.ts +19 -0
  113. package/CHANGELOG.md +0 -348
  114. package/NOTICE.txt +0 -221
  115. package/dist/reporters/reporter.d.ts.map +0 -1
  116. package/dist/reporters/reporter.js.map +0 -1
@@ -0,0 +1,63 @@
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 {
27
+ createMonitors,
28
+ ok,
29
+ formatAPIError,
30
+ formatFailedMonitors,
31
+ formatStaleMonitors,
32
+ formatNotFoundError,
33
+ } from './request';
34
+ import { Monitor } from '../dsl/monitor';
35
+ import { PushOptions } from '../common_types';
36
+ import { buildMonitorSchema } from './monitor';
37
+ import { progress, error, done, write } from '../helpers';
38
+
39
+ export async function push(monitors: Monitor[], options: PushOptions) {
40
+ progress(`preparing all monitors`);
41
+ const schemas = await buildMonitorSchema(monitors);
42
+ try {
43
+ progress(`creating all monitors`);
44
+ const { body, statusCode } = await createMonitors(schemas, options);
45
+ if (statusCode === 404) {
46
+ throw formatNotFoundError(await body.text());
47
+ }
48
+ if (!ok(statusCode)) {
49
+ const { error, message } = await body.json();
50
+ throw formatAPIError(statusCode, error, message);
51
+ }
52
+ const { staleMonitors, failedMonitors } = await body.json();
53
+ if (failedMonitors.length > 0) {
54
+ throw formatFailedMonitors(failedMonitors);
55
+ }
56
+ if (staleMonitors.length > 0) {
57
+ write(formatStaleMonitors());
58
+ }
59
+ done('Pushed');
60
+ } catch (e) {
61
+ error(e);
62
+ }
63
+ }
@@ -0,0 +1,77 @@
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 { mkdir } from 'fs/promises';
27
+ import { join } from 'path';
28
+ import { Bundler } from './bundler';
29
+ import { CACHE_PATH } from '../helpers';
30
+ import {
31
+ Monitor,
32
+ MonitorConfig,
33
+ SyntheticsLocationsType,
34
+ } from '../dsl/monitor';
35
+
36
+ export type MonitorSchema = Omit<MonitorConfig, 'locations'> & {
37
+ content: string;
38
+ locations: string[];
39
+ filter: Monitor['filter'];
40
+ };
41
+
42
+ // Internal representation of Locations that would be used when
43
+ // talking to the Kibana API
44
+ const LocationsMap: Record<SyntheticsLocationsType, string> = {
45
+ 'US East': 'us-east4-a',
46
+ 'EU West': 'europe-west2-a',
47
+ };
48
+ function translateLocation(locations: MonitorConfig['locations']) {
49
+ return locations.map(loc => LocationsMap[loc]).filter(Boolean);
50
+ }
51
+
52
+ export async function buildMonitorSchema(monitors: Monitor[]) {
53
+ if (monitors.length == 0) {
54
+ throw new Error('No Monitors found');
55
+ }
56
+ /**
57
+ * Set up the bundle artifacts path which can be used to
58
+ * create the bundles required for uploading journeys
59
+ */
60
+ const bundlePath = join(CACHE_PATH, 'bundles');
61
+ await mkdir(bundlePath, { recursive: true });
62
+ const bundler = new Bundler();
63
+ const schemas: MonitorSchema[] = [];
64
+
65
+ for (const monitor of monitors) {
66
+ const { source, config, filter } = monitor;
67
+ const outPath = join(bundlePath, config.name + '.zip');
68
+ const content = await bundler.build(source.file, outPath);
69
+ schemas.push({
70
+ ...config,
71
+ content,
72
+ locations: translateLocation(config.locations),
73
+ filter: filter,
74
+ });
75
+ }
76
+ return schemas;
77
+ }
@@ -0,0 +1,113 @@
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 path from 'path';
27
+ import fs from 'fs/promises';
28
+ import * as esbuild from 'esbuild';
29
+
30
+ export function commonOptions(): esbuild.BuildOptions {
31
+ return {
32
+ bundle: true,
33
+ external: ['node_modules', '@elastic/synthetics'],
34
+ minify: false,
35
+ platform: 'node',
36
+ format: 'esm',
37
+ write: false,
38
+ outExtension: {
39
+ '.js': '.js',
40
+ },
41
+ };
42
+ }
43
+
44
+ export type PluginData = {
45
+ path: string;
46
+ contents: string;
47
+ };
48
+ export type PluginCallback = (data: PluginData) => void;
49
+
50
+ export function MultiAssetPlugin(callback: PluginCallback): esbuild.Plugin {
51
+ const isBare = (str: string) => {
52
+ if (str.startsWith('/') || str.startsWith('./') || str.startsWith('../')) {
53
+ return true;
54
+ }
55
+ return false;
56
+ };
57
+
58
+ return {
59
+ name: 'esbuild-multiasset-plugin',
60
+ setup(build) {
61
+ build.onResolve({ filter: /.*?/ }, async args => {
62
+ // External and other packages need be marked external to
63
+ // be removed from the bundle
64
+ if (
65
+ build.initialOptions.external?.includes(args.path) ||
66
+ !isBare(args.path)
67
+ ) {
68
+ return {
69
+ external: true,
70
+ };
71
+ }
72
+
73
+ if (args.kind === 'entry-point') {
74
+ return {
75
+ path: args.path,
76
+ namespace: 'asset',
77
+ };
78
+ }
79
+
80
+ // If the modules are resolved locally, then
81
+ // use the imported path to get full path
82
+ const entryPath =
83
+ path.join(path.dirname(args.importer), args.path) +
84
+ path.extname(args.importer);
85
+ // Spin off another build to copy over the imported modules without bundling
86
+ const result = await esbuild.build({
87
+ ...commonOptions(),
88
+ entryPoints: {
89
+ [entryPath]: entryPath,
90
+ },
91
+ bundle: false,
92
+ external: [],
93
+ });
94
+
95
+ callback({
96
+ path: entryPath,
97
+ contents: result.outputFiles[0].text,
98
+ });
99
+
100
+ return {
101
+ errors: result.errors,
102
+ external: true,
103
+ };
104
+ });
105
+
106
+ build.onLoad({ filter: /.*?/, namespace: 'asset' }, async args => {
107
+ const contents = await fs.readFile(args.path, 'utf-8');
108
+ callback({ path: args.path, contents });
109
+ return { contents };
110
+ });
111
+ },
112
+ };
113
+ }
@@ -0,0 +1,123 @@
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 { bold, gray, yellow } from 'kleur/colors';
27
+ import { request } from 'undici';
28
+ import { PushOptions } from '../common_types';
29
+ import { indent, symbols } from '../helpers';
30
+ import { MonitorSchema } from './monitor';
31
+
32
+ /* eslint-disable @typescript-eslint/no-var-requires */
33
+ const { version } = require('../../package.json');
34
+
35
+ export type APISchema = {
36
+ project: string;
37
+ keep_stale: boolean;
38
+ monitors: MonitorSchema[];
39
+ };
40
+
41
+ function encodeAuth(auth: string) {
42
+ if (auth.includes(':')) {
43
+ return Buffer.from(auth).toString('base64');
44
+ }
45
+ return auth;
46
+ }
47
+
48
+ function getAPIUrl(options: PushOptions) {
49
+ return (
50
+ options.url + `/s/${options.space}/api/synthetics/service/project/monitors`
51
+ );
52
+ }
53
+
54
+ export function ok(statusCode: number) {
55
+ return statusCode >= 200 && statusCode <= 299;
56
+ }
57
+
58
+ export async function createMonitors(
59
+ monitors: MonitorSchema[],
60
+ options: PushOptions
61
+ ) {
62
+ const schema: APISchema = {
63
+ project: options.project,
64
+ keep_stale: !options.delete,
65
+ monitors,
66
+ };
67
+
68
+ return await request(getAPIUrl(options), {
69
+ method: 'PUT',
70
+ body: JSON.stringify(schema),
71
+ headers: {
72
+ authorization: `Basic ${encodeAuth(options.auth)}`,
73
+ 'content-type': 'application/json',
74
+ 'user-agent': `Elastic/Synthetics ${version}`,
75
+ 'kbn-xsrf': 'true',
76
+ },
77
+ });
78
+ }
79
+
80
+ export type APIMonitorError = {
81
+ id?: string;
82
+ reason: string;
83
+ details: string;
84
+ };
85
+
86
+ export function formatNotFoundError(message: string) {
87
+ return bold(
88
+ `${symbols['failed']} Please check your kibana url and try again - 404:${message}`
89
+ );
90
+ }
91
+
92
+ export function formatAPIError(
93
+ statuCode: number,
94
+ error: string,
95
+ message: string
96
+ ) {
97
+ let outer = bold(`${symbols['failed']} Error\n`);
98
+ let inner = bold(
99
+ `${symbols['failed']} monitor creation failed - ${statuCode}:${error}\n`
100
+ );
101
+ inner += indent(message, ' ');
102
+ outer += indent(inner);
103
+ return outer;
104
+ }
105
+
106
+ export function formatFailedMonitors(errors: APIMonitorError[]) {
107
+ let outer = bold(`${symbols['failed']} Error\n`);
108
+ for (const error of errors) {
109
+ const monitorId = error.id ? `: monitor(${error.id})` : '';
110
+ let inner = bold(`> ${error.reason}${monitorId}\n`);
111
+ inner += indent(error.details, ' ');
112
+ outer += indent(inner) + '\n';
113
+ outer += '\n';
114
+ }
115
+ return outer;
116
+ }
117
+
118
+ export function formatStaleMonitors() {
119
+ let outer = bold(yellow(`${symbols['warning']} Found stale monitors\n`));
120
+ const inner = gray(`Pass --delete to remove all the stale monitors.`);
121
+ outer += indent(inner, ' ');
122
+ return outer;
123
+ }
@@ -25,7 +25,6 @@
25
25
 
26
26
  import SonicBoom from 'sonic-boom';
27
27
  import { green, red, cyan } from 'kleur/colors';
28
- import Runner from '../core/runner';
29
28
  import {
30
29
  symbols,
31
30
  indent,
@@ -33,7 +32,13 @@ import {
33
32
  findPWLogsIndexes,
34
33
  rewriteErrorStack,
35
34
  } from '../helpers';
36
- import { ReporterOptions } from './reporter';
35
+ import { Reporter, ReporterOptions } from '.';
36
+ import {
37
+ JourneyEndResult,
38
+ JourneyStartResult,
39
+ StepEndResult,
40
+ } from '../common_types';
41
+ import { Journey, Step } from '../dsl';
37
42
 
38
43
  function renderError(error) {
39
44
  let output = '';
@@ -58,11 +63,16 @@ function renderDuration(durationMs) {
58
63
  return parseInt(durationMs);
59
64
  }
60
65
 
61
- export default class BaseReporter {
66
+ export default class BaseReporter implements Reporter {
62
67
  stream: SonicBoom;
63
68
  fd: number;
69
+ metrics = {
70
+ succeeded: 0,
71
+ failed: 0,
72
+ skipped: 0,
73
+ };
64
74
 
65
- constructor(public runner: Runner, options: ReporterOptions = {}) {
75
+ constructor(options: ReporterOptions = {}) {
66
76
  this.fd = options.fd || process.stdout.fd;
67
77
  /**
68
78
  * minLength is set to 1 byte to make sure we flush the
@@ -70,66 +80,57 @@ export default class BaseReporter {
70
80
  * before destroying the pipe with underlying file descriptor
71
81
  */
72
82
  this.stream = new SonicBoom({ fd: this.fd, sync: true, minLength: 1 });
73
-
74
- // flushSync is used here to make sure all the data from the underlying
75
- // SonicBoom stream buffer is completely written to the fd before closing
76
- // the process
77
- this.runner.on('end', () => this.stream.flushSync());
78
-
79
- this._registerListeners();
80
83
  }
81
84
 
82
- _registerListeners() {
83
- const result = {
84
- succeeded: 0,
85
- failed: 0,
86
- skipped: 0,
87
- };
88
-
89
- this.runner.on('journey:start', ({ journey }) => {
90
- this.write(`\nJourney: ${journey.name}`);
91
- });
92
-
93
- this.runner.on('step:end', ({ step, start, end, error, status }) => {
94
- const message = `${symbols[status]} Step: '${
95
- step.name
96
- }' ${status} (${renderDuration((end - start) * 1000)} ms)`;
97
- this.write(indent(message));
98
- error && this.write(renderError(error));
99
- result[status]++;
100
- });
101
-
102
- this.runner.on('journey:end', ({ error }) => {
103
- const { failed, succeeded, skipped } = result;
104
- const total = failed + succeeded + skipped;
85
+ onJourneyStart(journey: Journey, {}: JourneyStartResult) {
86
+ this.write(`\nJourney: ${journey.name}`);
87
+ }
105
88
 
106
- /**
107
- * Render the error on the terminal only when no steps could
108
- * be executed which means the error happened in one of the hooks
109
- */
110
- if (total === 0 && error) {
111
- this.write(renderError(error));
112
- }
113
- });
89
+ onStepEnd(journey: Journey, step: Step, result: StepEndResult) {
90
+ const { status, end, start, error } = result;
91
+ const message = `${symbols[status]} Step: '${
92
+ step.name
93
+ }' ${status} (${renderDuration((end - start) * 1000)} ms)`;
94
+ this.write(indent(message));
95
+ error && this.write(renderError(error));
96
+ this.metrics[status]++;
97
+ }
114
98
 
115
- this.runner.on('end', () => {
116
- const { failed, succeeded, skipped } = result;
117
- const total = failed + succeeded + skipped;
99
+ /* eslint-disable @typescript-eslint/no-unused-vars */
100
+ onJourneyEnd(journey: Journey, { error }: JourneyEndResult) {
101
+ const { failed, succeeded, skipped } = this.metrics;
102
+ const total = failed + succeeded + skipped;
103
+ /**
104
+ * Render the error on the terminal only when no steps could
105
+ * be executed which means the error happened in one of the hooks
106
+ */
107
+ if (total === 0 && error) {
108
+ this.write(renderError(error));
109
+ }
110
+ }
118
111
 
119
- let message = '\n';
120
- if (total === 0) {
121
- message = 'No tests found!';
122
- message += ` (${renderDuration(now())} ms) \n`;
123
- this.write(message);
124
- return;
125
- }
112
+ onEnd() {
113
+ const { failed, succeeded, skipped } = this.metrics;
114
+ const total = failed + succeeded + skipped;
126
115
 
127
- message += succeeded > 0 ? green(` ${succeeded} passed`) : '';
128
- message += failed > 0 ? red(` ${failed} failed`) : '';
129
- message += skipped > 0 ? cyan(` ${skipped} skipped`) : '';
116
+ let message = '\n';
117
+ if (total === 0) {
118
+ message = 'No tests found!';
130
119
  message += ` (${renderDuration(now())} ms) \n`;
131
120
  this.write(message);
132
- });
121
+ return;
122
+ }
123
+
124
+ message += succeeded > 0 ? green(` ${succeeded} passed`) : '';
125
+ message += failed > 0 ? red(` ${failed} failed`) : '';
126
+ message += skipped > 0 ? cyan(` ${skipped} skipped`) : '';
127
+ message += ` (${renderDuration(now())} ms) \n`;
128
+ this.write(message);
129
+
130
+ // flushSync is used here to make sure all the data from the underlying
131
+ // SonicBoom stream buffer is completely written to the fd before closing
132
+ // the process
133
+ this.stream.flushSync();
133
134
  }
134
135
 
135
136
  write(message) {
@@ -26,11 +26,34 @@
26
26
  import BaseReporter from './base';
27
27
  import JSONReporter from './json';
28
28
  import JUnitReporter from './junit';
29
- import Reporter, { ReporterOptions } from './reporter';
29
+ import { Journey, Step } from '../dsl';
30
+ import {
31
+ StartEvent,
32
+ JourneyEndResult,
33
+ JourneyStartResult,
34
+ StepEndResult,
35
+ } from '../common_types';
30
36
 
31
- export { Reporter, ReporterOptions };
32
- export const reporters = {
37
+ export type ReporterOptions = { fd?: number; colors?: boolean };
38
+ export type BuiltInReporterName = 'default' | 'json' | 'junit';
39
+ export type ReporterInstance = new (opts: ReporterOptions) => Reporter;
40
+ export const reporters: {
41
+ [key in BuiltInReporterName]: ReporterInstance;
42
+ } = {
33
43
  default: BaseReporter,
34
44
  json: JSONReporter,
35
45
  junit: JUnitReporter,
36
46
  };
47
+
48
+ export interface Reporter {
49
+ onStart?(params: StartEvent): void;
50
+ onJourneyRegister?(journey: Journey): void;
51
+ onJourneyStart?(journey: Journey, result: JourneyStartResult): void;
52
+ onStepStart?(journey: Journey, step: Step): void;
53
+ onStepEnd?(journey: Journey, step: Step, result: StepEndResult): void;
54
+ onJourneyEnd?(
55
+ journey: Journey,
56
+ result: JourneyEndResult
57
+ ): void | Promise<void>;
58
+ onEnd?(): void | Promise<void>;
59
+ }