feat(config): accept a function for app.command.env (ENG-819)
Builds on #2191 (services), which is merged.
What
app.command.env can be a sync or async function of the config's services:
export default defineConfig({
services: { email },
targets: [{ engine: web(), app: { url: 'http://127.0.0.1:0', command: {
executable: 'node', args: ['app/server.mjs'],
env: ({ services }) => ({ PORT: '{port}', EMAIL_API_URL: services.email.url }),
} } }],
});
- The function gets
{ services }, the same values a test sees.defineConfigtypes them fromconfig.services. - It's called one time per run, after the services start and before any app command starts. It's still called when a running process is reused with
reuseExisting, but that process doesn't get the variables and a notice is printed. Targets that share a function share one call. {port}expands in the returned values. Every value must be a string.undefinedvalues are type errors, and they're dropped at run time (a JavaScript config can return them), the same as in anenvobject.- If the function throws, or doesn't return an object of strings, the run fails with
INVALID_CONFIG(exit 2). A config or infrastructure error it throws, likeAPP_UNREACHABLE, keeps its code. If it doesn't return within the command'sstartupTimeout, the run fails withAPP_UNREACHABLE. - Commands that only read the config, such as
e2e list, never call it.
Why
With an env object, the app can only get a service's address if it's fixed, like http://127.0.0.1:4010. With a function, a service can pick a free port or start a container and pass its address to the app.
How it works
CommandConfig,TargetApp,Target,ProjectConfig, andE2EConfigtake aServicestype parameter forenv. The function type is declared as a method so adefineConfigconfig stays assignable toE2EConfig(packages/core/src/types.ts:1554, with a note saying why).defineConfigkeepsConfigas its first type parameter and infersServices, its second, fromconfig.services, sodefineConfig<MyConfig>(…)still compiles (packages/e2e/src/config/define.ts:16).normalizeCommandkeeps a functionenvas it is (packages/core/src/config/command.ts:109), anddigestCommandleaves it out of the digest, so a worker that imports the config again gets the same digest (command.ts:173).- The runner passes the copied service values to
startDeclaredProcessesas{ services }, beside the optional processpool(packages/e2e/src/run/runner.ts:1172). When a target has anenvfunction,commandEnvscalls each distinct function once withinstartupTimeoutand checks the result withreturnedEnv(packages/core/src/run/provision.ts:307,:412,packages/core/src/config/app.ts:278). It checks the signal before each call (provision.ts:424). Each call goes throughstartEnvCall(provision.ts:383): a config or infrastructure error keeps its code, and anything else becomesINVALID_CONFIG. That includes a test error, like a service call passed a value it can't copy, and a service call's error, which comes back without its code. declaredCommandsputs the returned variables in the command before it builds the process key, so targets with the same command and variables still share one app process (packages/core/src/run/declared-processes.ts:39,SpawnedCommandatcommand.ts:23).startDeclaredProcesseswraps thereadyhook of each app process. When the process was reused and one of its targets has anenvfunction, a notice is printed before the ready line (packages/core/src/run/provision.ts:329).- Targets that share one
envfunction share one call. The call records when it started and when it settled, so each target is held to its ownstartupTimeoutfrom the start, in any target order (packages/core/src/run/provision.ts:462). Theenvcall and the ready probe each getstartupTimeout, with one default,DEFAULT_STARTUP_TIMEOUT_MS(packages/core/src/run/managed-process.ts:15). returnedEnvrequires a plain object, so returning aMapfails withINVALID_CONFIGinstead of starting the app without any variables (packages/core/src/config/app.ts:279). For a returned string, the error saysgot a stringinstead of quoting it, since a connection URL with a password is a likely mistake there (app.ts:280).- In
e2e mcp, theopen_sessionsummary showsenv from a functionfor a command whoseenvis a function, because the variable names aren't known there (packages/e2e/src/mcp/app-lines.ts:40).
Tests cover each of these: a kept error code, a test error that becomes INVALID_CONFIG, a returned Map or string, both target orders for a shared function, and the reuseExisting notice, printed once for a reused process and never when the runner starts the app.
The env reference, the error rows, the services guide, and the skill pages now pass service addresses through an env function. The docs examples no longer name a port: the email service listens on port 0 and yields the address it got, and the emulate example uses port: 0.
Not covered yet
Two limits are out of scope for this PR:
- With
reuseExisting, the runner can't tell whether the running process got the variables, so it prints a notice and carries on. ENG-1540 tracks recording a hash of the app command when the runner starts it, and failing when a reused process doesn't match. - Service values reach the app only through
app.command.env. Mobile and desktop apps take settings as launch arguments rather than environment variables, and apps started outside e2e don't haveapp.command. A possible addition is alaunchArgumentsfunction on the mobile target'sapp, like theenvfunction.
Verified
Ran it locally: yes
The built CLI at this PR's head (a9db39d) ran the docs example, whose env function reads services.email.url:
$ e2e run
✓ service "email" ready 2ms
✓ target "web" command at http://127.0.0.1:54374/ ready 90ms
✓ |web| tests/signup.e2e.ts > signing up sends one welcome email 386ms
Tests 1 passed (1)
The new notice, from a config with reuseExisting: true run against an app started by hand on port 3456:
$ e2e run --config e2e.reuse.config.ts
✓ service "email" ready 3ms
ℹ target "web" command: reusing the process already serving http://127.0.0.1:3456/
ℹ target "web" command: the process at http://127.0.0.1:3456/ didn't get the variables from the target's env function. Restart it with them, or give fixed ports to the services it uses
✓ target "web" command at http://127.0.0.1:3456/ reused 8ms
Recording of the same e2e run --config e2e.reuse.config.ts run as above, against an app started by hand on port 3456. It shows the two notices, then the test fails as expected because the app wasn't given the email service's URL.
Benchmark: pnpm bench:ab at the top of the stack (#2259, which includes this PR) against main, on two web benchmark files:
bench-ab: c3109278 (merge base with origin/main) vs b6f27edd
apps/web-benchmark: e2e run --workers 1 tests/control-inventory.e2e.ts tests/cart-totals.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 24.22.1, load 4.8.
Behavior
Every test ran the same steps with the same outcome on every run of both builds.
Timings
| Metric | base median (IQR) | head median (IQR) | change (95% CI) | verdict |
|---|---|---|---|---|
| wall (process) | 4.53 s (4.48 s-4.54 s) | 4.50 s (4.41 s-4.61 s) | -0.7% (-2.6% to +3.3%) | unresolved |
| suite (sum of attempts) | 3.69 s (3.67 s-3.72 s) | 3.71 s (3.66 s-3.75 s) | -1.0% (-2.2% to +2.3%) | same |
| action (events) | 2.54 s (2.54 s-2.58 s) | 2.55 s (2.54 s-2.57 s) | -0.1% (-2.2% to +0.9%) | same, under the floor |
| step locator.longPress | 1.51 s (1.51 s-1.52 s) | 1.51 s (1.50 s-1.51 s) | -0.2% (-1.1% to +0.4%) | same, under the floor |
| step app.open | 740 ms (736 ms-743 ms) | 746 ms (726 ms-773 ms) | -1.0% (-3.5% to +5.4%) | same, under the floor |
| step locator.tap | 457 ms (447 ms-468 ms) | 470 ms (448 ms-479 ms) | +0.5% (-5.3% to +5.6%) | same, under the floor |
| step locator.check | 286 ms (276 ms-298 ms) | 296 ms (281 ms-297 ms) | -0.2% (-4.6% to +7.9%) | same, under the floor |
| step expect.toHaveText | 132 ms (129 ms-135 ms) | 130 ms (127 ms-134 ms) | +1.1% (-6.6% to +4.3%) | same, under the floor |
| step locator.scrollIntoView | 71 ms (69 ms-72 ms) | 72 ms (68 ms-72 ms) | +0.0% (-7.1% to +7.3%) | same, under the floor |
| step locator.secondaryTap | 70 ms (61 ms-79 ms) | 64 ms (62 ms-73 ms) | -8.5% (-21.0% to +25.2%) | same, under the floor |
| step locator.setInputFiles | 65 ms (64 ms-67 ms) | 63 ms (61 ms-65 ms) | -3.2% (-6.2% to -0.7%) | same, under the floor |
| step locator.doubleTap | 62 ms (61 ms-62 ms) | 63 ms (61 ms-63 ms) | +0.8% (-16.7% to +3.3%) | same, under the floor |
| step locator.uncheck | 38 ms (30 ms-45 ms) | 33 ms (32 ms-33 ms) | -14.3% (-29.7% to +31.3%) | same, under the floor |
| step app.back | 15 ms (14 ms-16 ms) | 14 ms (14 ms-15 ms) | -6.5% (-12.3% to +10.2%) | same, under the floor |
| step expect.toBeVisible | 3 ms (2 ms-3 ms) | 2 ms (2 ms-2 ms) | -33.3% (-42.3% to +41.4%) | same, under the floor |
| step expect.toBeChecked | 2 ms (2 ms-2 ms) | 3 ms (2 ms-3 ms) | 0.0% (0.0% to +73.2%) | same |
| step expect.toHaveURL | 1 ms (0 ms-1 ms) | 0 ms (0 ms-1 ms) | 0.0% (0.0% to 0.0%) | same |
Counters
Every counter matched on every run of both builds.
Tests outside the threshold: 0 of 95
With 95 tests compared at 95%, about 4.8 could land outside by chance.