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
| Name | Required | Description | Default |
|---|---|---|---|
| expectationType | Yes | Type of expectation | |
| elementMatcher | Yes | Matcher code or description | |
| expectedValue | No | Expected value | |
| negated | No | ||
| timeout | No | Custom timeout in ms | |
| visibilityThreshold | No |
Implementation Reference
- src/tools/index.ts:405-426 (handler)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, }; }, };
- src/utils/validators.ts:142-161 (schema)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; }
- src/tools/index.ts:428-442 (registration)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, ];