Skip to content

Commit f70557a

Browse files
alan-agius4dgp1130
authored andcommitted
test: use tree-kill callback
tree-kill kills process asynchronously, but previously we didn't wait for the processes to be terminated before we continue running other tests. With this change we use the tree-kill callback method to wait for the processes to be killed before we continue with out tests.
1 parent ae02b21 commit f70557a

17 files changed

+84
-188
lines changed

tests/legacy-cli/e2e/tests/basic/e2e.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ export default function () {
6666
ng('e2e', 'test-project', '--no-webdriver-update', '--dev-server-target='),
6767
),
6868
)
69-
.then(
70-
() => killAllProcesses(),
71-
(err) => {
72-
killAllProcesses();
73-
throw err;
74-
},
75-
)
69+
.finally(() => killAllProcesses())
7670
);
7771
}

tests/legacy-cli/e2e/tests/basic/rebuild.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,6 @@ export default function () {
181181
throw new Error('Expected component CSS to update.');
182182
}
183183
})
184-
.then(
185-
() => killAllProcesses(),
186-
(err: unknown) => {
187-
killAllProcesses();
188-
throw err;
189-
},
190-
)
184+
.finally(() => killAllProcesses())
191185
);
192186
}

tests/legacy-cli/e2e/tests/basic/serve.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export default async function () {
77
// Serve works without HMR
88
await ngServe('--no-hmr');
99
await verifyResponse();
10-
killAllProcesses();
10+
await killAllProcesses();
1111

1212
// Serve works with HMR
1313
await ngServe('--hmr');
1414
await verifyResponse();
1515
} finally {
16-
killAllProcesses();
16+
await killAllProcesses();
1717
}
1818
}
1919

tests/legacy-cli/e2e/tests/build/poll.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export default async function () {
2424
// But a rebuild should happen roughly within the 10 second window.
2525
await waitForAnyProcessOutputToMatch(webpackGoodRegEx, 7000);
2626
} finally {
27-
killAllProcesses();
27+
await killAllProcesses();
2828
}
2929
}

tests/legacy-cli/e2e/tests/build/rebuild-deps-type-check.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ export default function () {
114114
throw new Error('Expected no error but an error was shown.');
115115
}
116116
})
117-
.then(
118-
() => killAllProcesses(),
119-
(err: any) => {
120-
killAllProcesses();
121-
throw err;
122-
},
123-
)
117+
.finally(() => killAllProcesses())
124118
);
125119
}

tests/legacy-cli/e2e/tests/build/rebuild-replacements.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default async function () {
1313
return;
1414
}
1515

16-
let error;
1716
try {
1817
await execAndWaitForOutputToMatch(
1918
'ng',
@@ -26,12 +25,7 @@ export default async function () {
2625
// Should trigger a rebuild.
2726
await appendToFile('src/environments/environment.prod.ts', `console.log('PROD');`);
2827
await waitForAnyProcessOutputToMatch(webpackGoodRegEx, 45000);
29-
} catch (e) {
30-
error = e;
31-
}
32-
33-
killAllProcesses();
34-
if (error) {
35-
throw error;
28+
} finally {
29+
await killAllProcesses();
3630
}
3731
}

tests/legacy-cli/e2e/tests/build/rebuild-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export default async function () {
2626
writeFile('src/app/type.ts', `export type MyType = string;`),
2727
]);
2828
} finally {
29-
killAllProcesses();
29+
await killAllProcesses();
3030
}
3131
}

tests/legacy-cli/e2e/tests/commands/serve/reload-shims.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export default async function () {
2020
/Module not found: Error: Can't resolve 'path'/,
2121
);
2222
} finally {
23-
killAllProcesses();
23+
await killAllProcesses();
2424
}
2525
}

tests/legacy-cli/e2e/tests/commands/serve/serve-path.ts

+1-16
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,5 @@ export default function () {
1818
assert.strictEqual(response.status, 200);
1919
assert.match(await response.text(), /<app-root><\/app-root>/);
2020
})
21-
.then(
22-
() => killAllProcesses(),
23-
(err) => {
24-
killAllProcesses();
25-
throw err;
26-
},
27-
);
28-
// .then(() => ngServe('--base-href', 'test/'))
29-
// .then((response) => response.text())
30-
// .then(() => fetch('http://localhost:4200/test', { headers: { 'Accept': 'text/html' } }))
31-
// .then(body => {
32-
// if (!body.match(/<app-root><\/app-root>/)) {
33-
// throw new Error('Response does not match expected value.');
34-
// }
35-
// })
36-
// .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; });
21+
.finally(() => killAllProcesses());
3722
}

tests/legacy-cli/e2e/tests/misc/ask-missing-builder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default async function () {
1414
/Would you like to add a package with "deploy" capabilities/,
1515
);
1616

17-
killAllProcesses();
17+
await killAllProcesses();
1818

