Skip to content

Commit 08957b0

Browse files
chore: restructure to add sub commands to cli (#475)
* chore: restructure to add sub commands to cli * remove snapshot and keep test local * address review comments * add quiet exit code back
1 parent 0ee9fac commit 08957b0

File tree

11 files changed

+523
-443
lines changed

11 files changed

+523
-443
lines changed

__tests__/cli.test.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ import { devices } from 'playwright-chromium';
2929
import { Server } from './utils/server';
3030
import { megabitsToBytes, DEFAULT_NETWORK_CONDITIONS } from '../src/helpers';
3131

32+
const safeParse = (chunks: string[]) => {
33+
return chunks.map(data => {
34+
try {
35+
return JSON.parse(data);
36+
} catch (e) {
37+
throw `Error ${e} could not parse data '${data}'`;
38+
}
39+
});
40+
};
41+
3242
describe('CLI', () => {
3343
let server: Server;
3444
let serverParams: { url: string };
@@ -178,23 +188,20 @@ describe('CLI', () => {
178188
.run();
179189
await cli.waitFor('journey/end');
180190

181-
const screenshotRef = cli
182-
.buffer()
183-
.map(data => JSON.parse(data))
184-
.find(({ type }) => type === 'step/screenshot_ref');
191+
const data = safeParse(cli.buffer());
192+
const screenshotRef = data.find(
193+
({ type }) => type === 'step/screenshot_ref'
194+
);
185195
expect(screenshotRef).toBeDefined();
186196

187-
const networkData = cli
188-
.buffer()
189-
.map(data => JSON.parse(data))
190-
.find(({ type }) => type === 'journey/network_info');
197+
const networkData = data.find(
198+
({ type }) => type === 'journey/network_info'
199+
);
191200
expect(networkData).toBeDefined();
192201

193-
const traceData = cli
194-
.buffer()
195-
.map(data => JSON.parse(data))
196-
.find(({ type }) => type === 'step/metrics');
202+
const traceData = data.find(({ type }) => type === 'step/metrics');
197203
expect(traceData).toBeDefined();
204+
198205
expect(await cli.exitCode).toBe(0);
199206
}, 30000);
200207

@@ -226,13 +233,7 @@ describe('CLI', () => {
226233
])
227234
.run();
228235
await cli.waitFor('journey/end');
229-
const data = cli.buffer().map(data => {
230-
try {
231-
return JSON.parse(data);
232-
} catch (e) {
233-
throw `Error ${e} could not parse data '${data}'`;
234-
}
235-
});
236+
const data = safeParse(cli.buffer());
236237
const screenshotRef = data.find(
237238
({ type }) => type === 'step/screenshot_ref'
238239
);

__tests__/index.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
import fs from 'fs';
2727
import { run, journey } from '../src/index';
28-
import { runner } from '../src/core';
29-
import { RunOptions } from '../src/core/runner';
3028
import { generateTempPath } from '../src/helpers';
3129

3230
describe('Run', () => {
@@ -54,28 +52,4 @@ describe('Run', () => {
5452
{},
5553
]);
5654
});
57-
58-
it('calls runner with proper options', async () => {
59-
const runnerSpy = jest
60-
.spyOn(runner, 'run')
61-
.mockImplementation(() => Promise.resolve({}));
62-
63-
const options: RunOptions = {
64-
environment: 'debug',
65-
params: {
66-
foo: 'bar',
67-
},
68-
screenshots: 'on',
69-
filmstrips: false,
70-
trace: false,
71-
dryRun: true,
72-
match: 'check*',
73-
network: true,
74-
pauseOnError: true,
75-
quietExitCode: true,
76-
reporter: 'json',
77-
};
78-
await run(options);
79-
expect(runnerSpy.mock.calls[0][0]).toEqual(options);
80-
});
8155
});

__tests__/options.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 { CliArgs } from '../src/common_types';
27+
import { normalizeOptions } from '../src/options';
28+
import { join } from 'path';
29+
30+
describe('options', () => {
31+
it('normalize', async () => {
32+
const cliArgs: CliArgs = {
33+
params: {
34+
foo: 'bar',
35+
},
36+
headless: true,
37+
sandbox: false,
38+
screenshots: 'on',
39+
dryRun: true,
40+
match: 'check*',
41+
pauseOnError: true,
42+
config: join(__dirname, 'fixtures', 'synthetics.config.ts'),
43+
};
44+
expect(normalizeOptions({})).toMatchObject({
45+
environment: 'test',
46+
params: {},
47+
});
48+
expect(normalizeOptions(cliArgs)).toMatchObject({
49+
dryRun: true,
50+
environment: 'test',
51+
match: 'check*',
52+
params: {
53+
foo: 'bar',
54+
url: 'non-dev',
55+
},
56+
pauseOnError: true,
57+
playwrightOptions: {
58+
chromiumSandbox: false,
59+
defaultBrowserType: 'chromium',
60+
deviceScaleFactor: 4.5,
61+
hasTouch: true,
62+
headless: true,
63+
ignoreHTTPSErrors: undefined,
64+
isMobile: true,
65+
userAgent:
66+
'Mozilla/5.0 (Linux; Android 8.0.0; SM-G965U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4595.0 Mobile Safari/537.36',
67+
viewport: {
68+
height: 658,
69+
width: 320,
70+
},
71+
},
72+
screenshots: 'on',
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)