Skip to content

Commit 752fdf0

Browse files
authored
fix(playwright) :: let the kernel choose the ports
Before this was pinned to `8080` and `8081` which conflicted with any other program alerady on those ports. Now each server binds to `127.0.0.1:0` and Playright's `webServer.wait` gets the port. `reuseExistingServer` goes with the fixed ports, since there is no longer a known address for a stray server to answer on. The timeout rises to ten minutes because Playwright now starts the server itself, so that window has to cover a cold `cargo run`.
1 parent 22c17ae commit 752fdf0

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,16 @@ tests should render real components through their SQL fixture; do not inject com
112112
invoke SQLPage's JavaScript initialization functions directly. Parameterized fixtures may accept
113113
request variables when several tests need the same component with different data.
114114

115-
#### Start a sqlpage instance pointed to the official site source code
116-
117-
```bash
118-
cd examples/official-site
119-
cargo run
120-
```
121-
122115
#### Run the tests
123116

124-
In a separate terminal, run the tests:
125-
126117
```bash
127118
npm install
128119
cd tests/end-to-end
129120
npx playwright install chromium
130121
npm run test
131122
```
132123

133-
Playwright starts the component fixture server on port 8081 automatically. The official-site
134-
server on port 8080 must still be started separately as shown above.
124+
Playwright starts both servers itself on a free port. Set `SQLPAGE_BINARY` to run the servers from an already compiled binary instead of `cargo run`.
135125

136126
## Documentation
137127

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"database_url": "sqlite::memory:",
3-
"port": 8081
2+
"database_url": "sqlite::memory:"
43
}
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import { defineConfig, devices } from "@playwright/test";
22

3-
const fixtureBaseURL =
4-
process.env.SQLPAGE_FIXTURE_BASE ?? "http://127.0.0.1:8081";
53
const sqlpage =
64
process.env.SQLPAGE_BINARY ?? "cargo run --manifest-path ../../Cargo.toml --";
75

6+
const anyFreePort = "127.0.0.1:0";
7+
const compileAndStart = 600_000;
8+
9+
// Playwright uppercases the named capture group of `wait` into the environment
10+
// it hands the worker processes, which is where the tests read the address back
11+
// from: https://playwright.dev/docs/api/class-testconfig#test-config-web-server
12+
function announcedAddress(variable: string) {
13+
return {
14+
announcement: new RegExp(
15+
`View your website at:.*?http://(?<${variable.toLowerCase()}>\\S+)`,
16+
),
17+
url: `http://${process.env[variable]}`,
18+
};
19+
}
20+
21+
const officialSite = announcedAddress("SQLPAGE_OFFICIAL_SITE_ADDRESS");
22+
const fixtures = announcedAddress("SQLPAGE_FIXTURES_ADDRESS");
23+
824
export default defineConfig({
925
testDir: ".",
1026
fullyParallel: true,
@@ -19,30 +35,29 @@ export default defineConfig({
1935
{
2036
name: "official-site",
2137
testMatch: "*.spec.ts",
22-
use: {
23-
...devices["Desktop Chrome"],
24-
baseURL: process.env.SQLPAGE_TEST_BASE ?? "http://127.0.0.1:8080",
25-
},
38+
use: { ...devices["Desktop Chrome"], baseURL: officialSite.url },
2639
},
2740
{
2841
name: "fixtures",
2942
testMatch: "fixtures/**/test.ts",
30-
use: { ...devices["Desktop Chrome"], baseURL: fixtureBaseURL },
43+
use: { ...devices["Desktop Chrome"], baseURL: fixtures.url },
3144
},
3245
],
3346
webServer: [
3447
{
48+
name: "official site",
3549
command: sqlpage,
3650
cwd: "../../examples/official-site",
37-
url: "http://127.0.0.1:8080",
38-
reuseExistingServer: !process.env.CI,
39-
timeout: 120_000,
51+
env: { SQLPAGE_LISTEN_ON: anyFreePort },
52+
wait: { stderr: officialSite.announcement },
53+
timeout: compileAndStart,
4054
},
4155
{
56+
name: "fixtures",
4257
command: `${sqlpage} --web-root fixtures --config-dir fixture-server`,
43-
url: fixtureBaseURL,
44-
reuseExistingServer: !process.env.CI,
45-
timeout: 120_000,
58+
env: { SQLPAGE_LISTEN_ON: anyFreePort },
59+
wait: { stderr: fixtures.announcement },
60+
timeout: compileAndStart,
4661
},
4762
],
4863
});

0 commit comments

Comments
 (0)