Testing Forge Custom UI components Using Jest

Hey @bongartz, I’ve just been able successfully mock @forge/bridge in my Custom UI app (for my use case, to use view.getContext).

I put the following file at static/<APP_NAME>/src/__mocks__/@forge/bridge/index.ts:

import { Context } from "../path/to/definition/for/context";

let context: Context | null = null;

export function __setContext(newContext: Context) {
  context = newContext;
}

export const view = {
  getContext: () => context,
};

and then I can use the mock in a test like so:

import { getIssueKeyFromContext } from "./getIssueKeyFromContext";

test("gets issue key", async () => {
  // Arrange
  require("@forge/bridge").__setContext({
    extension: { type: "jira:issueAction", issue: { key: "KEY-123" } },
  });

  // Act
  const key = await getIssueKeyFromContext();

  // Assert
  expect(key).toBe("KEY-123");
});

Hope this is helpful.