From 10ccc38fcc0c9d9aa02109f815e3d467c4fdb179 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Fri, 5 Jun 2020 11:30:53 -0700 Subject: [PATCH] Fix test mocks + add some plugin async helpers where needed --- .../server/config/create_config.test.ts | 6 +++++- x-pack/plugins/reporting/server/plugin.ts | 19 +++++++++++++++++++ .../create_mock_browserdriverfactory.ts | 3 +-- .../create_mock_reportingplugin.ts | 18 +++++++++++++++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/reporting/server/config/create_config.test.ts b/x-pack/plugins/reporting/server/config/create_config.test.ts index 1c4c840cfa312..8ad8042a93105 100644 --- a/x-pack/plugins/reporting/server/config/create_config.test.ts +++ b/x-pack/plugins/reporting/server/config/create_config.test.ts @@ -45,7 +45,11 @@ describe('Reporting server createConfig$', () => { mockInitContext = makeMockInitContext({ kibanaServer: {}, }); - mockLogger = ({ warn: jest.fn(), debug: jest.fn() } as unknown) as LevelLogger; + mockLogger = ({ + warn: jest.fn(), + debug: jest.fn(), + clone: jest.fn().mockImplementation(() => mockLogger), + } as unknown) as LevelLogger; }); afterEach(() => { diff --git a/x-pack/plugins/reporting/server/plugin.ts b/x-pack/plugins/reporting/server/plugin.ts index ed4939fcc74f3..5eadcfab7ed28 100644 --- a/x-pack/plugins/reporting/server/plugin.ts +++ b/x-pack/plugins/reporting/server/plugin.ts @@ -20,9 +20,26 @@ export class ReportingPlugin private logger: LevelLogger; private reportingCore?: ReportingCore; + private setupDone: (val?: unknown) => void = () => {}; + private startDone: (val?: unknown) => void = () => {}; + private setupPromise: Promise; + private startPromise: Promise; + constructor(context: PluginInitializerContext) { this.logger = new LevelLogger(context.logger.get()); this.initializerContext = context; + + // Setup some promises for modules that need to await setup/start + this.setupPromise = new Promise((r) => (this.setupDone = r)); + this.startPromise = new Promise((r) => (this.startDone = r)); + } + + public async setupReady() { + return this.setupPromise; + } + + public async startReady() { + return this.startPromise; } public setup(core: CoreSetup, plugins: ReportingSetupDeps) { @@ -49,6 +66,7 @@ export class ReportingPlugin this.reportingCore = reportingCore; this.logger.debug('Setup complete'); + this.setupDone(); }); return {}; @@ -81,6 +99,7 @@ export class ReportingPlugin runValidations(config, elasticsearch, browserDriverFactory, this.logger); this.logger.debug('Start complete'); + this.startDone(); }); return {}; diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts index 0e8be7578e988..97e22e2ca2863 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts @@ -6,8 +6,7 @@ import { Page } from 'puppeteer'; import * as Rx from 'rxjs'; -import { HeadlessChromiumDriver, HeadlessChromiumDriverFactory } from '../browsers'; -import { chromium } from '../browsers'; +import { chromium, HeadlessChromiumDriver, HeadlessChromiumDriverFactory } from '../browsers'; import * as contexts from '../export_types/common/lib/screenshots/constants'; import { LevelLogger } from '../lib'; import { CaptureConfig, ElementsPositionAndAttribute } from '../types'; diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts index b04e697d0a118..085f4c9995fab 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts @@ -7,18 +7,28 @@ jest.mock('../routes'); jest.mock('../usage'); jest.mock('../browsers'); -jest.mock('../browsers'); jest.mock('../lib/create_queue'); jest.mock('../lib/enqueue_job'); jest.mock('../lib/validate'); import { of } from 'rxjs'; import { coreMock } from 'src/core/server/mocks'; +import { + initializeBrowserDriverFactory, + HeadlessChromiumDriverFactory, + chromium, +} from '../browsers'; import { ReportingConfig, ReportingCore } from '../'; import { ReportingInternalSetup } from '../core'; import { ReportingPlugin } from '../plugin'; import { ReportingSetupDeps, ReportingStartDeps } from '../types'; +(initializeBrowserDriverFactory as jest.Mock< + Promise +>).mockImplementation(() => Promise.resolve({} as HeadlessChromiumDriverFactory)); + +(chromium as any).createDriverFactory.mockImplementation(() => ({})); + const createMockSetupDeps = (setupMock?: any): ReportingSetupDeps => { return { security: setupMock.security, @@ -57,8 +67,10 @@ const createMockReportingPlugin = async (config: ReportingConfig): Promise