Skip to content

Commit 59bc26a

Browse files
committed
feat(22): dev server port override
1 parent ada2a05 commit 59bc26a

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ If a `devServerTarget` option is specified, the builder will launch an Angular s
7070
}
7171
```
7272

73+
You can additionaly override the `port` of the dev server. It's handy when you want to run dev server and Playwright tests on different ports.
74+
```json title="angular.json"
75+
"e2e": {
76+
"builder": "playwright-ng-schematics:playwright",
77+
"options": {
78+
"devServerTarget": "my-app:serve",
79+
"port": 0
80+
}
81+
}
82+
```
83+
7384
You still can make use of Playwright's `baseURL` option and mix it with `PLAYWRIGHT_TEST_BASE_URL` env variable.
7485
The example below shows projects using `PLAYWRIGHT_TEST_BASE_URL` (set by `devServerTarget`) or another base URL.
7586

src/builders/playwright/index.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe('Playwright builder', () => {
1919
await architectHost.addBuilderFromPackage(join(__dirname, '../../..'));
2020

2121
// Builder that mocks `ng run app:serve`
22-
const fakeBuilder = (): BuilderOutput => {
23-
return { success: true, baseUrl: 'https://example.com' };
22+
const fakeBuilder = (options: { port: number | null }): BuilderOutput => {
23+
return { success: true, baseUrl: `https://example.com:${options.port}` };
2424
};
2525
architectHost.addBuilder('fakeBuilder', createBuilder(fakeBuilder));
2626
architectHost.addTarget(targetFromTargetString('app:serve'), 'fakeBuilder');
@@ -55,6 +55,7 @@ describe('Playwright builder', () => {
5555
'playwright-ng-schematics:playwright',
5656
{
5757
devServerTarget: 'app:serve',
58+
port: 0,
5859
},
5960
);
6061
await run.stop();
@@ -66,7 +67,7 @@ describe('Playwright builder', () => {
6667
[],
6768
expect.objectContaining({
6869
env: expect.objectContaining({
69-
PLAYWRIGHT_TEST_BASE_URL: 'https://example.com',
70+
PLAYWRIGHT_TEST_BASE_URL: 'https://example.com:0',
7071
}),
7172
}),
7273
);

src/builders/playwright/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function buildArgs(options: JsonObject): string[] {
2626
if (key === 'devServerTarget') {
2727
return [];
2828
}
29+
if (key === 'port') {
30+
return [];
31+
}
2932

3033
// Skip objects, arrays, null, undefined (should already be validated by Angular though)
3134
if (
@@ -55,9 +58,11 @@ function buildArgs(options: JsonObject): string[] {
5558
async function startDevServer(
5659
context: BuilderContext,
5760
devServerTarget: string,
61+
port: number | null,
5862
): Promise<BuilderRun> {
5963
const target = targetFromTargetString(devServerTarget);
60-
const server = await context.scheduleTarget(target, {});
64+
const overrides: JsonObject = port !== null ? { port } : {};
65+
const server = await context.scheduleTarget(target, overrides);
6166

6267
return server;
6368
}
@@ -106,19 +111,26 @@ async function startPlaywrightTest(options: JsonObject, baseURL: string) {
106111
});
107112
}
108113

114+
interface PlaywrightBuilderOptions extends JsonObject {
115+
devServerTarget: string | null;
116+
port: number | null;
117+
files: string[] | null;
118+
}
119+
109120
async function runE2E(
110-
options: JsonObject,
121+
options: PlaywrightBuilderOptions,
111122
context: BuilderContext,
112123
): Promise<BuilderOutput> {
113124
let server: BuilderRun | undefined = undefined;
114125
let baseURL = '';
115126

116127
try {
117-
if (
118-
options.devServerTarget &&
119-
typeof options.devServerTarget === 'string'
120-
) {
121-
server = await startDevServer(context, options.devServerTarget);
128+
if (options.devServerTarget) {
129+
server = await startDevServer(
130+
context,
131+
options.devServerTarget,
132+
options.port,
133+
);
122134
const result = await server.result;
123135
baseURL = result.baseUrl;
124136
}

src/builders/playwright/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"description": "Dev server target to run tests against",
99
"type": "string"
1010
},
11+
"port": {
12+
"description": "Dev server port. Overrides the port defined in devServerTarget.",
13+
"type": "number"
14+
},
1115
"files": {
1216
"description": "Run a test file with the given file name or in the given directory. To specify multiple names, repeat this argument.",
1317
"type": "array",

0 commit comments

Comments
 (0)