Skip to content

Commit e77f09b

Browse files
author
avallete
committed
fix(tests): handle properly subchildrens process on test script
Switch from exec to spawn to kill children and sub-children in the same time from the main test process. Also add some error handling, and make the program exit with error code (1) if anything goes wrong during execution.
1 parent db03dcc commit e77f09b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

test/start.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
const exec = require('child_process').exec;
1+
const spawn = require('child_process').spawn;
22
const fs = require('fs-extra');
33
const path = require('path');
44

55
const strapiBin = path.resolve('./packages/strapi/bin/strapi.js');
66
const appName = 'testApp';
7+
let testExitCode = 0;
78
let appStart;
89

910
const databases = {
10-
mongo: `--dbclient=mongo --dbhost=127.0.0.1 --dbport=27017 --dbname=strapi-test-${new Date().getTime()} --dbusername="" --dbpassword=""`,
11-
postgres: `--dbclient=postgres --dbhost=127.0.0.1 --dbport=5432 --dbname=strapi-test --dbusername="" --dbpassword=""`,
12-
mysql: `--dbclient=mysql --dbhost=127.0.0.1 --dbport=3306 --dbname=strapi-test --dbusername="root" --dbpassword="root"`
11+
mongo: `--dbclient=mongo --dbhost=127.0.0.1 --dbport=27017 --dbname=strapi-test-${new Date().getTime()} --dbusername= --dbpassword=`,
12+
postgres: '--dbclient=postgres --dbhost=127.0.0.1 --dbport=5432 --dbname=strapi-test --dbusername= --dbpassword=',
13+
mysql: '--dbclient=mysql --dbhost=127.0.0.1 --dbport=3306 --dbname=strapi-test --dbusername=root --dbpassword=root'
1314
};
1415

1516
const {runCLI: jest} = require('jest-cli/build/cli');
@@ -29,32 +30,28 @@ const main = async () => {
2930

3031
const generate = (database) => {
3132
return new Promise((resolve, reject) => {
32-
const appCreation = exec(
33-
`node ${strapiBin} new ${appName} --dev ${database}`,
34-
);
33+
const appCreation = spawn('node', `${strapiBin} new ${appName} --dev ${database}`.split(' '), { detached: true });
3534

3635
appCreation.stdout.on('data', data => {
3736
console.log(data.toString());
3837

3938
if (data.includes('is ready at')) {
40-
appCreation.kill();
39+
process.kill(-appCreation.pid);
4140
return resolve();
4241
}
4342

4443
if (data.includes('Database connection has failed')) {
45-
appCreation.kill();
46-
return reject();
44+
process.kill(-appCreation.pid);
45+
return reject(new Error('Database connection has failed'));
4746
}
4847
});
4948
});
5049
};
5150

5251
const start = () => {
53-
return new Promise((resolve) => {
52+
return new Promise((resolve, reject) => {
5453
try {
55-
appStart = exec(
56-
`node ${strapiBin} start ${appName}`,
57-
);
54+
appStart = spawn('node', `${strapiBin} start ${appName}`.split(' '), {detached: true});
5855

5956
appStart.stdout.on('data', data => {
6057
console.log(data.toString());
@@ -65,7 +62,10 @@ const main = async () => {
6562
});
6663

6764
} catch (e) {
68-
console.error(e);
65+
if (typeof appStart !== 'undefined') {
66+
process.kill(-appStart.pid);
67+
}
68+
return reject(e);
6969
}
7070
});
7171
};
@@ -94,17 +94,22 @@ const main = async () => {
9494
};
9595

9696
const testProcess = async (database) => {
97-
await clean();
98-
await generate(database);
99-
await start();
100-
await test();
101-
102-
appStart.kill();
97+
try {
98+
await clean();
99+
await generate(database);
100+
await start();
101+
await test();
102+
process.kill(-appStart.pid);
103+
} catch (e) {
104+
console.error(e.message);
105+
testExitCode = 1;
106+
}
103107
};
104108

105109
await testProcess(databases.mongo);
106110
await testProcess(databases.postgres);
107111
await testProcess(databases.mysql);
112+
process.exit(testExitCode);
108113
};
109114

110115
main();

0 commit comments

Comments
 (0)