Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Fix browser diagnostic Jest test flakiness #118003

Merged
merged 8 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

const BROWSER_LAUNCH_TIME_TO_WAIT = 5 * 1000;

export const getBrowserLaunchTime = () => BROWSER_LAUNCH_TIME_TO_WAIT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the chosen approach is correct. It is not a good idea to have a helper function returning a constant just for unit tests.
Have you tried jest.useFakeTimers?
If that doesn't work, then the second option will be using RxJS marbles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll look into that 👍🏻

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { ReportingCore } from '../../../';
import { LevelLogger } from '../../../lib';
import { ChromiumArchivePaths } from '../paths';
import { args } from './args';
import { getBrowserLaunchTime } from './constants';

const paths = new ChromiumArchivePaths();
const browserLaunchTimeToWait = 5 * 1000;

// Default args used by pptr
// https://github.com/puppeteer/puppeteer/blob/13ea347/src/node/Launcher.ts#L168
Expand Down Expand Up @@ -121,7 +121,7 @@ export const browserStartLogs = (
// logs as sometimes it's "bind" successfully for remote connections, but later emit
// a log indicative of an issue (for example, no default font found).
return merge(exit$, error$, log$).pipe(
takeUntil(timer(browserLaunchTimeToWait)),
takeUntil(timer(getBrowserLaunchTime())),
reduce((acc, curr) => `${acc}${curr}\n`, ''),
tap(() => {
if (browserProcess && browserProcess.pid && !browserProcess.killed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
} from '../../test_helpers';
import type { ReportingRequestHandlerContext } from '../../types';
import { registerDiagnoseBrowser } from './browser';
import { getBrowserLaunchTime } from '../../browsers/chromium/driver_factory/constants';

jest.mock('../../browsers/chromium/driver_factory/constants');
jest.mock('child_process');
jest.mock('readline');

Expand All @@ -28,8 +30,7 @@ type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;
const devtoolMessage = 'DevTools listening on (ws://localhost:4000)';
const fontNotFoundMessage = 'Could not find the default font';

// FLAKY: https://github.com/elastic/kibana/issues/89369
describe.skip('POST /diagnose/browser', () => {
describe('POST /diagnose/browser', () => {
jest.setTimeout(6000);
const reportingSymbol = Symbol('reporting');
const mockLogger = createMockLevelLogger();
Expand All @@ -46,6 +47,8 @@ describe.skip('POST /diagnose/browser', () => {
});

beforeEach(async () => {
(getBrowserLaunchTime as jest.Mock).mockReturnValue(500);

({ server, httpSetup } = await setupServer(reportingSymbol));
httpSetup.registerRouteHandlerContext<ReportingRequestHandlerContext, 'reporting'>(
reportingSymbol,
Expand Down