1919
// Execute a command with TTY force enabled
2020
execWithEnv('ng', ['lint'], {
@@ -26,6 +26,6 @@ export default async function () {
2626
// Check if the prompt is shown
2727
await waitForAnyProcessOutputToMatch(/Would you like to add ESLint now/);
2828
} finally {
29-
killAllProcesses();
29+
await killAllProcesses();
3030
}
3131
}

tests/legacy-cli/e2e/tests/misc/fallback.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ export default function () {
3838
assert.strictEqual(response.status, 200);
3939
assert.match(await response.text(), /<app-root><\/app-root>/);
4040
})
41-
.then(
42-
() => killAllProcesses(),
43-
(err) => {
44-
killAllProcesses();
45-
throw err;
46-
},
47-
)
41+
.finally(() => killAllProcesses())
4842
);
4943
}

tests/legacy-cli/e2e/tests/misc/proxy-config.ts

+12-49
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,16 @@ export default function () {
3333
},
3434
};
3535

36-
return (
37-
Promise.resolve()
38-
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
39-
.then(() => ngServe('--proxy-config', proxyConfigFile))
40-
.then(() => fetch('http://localhost:4200/api/test'))
41-
.then(async (response) => {
42-
assert.strictEqual(response.status, 200);
43-
assert.match(await response.text(), /TEST_API_RETURN/);
44-
})
45-
.then(
46-
() => killAllProcesses(),
47-
(err) => {
48-
killAllProcesses();
49-
throw err;
50-
},
51-
)
52-
53-
// .then(() => updateJsonFile('angular.json', configJson => {
54-
// const app = configJson.defaults;
55-
// app.serve = {
56-
// proxyConfig: proxyConfigFile
57-
// };
58-
// }))
59-
// .then(() => ngServe())
60-
// .then(() => fetch('http://localhost:4200/api/test'))
61-
// .then(async (response) => {
62-
// assert.strictEqual(response.status, 200);
63-
// assert.match(await response.text(), /TEST_API_RETURN/)
64-
// })
65-
// .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
66-
67-
.then(
68-
() => server.close(),
69-
(err) => {
70-
server.close();
71-
throw err;
72-
},
73-
)
74-
);
75-
76-
// // A non-existing proxy file should error.
77-
// .then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json')))
78-
// .then(() => updateJsonFile('angular.json', configJson => {
79-
// const app = configJson.defaults;
80-
// app.serve = {
81-
// proxyConfig: 'proxy.non-existent.json'
82-
// };
83-
// }))
84-
// .then(() => expectToFail(() => ng('serve')));
36+
return Promise.resolve()
37+
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
38+
.then(() => ngServe('--proxy-config', proxyConfigFile))
39+
.then(() => fetch('http://localhost:4200/api/test'))
40+
.then(async (response) => {
41+
assert.strictEqual(response.status, 200);
42+
assert.match(await response.text(), /TEST_API_RETURN/);
43+
})
44+
.finally(async () => {
45+
await killAllProcesses();
46+
server.close();
47+
});
8548
}

tests/legacy-cli/e2e/tests/misc/public-host.ts

