Flipper Test Utilities

0.273.0 · active · verified Wed Apr 22

flipper-test-utils is a library providing common testing utilities specifically designed for developing and testing Flipper plugins and components within the Flipper ecosystem. It is an integral part of the larger Flipper monorepo maintained by Facebook, currently at version 0.273.0. The package has a rapid release cadence, with updates typically aligning with the main Flipper desktop application releases (often weekly or bi-weekly). Its primary purpose is to facilitate isolated and integrated testing of Flipper plugins by offering functionalities like component rendering (often re-exporting or wrapping `@testing-library/react` utilities), mocking the Flipper client API, and providing helper functions for common testing scenarios. This allows developers to thoroughly test their plugins' UI, data interactions, and Flipper host communications without needing a full Flipper desktop environment.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to render a Flipper plugin component and mock its Flipper client interactions for testing.

import React from 'react';
import { render, createStubFlipperLib } from 'flipper-test-utils';
import { act } from 'react-dom/test-utils'; // Essential for React 18+ tests
import { FlipperPlugin, Flipper } from 'flipper-plugin';

interface MyPluginClient {
  getGreeting(): Promise<string>;
}

// Define a simple Flipper plugin component
class MyFlipperPlugin extends FlipperPlugin<any, any, any> {
  static id = 'MyFlipperPlugin';
  static title = 'My Flipper Plugin';

  constructor(flipper: Flipper) {
    super(flipper);
  }

  render() {
    return (
      <div>
        <h1>Hello from My Flipper Plugin!</h1>
        <GreetingFetcher client={this.client} />
      </div>
    );
  }
}

// A component that uses the Flipper client
function GreetingFetcher({ client }: { client: MyPluginClient }) {
  const [greeting, setGreeting] = React.useState('Loading...');

  React.useEffect(() => {
    const fetchGreeting = async () => {
      const message = await client.getGreeting();
      setGreeting(message);
    };
    fetchGreeting();
  }, [client]);

  return <p>{greeting}</p>;
}

describe('MyFlipperPlugin', () => {
  it('renders correctly and fetches a greeting', async () => {
    const stubFlipperLib = createStubFlipperLib();

    // Mock the client's method directly on the stub
    stubFlipperLib.pluginClient.getGreeting = async () => 'Hello World!';

    let rendered;
    await act(async () => {
      rendered = render(
        <MyFlipperPlugin flipper={stubFlipperLib} />, 
        { wrapper: ({ children }) => <div>{children}</div> } // Provide a simple wrapper if needed
      );
    });

    expect(rendered.getByText('Hello from My Flipper Plugin!')).toBeInTheDocument();
    // Wait for the async effect to complete
    await rendered.findByText('Hello World!'); 
    expect(rendered.getByText('Hello World!')).toBeInTheDocument();
  });
});

view raw JSON →