Remove some unhandled rejections in tests

Summary: Fixed several tests that caused uncaught promise rejects to fire after the tests finished. This caused jest to fail if there are too many of them.

Reviewed By: aigoncharov

Differential Revision: D32118124

fbshipit-source-id: 50734dab6dee2efec7f056940af72858b27b1707
This commit is contained in:
Michel Weststrate
2021-11-03 03:12:39 -07:00
committed by Facebook GitHub Bot
parent 8f3e729b7b
commit 72ce759e61
7 changed files with 55 additions and 30 deletions

View File

@@ -225,7 +225,18 @@ test('new clients replace old ones', async () => {
expect(instance.instanceApi.disconnect).toBeCalledTimes(0); expect(instance.instanceApi.disconnect).toBeCalledTimes(0);
const client2 = await createClient(device, 'AnotherApp', client.query, true); const client2 = await createClient(device, 'AnotherApp', client.query, true);
handleClientConnected(null as any, store, logger, client2); await handleClientConnected(
{
exec: (async () => {
return {
success: {}, // {plugins: []},
};
}) as any,
},
store,
logger,
client2,
);
expect(client2.connected.get()).toBe(true); expect(client2.connected.get()).toBe(true);
const instance2 = client2.sandyPluginStates.get(plugin.id)!; const instance2 = client2.sandyPluginStates.get(plugin.id)!;

View File

@@ -9,7 +9,7 @@
import React from 'react'; import React from 'react';
import {State, Store} from '../reducers/index'; import {State, Store} from '../reducers/index';
import {Logger} from 'flipper-common'; import {FlipperServer, Logger} from 'flipper-common';
import {FlipperServerImpl} from 'flipper-server-core'; import {FlipperServerImpl} from 'flipper-server-core';
import {selectClient} from '../reducers/connections'; import {selectClient} from '../reducers/connections';
import Client from '../Client'; import Client from '../Client';
@@ -168,7 +168,7 @@ export default async (store: Store, logger: Logger) => {
}; };
export async function handleClientConnected( export async function handleClientConnected(
server: FlipperServerImpl, server: Pick<FlipperServer, 'exec'>,
store: Store, store: Store,
logger: Logger, logger: Logger,
{id, query}: ClientDescription, {id, query}: ClientDescription,

View File

@@ -58,7 +58,7 @@ export default class MockFlipper {
private _clients: Client[] = []; private _clients: Client[] = [];
private _deviceCounter: number = 0; private _deviceCounter: number = 0;
private _clientCounter: number = 0; private _clientCounter: number = 0;
private _flipperServer: FlipperServer = createFlipperServerMock(); flipperServer: FlipperServer = createFlipperServerMock();
public get store(): Store { public get store(): Store {
return this._store; return this._store;
@@ -98,7 +98,7 @@ export default class MockFlipper {
this._store.dispatch(registerPlugins(plugins ?? [])); this._store.dispatch(registerPlugins(plugins ?? []));
this._store.dispatch({ this._store.dispatch({
type: 'SET_FLIPPER_SERVER', type: 'SET_FLIPPER_SERVER',
payload: this._flipperServer, payload: this.flipperServer,
}); });
} }

View File

@@ -26,7 +26,7 @@ import BaseDevice from '../devices/BaseDevice';
import {Store} from '../reducers/index'; import {Store} from '../reducers/index';
import Client from '../Client'; import Client from '../Client';
import {ClientQuery, Logger} from 'flipper-common'; import {ClientQuery, FlipperServer, Logger} from 'flipper-common';
import {FlipperDevicePlugin, FlipperPlugin, PluginDefinition} from '../plugin'; import {FlipperDevicePlugin, FlipperPlugin, PluginDefinition} from '../plugin';
import PluginContainer from '../PluginContainer'; import PluginContainer from '../PluginContainer';
import {isDevicePluginDefinition} from '../utils/pluginUtils'; import {isDevicePluginDefinition} from '../utils/pluginUtils';
@@ -42,6 +42,7 @@ export type MockFlipperResult = {
client: Client; client: Client;
device: BaseDevice; device: BaseDevice;
store: Store; store: Store;
server: FlipperServer;
pluginKey: string; pluginKey: string;
sendError(error: any, client?: Client): void; sendError(error: any, client?: Client): void;
sendMessage(method: string, params: any, client?: Client): void; sendMessage(method: string, params: any, client?: Client): void;
@@ -126,6 +127,7 @@ export async function createMockFlipperWithPlugin(
}); });
const logger = mockFlipper.logger; const logger = mockFlipper.logger;
const store = mockFlipper.store; const store = mockFlipper.store;
const server = mockFlipper.flipperServer;
const createDevice = (options: Parameters<MockFlipper['createDevice']>[0]) => const createDevice = (options: Parameters<MockFlipper['createDevice']>[0]) =>
mockFlipper.createDevice(options); mockFlipper.createDevice(options);
@@ -206,6 +208,7 @@ export async function createMockFlipperWithPlugin(
client, client,
device: device as any, device: device as any,
store, store,
server,
selectPlugin: selectPluginImpl, selectPlugin: selectPluginImpl,
sendError(error: any, actualClient = client) { sendError(error: any, actualClient = client) {
actualClient.onMessage( actualClient.onMessage(

View File

@@ -333,7 +333,11 @@ export abstract class BasePluginInstance {
if (!this.activated) { if (!this.activated) {
this.flipperLib.enableMenuEntries(this.menuEntries); this.flipperLib.enableMenuEntries(this.menuEntries);
this.activated = true; this.activated = true;
try {
this.events.emit('activate'); this.events.emit('activate');
} catch (e) {
console.error(`Failed to activate plugin: ${this.definition.id}`, e);
}
this.flipperLib.logger.trackTimeSince( this.flipperLib.logger.trackTimeSince(
`activePlugin-${this.definition.id}`, `activePlugin-${this.definition.id}`,
); );
@@ -347,7 +351,11 @@ export abstract class BasePluginInstance {
if (this.activated) { if (this.activated) {
this.activated = false; this.activated = false;
this.lastDeeplink = undefined; this.lastDeeplink = undefined;
try {
this.events.emit('deactivate'); this.events.emit('deactivate');
} catch (e) {
console.error(`Failed to deactivate plugin: ${this.definition.id}`, e);
}
} }
} }

View File

@@ -334,8 +334,10 @@ export function startDevicePlugin<Module extends FlipperDevicePluginModule>(
}, },
}; };
(res as any)._backingInstance = pluginInstance; (res as any)._backingInstance = pluginInstance;
if (!options?.startUnactivated) {
// we start connected // we start connected
pluginInstance.activate(); pluginInstance.activate();
}
return res; return res;
} }

View File

@@ -89,11 +89,11 @@ type CertificateProviderConfig = {
* Flipper CA. * Flipper CA.
*/ */
export default class CertificateProvider { export default class CertificateProvider {
logger: Logger; private logger: Logger;
_adb: Promise<ADBClient> | undefined; private _adb: Promise<ADBClient> | undefined;
certificateSetup: Promise<void>; private didCertificateSetup = false;
config: CertificateProviderConfig; private config: CertificateProviderConfig;
server: ServerController; private server: ServerController;
get adb(): Promise<ADBClient> { get adb(): Promise<ADBClient> {
if (this.config.enableAndroid) { if (this.config.enableAndroid) {
@@ -125,20 +125,6 @@ export default class CertificateProvider {
this._adb = undefined; // no adb client available this._adb = undefined; // no adb client available
}) as Promise<ADBClient>) }) as Promise<ADBClient>)
: undefined; : undefined;
if (isTest()) {
this.certificateSetup = Promise.reject(
new Error('Server certificates not available in test'),
);
} else {
this.certificateSetup = reportPlatformFailures(
this.ensureServerCertExists(),
'ensureServerCertExists',
);
// make sure initialization failure is already logged
this.certificateSetup.catch((e) => {
console.error('Failed to find or generate certificates', e);
});
}
this.config = config; this.config = config;
this.server = server; this.server = server;
} }
@@ -162,6 +148,21 @@ export default class CertificateProvider {
); );
}; };
async certificateSetup() {
if (this.didCertificateSetup) {
return;
}
if (isTest()) {
throw new Error('Server certificates not available in test');
} else {
await reportPlatformFailures(
this.ensureServerCertExists(),
'ensureServerCertExists',
);
}
this.didCertificateSetup = true;
}
async processCertificateSigningRequest( async processCertificateSigningRequest(
unsanitizedCsr: string, unsanitizedCsr: string,
os: string, os: string,
@@ -176,7 +177,7 @@ export default class CertificateProvider {
const rootFolder = await promisify(tmp.dir)(); const rootFolder = await promisify(tmp.dir)();
const certFolder = rootFolder + '/FlipperCerts/'; const certFolder = rootFolder + '/FlipperCerts/';
const certsZipPath = rootFolder + '/certs.zip'; const certsZipPath = rootFolder + '/certs.zip';
await this.certificateSetup; await this.certificateSetup();
const caCert = await this.getCACertificate(); const caCert = await this.getCACertificate();
await this.deployOrStageFileForMobileApp( await this.deployOrStageFileForMobileApp(
appDirectory, appDirectory,