+38-74
Original file line numberDiff line numberDiff line change
@@ -14,78 +14,42 @@ export default function () {
1414
const publicHost = `${firstLocalIp}:4200`;
1515
const localAddress = `http://${publicHost}`;
1616

17-
return (
18-
Promise.resolve()
19-
// Disabling this test. Webpack Dev Server does not check the hots anymore when binding to
20-
// numeric IP addresses.
21-
// .then(() => ngServe('--host=0.0.0.0'))
22-
// .then(() => fetch(localAddress))
23-
// .then((response) => response.text())
24-
// .then(body => {
25-
// if (!body.match(/Invalid Host header/)) {
26-
// throw new Error('Response does not match expected value.');
27-
// }
28-
// })
29-
// .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
30-
.then(() => ngServe('--host=0.0.0.0', `--public-host=${publicHost}`))
31-
.then(() => fetch(localAddress))
32-
.then((response) => response.text())
33-
.then((body) => {
34-
if (!body.match(/<app-root><\/app-root>/)) {
35-
throw new Error('Response does not match expected value.');
36-
}
37-
})
38-
.then(
39-
() => killAllProcesses(),
40-
(err) => {
41-
killAllProcesses();
42-
throw err;
43-
},
44-
)
45-
.then(() => ngServe('--host=0.0.0.0', `--disable-host-check`))
46-
.then(() => fetch(localAddress))
47-
.then((response) => response.text())
48-
.then((body) => {
49-
if (!body.match(/<app-root><\/app-root>/)) {
50-
throw new Error('Response does not match expected value.');
51-
}
52-
})
53-
.then(
54-
() => killAllProcesses(),
55-
(err) => {
56-
killAllProcesses();
57-
throw err;
58-
},
59-
)
60-
.then(() => ngServe('--host=0.0.0.0', `--public-host=${localAddress}`))
61-
.then(() => fetch(localAddress))
62-
.then((response) => response.text())
63-
.then((body) => {
64-
if (!body.match(/<app-root><\/app-root>/)) {
65-
throw new Error('Response does not match expected value.');
66-
}
67-
})
68-
.then(
69-
() => killAllProcesses(),
70-
(err) => {
71-
killAllProcesses();
72-
throw err;
73-
},
74-
)
75-
.then(() => ngServe('--host=0.0.0.0', `--public-host=${firstLocalIp}`))
76-
.then(() => fetch(localAddress))
77-
.then((response) => response.text())
78-
.then((body) => {
79-
if (!body.match(/<app-root><\/app-root>/)) {
80-
throw new Error('Response does not match expected value.');
81-
}
82-
})
83-
.then(
84-
() => killAllProcesses(),
85-
(err) => {
86-
killAllProcesses();
87-
throw err;
88-
},
89-
)
90-
);
17+
return Promise.resolve()
18+
.then(() => ngServe('--host=0.0.0.0', `--public-host=${publicHost}`))
19+
.then(() => fetch(localAddress))
20+
.then((response) => response.text())
21+
.then((body) => {
22+
if (!body.match(/<app-root><\/app-root>/)) {
23+
throw new Error('Response does not match expected value.');
24+
}
25+
})
26+
.then(() => killAllProcesses())
27+
.then(() => ngServe('--host=0.0.0.0', `--disable-host-check`))
28+
.then(() => fetch(localAddress))
29+
.then((response) => response.text())
30+
.then((body) => {
31+
if (!body.match(/<app-root><\/app-root>/)) {
32+
throw new Error('Response does not match expected value.');
33+
}
34+
})
35+
36+
.then(() => killAllProcesses())
37+
.then(() => ngServe('--host=0.0.0.0', `--public-host=${localAddress}`))
38+
.then(() => fetch(localAddress))
39+
.then((response) => response.text())
40+
.then((body) => {
41+
if (!body.match(/<app-root><\/app-root>/)) {
42+
throw new Error('Response does not match expected value.');
43+
}
44+
})
45+
.then(() => killAllProcesses())
46+
.then(() => ngServe('--host=0.0.0.0', `--public-host=${firstLocalIp}`))
47+
.then(() => fetch(localAddress))
48+
.then((response) => response.text())
49+
.then((body) => {
50+
if (!body.match(/<app-root><\/app-root>/)) {
51+
throw new Error('Response does not match expected value.');
52+
}
53+
})
54+
.finally(() => killAllProcesses());
9155
}

tests/legacy-cli/e2e/tests/misc/ssl-default.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export default async function () {
1717
assert.strictEqual(response.status, 200);
1818
assert.match(await response.text(), /<app-root><\/app-root>/);
1919
} finally {
20-
killAllProcesses();
20+
await killAllProcesses();
2121
}
2222
}

tests/legacy-cli/e2e/tests/misc/ssl-with-cert.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ export default async function () {
2525
assert.strictEqual(response.status, 200);
2626
assert.match(await response.text(), /<app-root><\/app-root>/);
2727
} finally {
28-
killAllProcesses();
28+
await killAllProcesses();
2929
}
3030
}

tests/legacy-cli/e2e/tests/misc/title.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ export default async function () {
2020
throw new Error('Title of the process was not properly set.');
2121
}
2222
} finally {
23-
killAllProcesses();
23+
await killAllProcesses();
2424
}
2525
}

tests/legacy-cli/e2e/utils/process.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { concat, defer, EMPTY, from } from 'rxjs';
55
import { repeat, takeLast } from 'rxjs/operators';
66
import { getGlobalVariable } from './env';
77
import { catchError } from 'rxjs/operators';
8-
const treeKill = require('tree-kill');
8+
import treeKill from 'tree-kill';
99

1010
interface ExecOptions {
1111
silent?: boolean;
@@ -189,8 +189,22 @@ export function waitForAnyProcessOutputToMatch(
189189
return Promise.race(matchPromises.concat([timeoutPromise]));
190190
}
191191

192-
export function killAllProcesses(signal = 'SIGTERM') {
193-
_processes.forEach((process) => treeKill(process.pid, signal));
192+
export async function killAllProcesses(signal = 'SIGTERM'): Promise<void> {
193+
await Promise.all(
194+
_processes.map(
195+
({ pid }) =>
196+
new Promise<void>((resolve, reject) => {
197+
treeKill(pid, signal, (err) => {
198+
if (err) {
199+
reject(err);
200+
} else {
201+
resolve();
202+
}
203+
});
204+
}),
205+
),
206+
);
207+
194208
_processes = [];
195209
}
196210

0 commit comments

Comments
 (0)