Skip to content

Commit

Permalink
chore: fix flaky tests in CLI mock (#437)
Browse files Browse the repository at this point in the history
* chore: fix flaky tests in CLI mock

* fix cli tests

* switch off screenshots for tls
  • Loading branch information
vigneshshanmugam authored Mar 28, 2022
1 parent 2e0feff commit c500e52
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
21 changes: 12 additions & 9 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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);
});
Expand Down Expand Up @@ -366,6 +364,8 @@ describe('CLI', () => {
JSON.stringify({ url: tlsServer.TEST_PAGE }),
'--reporter',
'json',
'--screenshots',
'off',
];
});

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion __tests__/fixtures/example.journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
Expand Down
9 changes: 6 additions & 3 deletions __tests__/plugins/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<script src=${server.PREFIX}/delay100 />`);
res.end(`<script src=${server.PREFIX}/delay20 />`);
});

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

0 comments on commit c500e52

Please sign in to comment.