{"slug":"capacitor-testing","title":"capacitor-testing","summary":"Testing guide for Capacitor apps and plugins. Covers web-layer unit and component tests with Vitest and mocked Capacitor plugins (`vi.mock('@capacitor/core')`, `registerPlugin`), Playwright for the web build, Appium E2E with NATIVE_APP / WEBVIEW context switching, native plugin u","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-05T18:17:13.902605Z","repo":{"url":"https://github.com/Cap-go/capgo-skills","stars":79,"forks":4,"license":"MIT","updatedAt":"2026-12-05T16:44:30Z"},"bodyHtml":"<hr>\n<h2>name: capacitor-testing\ndescription: Testing guide for Capacitor apps and plugins. Covers web-layer unit and component tests with Vitest and mocked Capacitor plugins (<code>vi.mock('@capacitor/core')</code>, <code>registerPlugin</code>), Playwright for the web build, Appium E2E with NATIVE_APP / WEBVIEW context switching, native plugin unit tests with Swift Testing vs XCTest (what can migrate, <code>CAPPluginCall</code> patterns, <code>xcodebuild test</code> on a simulator) and Android JUnit, plus an agent-driven verification loop on simulators and devices (build, install, launch, screenshot, UI hierarchy, tap, logs) with <code>xcrun simctl</code>, <code>xcrun devicectl</code>, <code>adb</code>, Xcode MCP device tools, and live reload via <code>npx cap run --url</code> (Capacitor 9) or <code>-l --host --port</code> (Capacitor 8). Use when adding tests, mocking plugins, \"does it work on device\", verifying a UI change on a simulator, migrating XCTest to Swift Testing, or setting up test CI. Do not use for crash log capture (ios-android-logs), WebView debugging sessions (debugging-capacitor), or build pipelines (capacitor-ci-cd).</h2>\n<h1>Testing Capacitor Apps and Plugins</h1>\n<p>A Capacitor app has three layers to test: the web app (most logic), the native bridge and plugins (Swift/Kotlin), and the running app on a device. Pick the cheapest layer that can catch the bug.</p>\n<h2>When to Use</h2>\n<p>TRIGGER when:</p>\n<ul>\n<li>Adding unit, component or E2E tests to a Capacitor app</li>\n<li>Mocking Capacitor or plugin APIs in Vitest / Jest</li>\n<li>Writing or modernizing native unit tests for a plugin (Swift Testing, XCTest, JUnit)</li>\n<li>Verifying a change on a simulator, emulator or device (\"test this\", \"does it work\", \"check on device\")</li>\n<li>E2E tests that cannot find web elements in the app (WebView context)</li>\n<li>Setting up test jobs in CI</li>\n</ul>\n<p>Do not use:</p>\n<ul>\n<li>Reading device or crash logs: <code>ios-android-logs</code></li>\n<li>Safari Web Inspector / Chrome DevTools debugging: <code>debugging-capacitor</code></li>\n<li>Build, signing and release pipelines: <code>capacitor-ci-cd</code></li>\n<li>App Intents tests: <code>capacitor-app-intents</code> (AppIntentsTesting)</li>\n</ul>\n<h2>Choose the layer</h2>\n<table>\n<thead>\n<tr>\n<th>What changed</th>\n<th>Test with</th>\n<th>Reference</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Business logic, state, services that call plugins</td>\n<td>Vitest with mocked plugins</td>\n<td><a href=\"references/web-unit-tests.md\">references/web-unit-tests.md</a></td>\n</tr>\n<tr>\n<td>Components (React, Vue, Angular, Svelte)</td>\n<td>Testing Library on Vitest</td>\n<td><a href=\"references/web-unit-tests.md\">references/web-unit-tests.md</a></td>\n</tr>\n<tr>\n<td>Web flows (routing, forms)</td>\n<td>Playwright against the dev server or <code>dist</code></td>\n<td><a href=\"references/e2e.md\">references/e2e.md</a></td>\n</tr>\n<tr>\n<td>Plugin native code (Swift / Kotlin)</td>\n<td>Swift Testing or XCTest; JUnit</td>\n<td><a href=\"references/native-plugin-tests.md\">references/native-plugin-tests.md</a></td>\n</tr>\n<tr>\n<td>Full app on device, native UI, permissions, deep links</td>\n<td>Agent verification loop; Appium or Maestro for repeatable suites</td>\n<td><a href=\"references/device-verification-loop.md\">references/device-verification-loop.md</a>, <a href=\"references/e2e.md\">references/e2e.md</a></td>\n</tr>\n<tr>\n<td>CI jobs</td>\n<td>GitHub Actions matrix</td>\n<td><a href=\"references/ci.md\">references/ci.md</a></td>\n</tr>\n</tbody>\n</table>\n<p>Only load a reference when its layer is in play.</p>\n<h2>Facts that save time</h2>\n<ul>\n<li><strong>Plugins in jsdom run their web implementation</strong>, or throw <code>unimplemented</code> when there is none. <code>Capacitor.isNativePlatform()</code> returns <code>false</code> in tests unless you mock <code>@capacitor/core</code>. Mock at the plugin package boundary, not deep inside the app.</li>\n<li><strong>E2E tools see the WebView as one native element.</strong> Appium must switch to the <code>WEBVIEW_*</code> context to find DOM elements. Capacitor makes the WebView inspectable in debug builds; for release builds set <code>ios.webContentsDebuggingEnabled</code> / <code>android.webContentsDebuggingEnabled</code> in <code>capacitor.config</code>. Never ship release builds with it on by accident.</li>\n<li><strong>Detox is not an option.</strong> It targets React Native and does not drive Capacitor WebView content.</li>\n<li><strong>Swift Testing cannot replace all XCTest.</strong> UI tests (XCUIApplication) and <code>measure {}</code> performance tests stay XCTest. Unit tests can migrate one class at a time; a file can hold both.</li>\n<li><strong>iOS-only plugin packages cannot use <code>swift test</code></strong> because they depend on UIKit and Capacitor. Run <code>xcodebuild test -scheme &lt;Package&gt; -destination 'platform=iOS Simulator,...'</code>.</li>\n<li><strong>Capacitor 9 removed the deprecated Swift APIs.</strong> Tests that build calls with the old <code>CAPPluginCall(callbackId:options:success:error:)</code> initializer or <code>CAPPlugin(bridge:pluginId:pluginName:)</code> must move to <code>CAPPluginCall(callbackId:methodName:options:success:error:)</code> and a plain <code>init()</code>.</li>\n<li><strong>Live reload flags changed in Capacitor 9.</strong> Capacitor 9: <code>npx cap run ios --url http://&lt;lan-ip&gt;:5173</code>. Capacitor 8: <code>npx cap run ios -l --host &lt;lan-ip&gt; --port 5173</code>. The Ionic CLI's <code>ionic cap run ios -l --external</code> is a different tool.</li>\n</ul>\n<h2>Workflow</h2>\n<ol>\n<li><strong>Inspect.</strong> Read <code>package.json</code> (test runner, framework, <code>@capacitor/*</code> versions), <code>capacitor.config.*</code>, existing test config (<code>vitest.config.*</code>, <code>playwright.config.*</code>, <code>wdio.conf.*</code>, <code>.maestro/</code>), and for plugins <code>Package.swift</code>, the podspec, <code>ios/Tests/</code>, <code>android/src/test/</code>.</li>\n<li><strong>Ask</strong> which layer the user wants if the request is general. Suggest the cheapest one that covers the risk.</li>\n<li><strong>Write tests</strong> using the matching reference. Follow the project's existing runner and style; do not add a second runner.</li>\n<li><strong>Run them</strong> and show the output. A test that was never run is not done.</li>\n<li><strong>For UI-affecting changes, run the device verification loop</strong> (<a href=\"references/device-verification-loop.md\">references/device-verification-loop.md</a>) and report screenshots and findings.</li>\n</ol>\n<h2>Verify</h2>\n<pre><code># Web layer\nnpx vitest run\nnpx playwright test\n\n# Plugin, iOS (from the plugin repo)\nxcodebuild test -scheme &lt;PluginPackageName&gt; \\\n  -destination \"platform=iOS Simulator,name=$(xcrun simctl list devices available | grep -m1 -o 'iPhone [^(]*' | sed 's/ *$//')\"\n\n# Plugin, Android (from the plugin repo)\ncd android &amp;&amp; ./gradlew test\n\n# App native tests, if the app has a test target\nxcodebuild test -project ios/App/App.xcodeproj -scheme App -destination 'platform=iOS Simulator,name=&lt;device&gt;'\n# CocoaPods projects: -workspace ios/App/App.xcworkspace\n</code></pre>\n<p>Report: tests added, command run, pass/fail counts, and anything that could not run locally (no simulator, no device, missing Android SDK).</p>\n<h2>Error Handling</h2>\n<table>\n<thead>\n<tr>\n<th>Symptom</th>\n<th>Cause</th>\n<th>Fix</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>\"&lt;Plugin&gt;\" plugin is not implemented on web</code> in Vitest</td>\n<td>Real plugin running in jsdom</td>\n<td><code>vi.mock('&lt;plugin package&gt;')</code> in the setup file</td>\n</tr>\n<tr>\n<td>Mock ignored, real plugin still called</td>\n<td>Mock path differs from the import path, or the mock is registered after import</td>\n<td>Mock the exact specifier the app imports. Put <code>vi.mock</code> in <code>setupFiles</code> or at the top of the test file</td>\n</tr>\n<tr>\n<td>Appium cannot find <code>data-testid</code> elements</td>\n<td>Still in the <code>NATIVE_APP</code> context</td>\n<td><code>await driver.switchContext(webviewContext)</code> after waiting for <code>getContexts()</code> to list a <code>WEBVIEW_*</code> entry</td>\n</tr>\n<tr>\n<td>No <code>WEBVIEW_*</code> context</td>\n<td>WebView not inspectable (release build)</td>\n<td>Use a debug build, or enable <code>webContentsDebuggingEnabled</code> for the test build only</td>\n</tr>\n<tr>\n<td>Android WebView context fails with a chromedriver version error</td>\n<td>Chromedriver does not match the device WebView</td>\n<td>Enable Appium's chromedriver autodownload (an insecure feature flag) or pin a matching chromedriver; check the Appium UiAutomator2 docs</td>\n</tr>\n<tr>\n<td><code>No such module 'Testing'</code></td>\n<td>Old Xcode</td>\n<td>Swift Testing needs the Xcode 16+ toolchain. Capacitor 9 needs Xcode 27 anyway</td>\n</tr>\n<tr>\n<td><code>error: no such module 'UIKit'</code> from <code>swift test</code></td>\n<td>macOS host build of an iOS package</td>\n<td>Use <code>xcodebuild test</code> with an iOS Simulator destination</td>\n</tr>\n<tr>\n<td><code>'init(callbackId:options:success:error:)' is deprecated</code> / unavailable</td>\n<td>Old test helper</td>\n<td>Add <code>methodName:</code></td>\n</tr>\n<tr>\n<td>Live reload: <code>error: unknown option '-l'</code></td>\n<td>Capacitor 9 CLI</td>\n<td>Use <code>--url http://&lt;lan-ip&gt;:&lt;port&gt;</code></td>\n</tr>\n<tr>\n<td>Live reload app shows a blank page</td>\n<td>Dev server bound to localhost, or wrong IP</td>\n<td>Bind to <code>0.0.0.0</code> (Vite: <code>--host</code>), use the LAN IP, same Wi-Fi</td>\n</tr>\n</tbody>\n</table>\n<h2>Related Skills</h2>\n<ul>\n<li><code>debugging-capacitor</code>: inspect the WebView and native debuggers</li>\n<li><code>ios-android-logs</code>: stream logs during device runs</li>\n<li><code>capacitor-ci-cd</code>: build and release pipelines</li>\n<li><code>capacitor-ios-resizability</code>: what to check in resized windows during device runs</li>\n<li><code>capacitor-plugin-upgrade-v8-to-v9</code>: plugin API removals that affect test helpers</li>\n</ul>\n","files":[{"path":"metadata.json","sizeBytes":1476,"isText":true},{"path":"references/ci.md","sizeBytes":1979,"isText":true},{"path":"references/device-verification-loop.md","sizeBytes":5582,"isText":true},{"path":"references/e2e.md","sizeBytes":4178,"isText":true},{"path":"references/native-plugin-tests.md","sizeBytes":6429,"isText":true},{"path":"references/web-unit-tests.md","sizeBytes":4033,"isText":true},{"path":"SKILL.md","sizeBytes":8142,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-05T18:18:11.707738Z","sha256":"D1F4C83BA6B793B01FC17591BD675C4AFC693A57C56E319CD770AB14958871A5","sizeBytes":14787},"review":null,"source":{"repositoryUrl":"https://github.com/Cap-go/capgo-skills","path":"plugins/capacitor-quality/skills/capacitor-testing","license":"MIT","commit":"76462e3a0041ee8dd0d370c344d9fd3ae2c29cdb","subtreeSha":"02BA650783CB2531FF711B2104E5065C0FECE26AE8BEFCBE8D57F4DA1BA6BF35","lastSyncedAt":"2026-12-05T18:17:06.651351Z"},"reviewedAt":"2026-12-05T18:19:35.365874Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/Cap-go/capgo-skills/tree/main/plugins/capacitor-quality/skills/capacitor-testing"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install cap-go-capgo-skills@llmmart"},{"target":"git","command":"git clone https://github.com/Cap-go/capgo-skills.git"}]}