Skip to main content
Glama

detox_generate_expectation

Generate Detox mobile testing assertions for React Native apps to verify UI elements like visibility, text, labels, and values during end-to-end testing.

Instructions

Generate Detox expectation/assertion code (toBeVisible, toHaveText, etc.).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expectationTypeYesType of expectation
elementMatcherYesMatcher code or description
expectedValueNoExpected value
negatedNo
timeoutNoCustom timeout in ms
visibilityThresholdNo

Implementation Reference

  • MCP tool definition and handler for 'detox_generate_expectation'. Validates input using GenerateExpectationArgsSchema and generates code by calling generateExpectationCode.
    export const generateExpectationTool: Tool = {
      name: "detox_generate_expectation",
      description: "Generate Detox expectation/assertion code (toBeVisible, toHaveText, etc.).",
      inputSchema: zodToJsonSchema(GenerateExpectationArgsSchema),
      handler: async (args: z.infer<typeof GenerateExpectationArgsSchema>) => {
        const parsed = GenerateExpectationArgsSchema.parse(args);
    
        const code = generateExpectationCode({
          expectationType: parsed.expectationType,
          elementMatcher: parsed.elementMatcher,
          expectedValue: parsed.expectedValue,
          negated: parsed.negated,
          timeout: parsed.timeout,
          visibilityThreshold: parsed.visibilityThreshold,
        });
    
        return {
          success: true,
          code,
        };
      },
    };
  • Zod schema defining the input arguments for the detox_generate_expectation tool.
    export const GenerateExpectationArgsSchema = z.object({
      expectationType: z
        .enum([
          "toBeVisible",
          "toExist",
          "toBeFocused",
          "toHaveText",
          "toHaveLabel",
          "toHaveId",
          "toHaveValue",
          "toHaveSliderPosition",
          "toHaveToggleValue",
        ])
        .describe("Type of expectation"),
      elementMatcher: z.string().describe("Matcher code or description"),
      expectedValue: z.string().optional().describe("Expected value"),
      negated: z.boolean().optional().default(false),
      timeout: z.number().optional().describe("Custom timeout in ms"),
      visibilityThreshold: z.number().min(1).max(100).optional(),
    });
  • Core helper function that generates the Detox expectation code snippet based on provided options.
    export function generateExpectationCode(options: {
      expectationType:
        | "toBeVisible"
        | "toExist"
        | "toBeFocused"
        | "toHaveText"
        | "toHaveLabel"
        | "toHaveId"
        | "toHaveValue"
        | "toHaveSliderPosition"
        | "toHaveToggleValue";
      elementMatcher: string;
      expectedValue?: string | number | boolean;
      negated?: boolean;
      timeout?: number;
      visibilityThreshold?: number;
    }): string {
      const element = options.elementMatcher;
      const not = options.negated ? ".not" : "";
      let expectation: string;
    
      switch (options.expectationType) {
        case "toBeVisible":
          if (options.visibilityThreshold) {
            expectation = `toBeVisible(${options.visibilityThreshold})`;
          } else {
            expectation = "toBeVisible()";
          }
          break;
    
        case "toExist":
          expectation = "toExist()";
          break;
    
        case "toBeFocused":
          expectation = "toBeFocused()";
          break;
    
        case "toHaveText":
          expectation = `toHaveText('${options.expectedValue || ""}')`;
          break;
    
        case "toHaveLabel":
          expectation = `toHaveLabel('${options.expectedValue || ""}')`;
          break;
    
        case "toHaveId":
          expectation = `toHaveId('${options.expectedValue || ""}')`;
          break;
    
        case "toHaveValue":
          expectation = `toHaveValue('${options.expectedValue || ""}')`;
          break;
    
        case "toHaveSliderPosition":
          expectation = `toHaveSliderPosition(${options.expectedValue || 0})`;
          break;
    
        case "toHaveToggleValue":
          expectation = `toHaveToggleValue(${options.expectedValue ?? true})`;
          break;
    
        default:
          expectation = "toBeVisible()";
      }
    
      let code = `await expect(${element})${not}.${expectation};`;
    
      if (options.timeout) {
        code = `await waitFor(${element})${not}.${expectation}.withTimeout(${options.timeout});`;
      }
    
      return code;
    }
  • Registration of the generateExpectationTool in the allTools array for MCP tool serving.
    // Export all tools
    export const allTools: Tool[] = [
      buildTool,
      testTool,
      initTool,
      readConfigTool,
      listConfigurationsTool,
      validateConfigTool,
      createConfigTool,
      listDevicesTool,
      generateTestTool,
      generateMatcherTool,
      generateActionTool,
      generateExpectationTool,
    ];

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gayancliyanage/detox-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server