|
| 1 | +# UI Testing Patterns for VS Code Extensions |
| 2 | + |
| 3 | +This document describes patterns for testing VS Code UI interactions without requiring manual user input. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +VS Code integration tests can pause waiting for user input when commands trigger UI elements like QuickPicks or InputBoxes. To automate these tests, we use mock UI elements with simulation capabilities. |
| 8 | + |
| 9 | +## UI Automation Helpers |
| 10 | + |
| 11 | +The `test-helpers.ts` file provides several UI automation utilities: |
| 12 | + |
| 13 | +### 1. Mock InputBox with Automation |
| 14 | + |
| 15 | +```typescript |
| 16 | +const inputBox = createMockInputBox(); |
| 17 | + |
| 18 | +// Simulate user typing |
| 19 | +inputBox.simulateUserInput("test value"); |
| 20 | + |
| 21 | +// Simulate pressing Enter |
| 22 | +inputBox.simulateAccept(); |
| 23 | + |
| 24 | +// Simulate cancellation |
| 25 | +inputBox.simulateHide(); |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. Mock QuickPick with Automation |
| 29 | + |
| 30 | +```typescript |
| 31 | +const quickPick = createMockQuickPickWithAutomation<vscode.QuickPickItem>(); |
| 32 | + |
| 33 | +// Set items |
| 34 | +quickPick.items = [ |
| 35 | + { label: "Option 1" }, |
| 36 | + { label: "Option 2" } |
| 37 | +]; |
| 38 | + |
| 39 | +// Simulate selecting an item |
| 40 | +quickPick.simulateItemSelection(0); // by index |
| 41 | +// or |
| 42 | +quickPick.simulateItemSelection({ label: "Option 1" }); // by item |
| 43 | + |
| 44 | +// Simulate accepting the selection |
| 45 | +quickPick.simulateAccept(); |
| 46 | +``` |
| 47 | + |
| 48 | +## Integration Test Pattern |
| 49 | + |
| 50 | +Here's the pattern for testing commands that show UI: |
| 51 | + |
| 52 | +```typescript |
| 53 | +test("should handle UI interaction", async () => { |
| 54 | + // 1. Create mock UI elements |
| 55 | + const quickPick = createMockQuickPickWithAutomation(); |
| 56 | + const inputBox = createMockInputBox(); |
| 57 | + |
| 58 | + // 2. Save original VS Code methods |
| 59 | + const originalCreateQuickPick = vscode.window.createQuickPick; |
| 60 | + const originalShowInputBox = vscode.window.showInputBox; |
| 61 | + |
| 62 | + try { |
| 63 | + // 3. Replace VS Code methods with mocks |
| 64 | + (vscode.window as any).createQuickPick = () => quickPick; |
| 65 | + (vscode.window as any).showInputBox = async () => { |
| 66 | + return new Promise((resolve) => { |
| 67 | + setTimeout(() => { |
| 68 | + inputBox.simulateUserInput("user input"); |
| 69 | + inputBox.simulateAccept(); |
| 70 | + resolve("user input"); |
| 71 | + }, 10); |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | + // 4. Start the command |
| 76 | + const commandPromise = vscode.commands.executeCommand("your.command"); |
| 77 | + |
| 78 | + // 5. Wait for UI to initialize |
| 79 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 80 | + |
| 81 | + // 6. Simulate user interactions |
| 82 | + quickPick.items = [{ label: "Option" }]; |
| 83 | + quickPick.simulateItemSelection(0); |
| 84 | + quickPick.simulateAccept(); |
| 85 | + |
| 86 | + // 7. Wait for command completion |
| 87 | + await commandPromise; |
| 88 | + |
| 89 | + // 8. Assert results |
| 90 | + assert.ok(quickPick.show.called, "Quick pick should be shown"); |
| 91 | + } finally { |
| 92 | + // 9. Restore original methods |
| 93 | + (vscode.window as any).createQuickPick = originalCreateQuickPick; |
| 94 | + (vscode.window as any).showInputBox = originalShowInputBox; |
| 95 | + } |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +## Common Patterns |
| 100 | + |
| 101 | +### Testing Login Flow |
| 102 | + |
| 103 | +```typescript |
| 104 | +test("should handle login with URL and token", async () => { |
| 105 | + const quickPick = createMockQuickPickWithAutomation(); |
| 106 | + const inputBox = createMockInputBox(); |
| 107 | + |
| 108 | + // Mock VS Code UI |
| 109 | + (vscode.window as any).createQuickPick = () => quickPick; |
| 110 | + (vscode.window as any).showInputBox = async (options) => { |
| 111 | + // Handle token validation if needed |
| 112 | + if (options.validateInput) { |
| 113 | + const result = await options.validateInput("test-token"); |
| 114 | + if (result) return undefined; // Validation failed |
| 115 | + } |
| 116 | + return "test-token"; |
| 117 | + }; |
| 118 | + |
| 119 | + // Execute login |
| 120 | + const loginPromise = vscode.commands.executeCommand("coder.login"); |
| 121 | + |
| 122 | + // Simulate URL selection |
| 123 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 124 | + quickPick.items = [{ label: "https://coder.example.com" }]; |
| 125 | + quickPick.simulateItemSelection(0); |
| 126 | + quickPick.simulateAccept(); |
| 127 | + |
| 128 | + await loginPromise; |
| 129 | +}); |
| 130 | +``` |
| 131 | + |
| 132 | +### Testing Cancellation |
| 133 | + |
| 134 | +```typescript |
| 135 | +test("should handle user cancellation", async () => { |
| 136 | + const quickPick = createMockQuickPickWithAutomation(); |
| 137 | + |
| 138 | + (vscode.window as any).createQuickPick = () => quickPick; |
| 139 | + |
| 140 | + const commandPromise = vscode.commands.executeCommand("coder.open"); |
| 141 | + |
| 142 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 143 | + |
| 144 | + // Simulate user pressing Escape |
| 145 | + quickPick.simulateHide(); |
| 146 | + |
| 147 | + try { |
| 148 | + await commandPromise; |
| 149 | + } catch (error) { |
| 150 | + // Command should handle cancellation gracefully |
| 151 | + } |
| 152 | +}); |
| 153 | +``` |
| 154 | + |
| 155 | +### Testing Multi-Step Flows |
| 156 | + |
| 157 | +```typescript |
| 158 | +test("should handle multi-step wizard", async () => { |
| 159 | + let step = 0; |
| 160 | + const quickPicks = [ |
| 161 | + createMockQuickPickWithAutomation(), |
| 162 | + createMockQuickPickWithAutomation() |
| 163 | + ]; |
| 164 | + |
| 165 | + (vscode.window as any).createQuickPick = () => { |
| 166 | + return quickPicks[step++]; |
| 167 | + }; |
| 168 | + |
| 169 | + const commandPromise = vscode.commands.executeCommand("coder.wizard"); |
| 170 | + |
| 171 | + // Step 1 |
| 172 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 173 | + quickPicks[0].items = [{ label: "Step 1 Option" }]; |
| 174 | + quickPicks[0].simulateItemSelection(0); |
| 175 | + quickPicks[0].simulateAccept(); |
| 176 | + |
| 177 | + // Step 2 |
| 178 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 179 | + quickPicks[1].items = [{ label: "Step 2 Option" }]; |
| 180 | + quickPicks[1].simulateItemSelection(0); |
| 181 | + quickPicks[1].simulateAccept(); |
| 182 | + |
| 183 | + await commandPromise; |
| 184 | +}); |
| 185 | +``` |
| 186 | + |
| 187 | +## Best Practices |
| 188 | + |
| 189 | +1. **Always restore original methods** - Use try/finally blocks to ensure VS Code methods are restored |
| 190 | +2. **Add delays for UI initialization** - Use `setTimeout` to allow commands to initialize their UI |
| 191 | +3. **Test both success and cancellation paths** - Ensure commands handle user cancellation gracefully |
| 192 | +4. **Mock validation functions** - When testing InputBox validation, mock the validateInput callback |
| 193 | +5. **Use type assertions carefully** - Use `(vscode.window as any)` to bypass TypeScript checks when mocking |
| 194 | + |
| 195 | +## Debugging Tips |
| 196 | + |
| 197 | +1. **Add console.log statements** - Log when UI elements are created and interacted with |
| 198 | +2. **Check mock call counts** - Use `assert.ok(quickPick.show.called)` to verify UI was shown |
| 199 | +3. **Increase timeouts** - If tests are flaky, increase the initialization delay |
| 200 | +4. **Run tests in isolation** - Use `.only` to debug specific tests |
| 201 | + |
| 202 | +## Common Issues |
| 203 | + |
| 204 | +1. **Test hangs waiting for input** - Ensure you're mocking the correct VS Code method |
| 205 | +2. **Mock not being called** - Check that the command uses the expected UI method |
| 206 | +3. **Timing issues** - Adjust delays between command start and UI simulation |
| 207 | +4. **Type errors** - Use type assertions when setting mock methods on vscode.window |
0 commit comments