From c500e52624b1819335ccc7d5470cb94c138018c4 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Mon, 28 Mar 2022 10:24:42 -0700 Subject: [PATCH] chore: fix flaky tests in CLI mock (#437) * chore: fix flaky tests in CLI mock * fix cli tests * switch off screenshots for tls --- __tests__/cli.test.ts | 21 ++++++++++++--------- __tests__/fixtures/example.journey.ts | 2 +- __tests__/plugins/network.test.ts | 9 ++++++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/__tests__/cli.test.ts b/__tests__/cli.test.ts index 42517658..85484619 100644 --- a/__tests__/cli.test.ts +++ b/__tests__/cli.test.ts @@ -187,6 +187,7 @@ describe('CLI', () => { ]) .run(); await cli.waitFor('journey/end'); + expect(await cli.exitCode).toBe(0); const data = safeParse(cli.buffer()); const screenshotRef = data.find( @@ -201,9 +202,7 @@ describe('CLI', () => { const traceData = data.find(({ type }) => type === 'step/metrics'); expect(traceData).toBeDefined(); - - expect(await cli.exitCode).toBe(0); - }, 30000); + }); it('override screenshots with `--rich-events` flag', async () => { const cli = new CLIMock() @@ -215,10 +214,9 @@ describe('CLI', () => { ]) .run(); await cli.waitFor('journey/end'); - const screenshots = cli - .buffer() - .map(data => JSON.parse(data)) - .find(({ type }) => type === 'step/screenshot_ref'); + const screenshots = safeParse(cli.buffer()).find( + ({ type }) => type === 'step/screenshot_ref' + ); expect(screenshots).not.toBeDefined(); expect(await cli.exitCode).toBe(0); }); @@ -366,6 +364,8 @@ describe('CLI', () => { JSON.stringify({ url: tlsServer.TEST_PAGE }), '--reporter', 'json', + '--screenshots', + 'off', ]; }); @@ -559,7 +559,7 @@ class CLIMock { this.data = data.toString(); // Uncomment the line below if the process is blocked and you need to see its output // console.log('CLIMock.stdout:', this.data); - this.chunks.push(...this.data.split('\n').filter(Boolean)); + this.chunks.push(this.data); if (this.waitForPromise && this.data.includes(this.waitForText)) { this.process.stdout.off('data', dataListener); this.waitForPromise(); @@ -588,6 +588,9 @@ class CLIMock { } buffer() { - return this.chunks; + // Merge all the interleaved chunks from stdout and + // split them on new line as synthetics runner writes the + // JSON output in separate lines for every event. + return this.chunks.join('').split('\n').filter(Boolean); } } diff --git a/__tests__/fixtures/example.journey.ts b/__tests__/fixtures/example.journey.ts index fa663a2d..7f6e17c6 100644 --- a/__tests__/fixtures/example.journey.ts +++ b/__tests__/fixtures/example.journey.ts @@ -27,7 +27,7 @@ import { journey, step } from '../../'; journey('example journey', ({ page, params }) => { step('go to test page', async () => { - await page.goto(params.url, { timeout: 1500 }); + await page.goto(params.url, { waitUntil: 'networkidle' }); await page.evaluate(() => window.performance.mark('page-loaded')); await page.waitForSelector('h2.synthetics', { timeout: 1500 }); }); diff --git a/__tests__/plugins/network.test.ts b/__tests__/plugins/network.test.ts index 9d6fcff8..5555165a 100644 --- a/__tests__/plugins/network.test.ts +++ b/__tests__/plugins/network.test.ts @@ -114,17 +114,20 @@ describe('network', () => { it('timings for aborted requests', async () => { const driver = await Gatherer.setupDriver({ wsEndpoint }); + await driver.client.send('Network.setCacheDisabled', { + cacheDisabled: true, + }); const network = new NetworkManager(driver); await network.start(); const delayTime = 20; - server.route('/delay100', async (req, res) => { + server.route('/delay20', async (req, res) => { await delay(delayTime); res.destroy(); }); server.route('/index', async (_, res) => { res.setHeader('content-type', 'text/html'); - res.end(`