Skip to content

Commit c500e52

Browse files
chore: fix flaky tests in CLI mock (#437)
* chore: fix flaky tests in CLI mock * fix cli tests * switch off screenshots for tls
1 parent 2e0feff commit c500e52

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

__tests__/cli.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ describe('CLI', () => {
187187
])
188188
.run();
189189
await cli.waitFor('journey/end');
190+
expect(await cli.exitCode).toBe(0);
190191

191192
const data = safeParse(cli.buffer());
192193
const screenshotRef = data.find(
@@ -201,9 +202,7 @@ describe('CLI', () => {
201202

202203
const traceData = data.find(({ type }) => type === 'step/metrics');
203204
expect(traceData).toBeDefined();
204-
205-
expect(await cli.exitCode).toBe(0);
206-
}, 30000);
205+
});
207206

208207
it('override screenshots with `--rich-events` flag', async () => {
209208
const cli = new CLIMock()
@@ -215,10 +214,9 @@ describe('CLI', () => {
215214
])
216215
.run();
217216
await cli.waitFor('journey/end');
218-
const screenshots = cli
219-
.buffer()
220-
.map(data => JSON.parse(data))
221-
.find(({ type }) => type === 'step/screenshot_ref');
217+
const screenshots = safeParse(cli.buffer()).find(
218+
({ type }) => type === 'step/screenshot_ref'
219+
);
222220
expect(screenshots).not.toBeDefined();
223221
expect(await cli.exitCode).toBe(0);
224222
});
@@ -366,6 +364,8 @@ describe('CLI', () => {
366364
JSON.stringify({ url: tlsServer.TEST_PAGE }),
367365
'--reporter',
368366
'json',
367+
'--screenshots',
368+
'off',
369369
];
370370
});
371371

@@ -559,7 +559,7 @@ class CLIMock {
559559
this.data = data.toString();
560560
// Uncomment the line below if the process is blocked and you need to see its output
561561
// console.log('CLIMock.stdout:', this.data);
562-
this.chunks.push(...this.data.split('\n').filter(Boolean));
562+
this.chunks.push(this.data);
563563
if (this.waitForPromise && this.data.includes(this.waitForText)) {
564564
this.process.stdout.off('data', dataListener);
565565
this.waitForPromise();
@@ -588,6 +588,9 @@ class CLIMock {
588588
}
589589

590590
buffer() {
591-
return this.chunks;
591+
// Merge all the interleaved chunks from stdout and
592+
// split them on new line as synthetics runner writes the
593+
// JSON output in separate lines for every event.
594+
return this.chunks.join('').split('\n').filter(Boolean);
592595
}
593596
}

__tests__/fixtures/example.journey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { journey, step } from '../../';
2727

2828
journey('example journey', ({ page, params }) => {
2929
step('go to test page', async () => {
30-
await page.goto(params.url, { timeout: 1500 });
30+
await page.goto(params.url, { waitUntil: 'networkidle' });
3131
await page.evaluate(() => window.performance.mark('page-loaded'));
3232
await page.waitForSelector('h2.synthetics', { timeout: 1500 });
3333
});

__tests__/plugins/network.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,20 @@ describe('network', () => {
114114

115115
it('timings for aborted requests', async () => {
116116
const driver = await Gatherer.setupDriver({ wsEndpoint });
117+
await driver.client.send('Network.setCacheDisabled', {
118+
cacheDisabled: true,
119+
});
117120
const network = new NetworkManager(driver);
118121
await network.start();
119122

120123
const delayTime = 20;
121-
server.route('/delay100', async (req, res) => {
124+
server.route('/delay20', async (req, res) => {
122125
await delay(delayTime);
123126
res.destroy();
124127
});
125128
server.route('/index', async (_, res) => {
126129
res.setHeader('content-type', 'text/html');
127-
res.end(`<script src=${server.PREFIX}/delay100 />`);
130+
res.end(`<script src=${server.PREFIX}/delay20 />`);
128131
});
129132

130133
await driver.page.goto(server.PREFIX + '/index');
@@ -133,7 +136,7 @@ describe('network', () => {
133136
const netinfo = await network.stop();
134137
expect(netinfo.length).toBe(2);
135138
expect(netinfo[1]).toMatchObject({
136-
url: `${server.PREFIX}/delay100`,
139+
url: `${server.PREFIX}/delay20`,
137140
response: {
138141
headers: {},
139142
mimeType: 'x-unknown',

0 commit comments

Comments
 (0)