Skip to content

Commit 0e05e51

Browse files
ReToCodefilipesilva
authored andcommitted
fix(@angular/cli): Fixed e2e task to respect --publicHost setting as baseUrl for protractor (#6266)
#6173 added two new settings to the serve task. The --publicHost setting is not respected as baseUrl for protractor in the e2e-task. With this fix, if --publicHost is set, it will be used as baseUrl for protrator.
1 parent aabad93 commit 0e05e51

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

packages/@angular/cli/tasks/e2e.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ export const E2eTask = Task.extend({
2626
};
2727

2828
// use serve url as override for protractors baseUrl
29-
if (e2eTaskOptions.serve) {
29+
if (e2eTaskOptions.serve && e2eTaskOptions.publicHost) {
30+
let publicHost = e2eTaskOptions.publicHost;
31+
if (!/^\w+:\/\//.test(publicHost)) {
32+
publicHost = `${e2eTaskOptions.ssl ? 'https' : 'http'}://${publicHost}`;
33+
}
34+
const clientUrl = url.parse(publicHost);
35+
e2eTaskOptions.publicHost = clientUrl.host;
36+
additionalProtractorConfig.baseUrl = url.format(clientUrl);
37+
} else if (e2eTaskOptions.serve) {
3038
additionalProtractorConfig.baseUrl = url.format({
3139
protocol: e2eTaskOptions.ssl ? 'https' : 'http',
3240
hostname: e2eTaskOptions.host,

tests/e2e/tests/misc/live-reload.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import * as os from 'os';
2+
import * as _ from 'lodash';
13
import * as express from 'express';
24
import * as http from 'http';
35

4-
import { appendToFile, writeMultipleFiles } from '../../utils/fs';
6+
import {appendToFile, writeMultipleFiles, writeFile} from '../../utils/fs';
57
import {
68
killAllProcesses,
79
silentExecAndWaitForOutputToMatch,
@@ -18,21 +20,32 @@ export default function () {
1820
const app = express();
1921
const server = http.createServer(app);
2022
let liveReloadCount = 0;
21-
let liveReloadClientCalled = false;
2223
function resetApiVars() {
2324
liveReloadCount = 0;
24-
liveReloadClientCalled = false;
2525
}
2626

2727
server.listen(0);
2828
app.set('port', server.address().port);
29+
30+
const firstLocalIp = _(os.networkInterfaces())
31+
.values()
32+
.flatten()
33+
.filter({ family: 'IPv4', internal: false })
34+
.map('address')
35+
.first();
36+
const publicHost = `${firstLocalIp}:4200`;
37+
2938
const apiUrl = `http://localhost:${server.address().port}`;
3039

3140
// This endpoint will be pinged by the main app on each reload.
3241
app.get('/live-reload-count', _ => liveReloadCount++);
33-
// This endpoint will be pinged by webpack to check for live reloads.
34-
app.get('/sockjs-node/info', _ => liveReloadClientCalled = true);
3542

43+
const proxyConfigFile = 'proxy.config.json';
44+
const proxyConfig = {
45+
'/live-reload-count': {
46+
target: apiUrl
47+
}
48+
};
3649

3750
return Promise.resolve()
3851
.then(_ => writeMultipleFiles({
@@ -122,15 +135,35 @@ export default function () {
122135
.then(_ => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
123136
.then(_ => resetApiVars())
124137
// Serve with live reload client set to api should call api.
138+
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
139+
// Update the component to call the webserver
140+
.then(() => writeFile('./src/app/app.component.ts',
141+
`
142+
import { Component } from '@angular/core';
143+
import { Http } from '@angular/http';
144+
@Component({
145+
selector: 'app-root',
146+
template: '<h1>Live reload test</h1>'
147+
})
148+
export class AppComponent {
149+
constructor(private http: Http) {
150+
http.get('http://${publicHost + '/live-reload-count'}').subscribe(res => null);
151+
}
152+
}`))
125153
.then(_ => silentExecAndWaitForOutputToMatch(
126154
'ng',
127-
['e2e', '--watch', `--public-host=${apiUrl}`],
155+
['e2e', '--watch', '--host=0.0.0.0', '--port=4200', `--public-host=${publicHost}`, '--proxy', proxyConfigFile],
128156
protractorGoodRegEx
129157
))
130158
.then(_ => wait(2000))
159+
.then(_ => appendToFile('src/main.ts', 'console.log(1);'))
160+
.then(_ => waitForAnyProcessOutputToMatch(webpackGoodRegEx, 5000))
161+
.then(_ => wait(2000))
131162
.then(_ => {
132-
if (!liveReloadClientCalled) {
133-
throw new Error(`Expected live-reload client to have been called but it was not.`);
163+
if (liveReloadCount != 2) {
164+
throw new Error(
165+
`Expected API to have been called 2 times but it was called ${liveReloadCount} times.`
166+
);
134167
}
135168
})
136169
.then(_ => killAllProcesses(), (err) => { killAllProcesses(); throw err; })

0 commit comments

Comments
 (0)