Skip to content

Commit 0d8e323

Browse files
cleanup
1 parent 494ce1b commit 0d8e323

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

.github/workflows/test-simulators.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ jobs:
9797
AVD_NAME: capacitor-avd-x86_64-35
9898
TEST_PLATFORM: android
9999
TEST_TARGET: emulator-5554
100-
TEST_SERVER_HOST: 10.0.2.2
101100
steps:
102101
- uses: actions/checkout@v6
103102
with:
@@ -136,7 +135,7 @@ jobs:
136135
# This dummy Vitest server URL is only used for the prebuild; the test run
137136
# updates Capacitor with the actual Vitest URL before launching the app.
138137
env:
139-
CAPACITOR_VITEST_SERVER_URL: http://${{ env.TEST_SERVER_HOST }}
138+
CAPACITOR_VITEST_SERVER_URL: http://10.0.2.2
140139
run: |
141140
pnpm exec cap sync android
142141
cd android

packages/capacitor/DEVELOP.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cd ..
1616

1717
The test provider reads these environment variables:
1818

19-
- `TEST_PLATFORM`: Native platform to run. Use `ios` or `android`. Defaults to `ios`.
19+
- `TEST_PLATFORM`: Native platform to run. Use `ios` or `android`.
2020
- `TEST_TARGET`: Simulator/emulator target id passed to `cap run --target`.
2121
- `TEST_SERVER_HOST`: Hostname the native app should use to reach the Vitest server. Android defaults to `10.0.2.2`; iOS uses the Vitest URL host as-is.
2222

@@ -61,8 +61,7 @@ Run the integration tests on Android:
6161
cd packages/capacitor
6262
TEST_PLATFORM=android \
6363
TEST_TARGET=emulator-5554 \
64-
TEST_SERVER_HOST=10.0.2.2 \
6564
pnpm exec vitest run --config vitest.config.ts
6665
```
6766

68-
`TEST_SERVER_HOST=10.0.2.2` lets the Android emulator reach the host machine without `adb reverse`. The Vitest config also binds the test server to `0.0.0.0` for this reason.
67+
Android defaults `TEST_SERVER_HOST` to `10.0.2.2`, which lets the emulator reach the host machine without `adb reverse`. The Vitest config also binds the test server to `0.0.0.0` for this reason.

packages/capacitor/vitest.config.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ const environment = {
3939
* Hostname the app should use to reach the Vitest server. Android emulators use
4040
* 10.0.2.2 to connect to the host machine instead of the emulator loopback.
4141
*/
42-
serverHost: process.env.TEST_SERVER_HOST
42+
serverHost: process.env.TEST_SERVER_HOST ?? (process.env.TEST_PLATFORM == 'android' ? '10.0.2.2' : undefined)
4343
};
4444

45-
if (environment.platform == 'android' && !environment.serverHost) {
46-
throw new Error('Missing TEST_SERVER_HOST. See packages/capacitor/DEVELOP.md for Android integration test setup.');
47-
}
48-
4945
function serverUrlForPlatform(url: string): string {
5046
if (environment.platform != 'android') {
5147
return url;
@@ -79,7 +75,7 @@ class CapacitorBrowserProvider implements BrowserProvider {
7975
console.log(`Opening Capacitor app with Vitest URL: ${serverUrl}`);
8076

8177
// Ensure the target app spawning webviews is up-to-date with the current Vitest server URL.
82-
const buildResult = spawnSync('npx', ['cap', 'sync'], {
78+
const buildResult = spawnSync('npx', ['cap', 'sync', environment.platform], {
8379
stdio: 'inherit',
8480
cwd: EXAMPLE_APP_DIR,
8581
env: {
@@ -88,7 +84,9 @@ class CapacitorBrowserProvider implements BrowserProvider {
8884
}
8985
});
9086
if (buildResult.status !== 0) {
91-
throw new Error(`cap sync failed with exit code ${buildResult.status}`);
87+
throw new Error(
88+
`cap sync failed with ${buildResult.signal ? `signal ${buildResult.signal}` : `exit code ${buildResult.status}`}`
89+
);
9290
}
9391
console.log(`Launching ${environment.platform} Capacitor app on ${environment.target}`);
9492

@@ -105,7 +103,13 @@ class CapacitorBrowserProvider implements BrowserProvider {
105103
// The process to run the Capacitor app will end once the app starts,
106104
// we don't keep track of it, but we do fail if the command failed.
107105
await new Promise<void>((resolve, reject) => {
108-
app.once('exit', () => resolve());
106+
app.once('exit', (code, signal) => {
107+
if (code === 0) {
108+
resolve();
109+
} else {
110+
reject(new Error(`cap run failed with ${signal ? `signal ${signal}` : `exit code ${code}`}`));
111+
}
112+
});
109113
app.once('error', reject);
110114
});
111115

0 commit comments

Comments
 (0)