strapi/test/start.js

130 lines
3.5 KiB
JavaScript
Raw Normal View History

const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
const cypress = require('cypress')
const { deleteApp } = require('./helpers/deleteFolder');
const strapiBin = path.resolve('./packages/strapi/bin/strapi.js');
const appName = 'testApp';
let testExitCode = 0;
let appStart;
const databases = {
mongo: `--dbclient=mongo --dbhost=127.0.0.1 --dbport=27017 --dbname=strapi-test-${new Date().getTime()} --dbusername= --dbpassword=`,
postgres: '--dbclient=postgres --dbhost=127.0.0.1 --dbport=5432 --dbname=strapi-test --dbusername= --dbpassword=',
mysql: '--dbclient=mysql --dbhost=127.0.0.1 --dbport=3306 --dbname=strapi-test --dbusername=root --dbpassword=root'
};
const {runCLI: jest} = require('jest-cli/build/cli');
const main = async () => {
const clean = () => {
return new Promise(async (resolve) => {
try {
fs.accessSync(appName);
await deleteApp(path.resolve(appName));
} catch(err) {
// Silent
}
resolve();
});
};
const generate = (database) => {
2018-06-03 20:46:43 +02:00
return new Promise((resolve, reject) => {
const appCreation = spawn('node', `${strapiBin} new ${appName} --dev ${database}`.split(' '), { detached: true });
appCreation.stdout.on('data', data => {
2018-10-31 17:20:09 +01:00
console.log(data.toString().trim());
if (data.includes('is ready at')) {
2018-10-31 17:20:09 +01:00
process.kill(appCreation.pid);
return resolve();
}
if (data.includes('Database connection has failed')) {
2018-10-31 17:20:09 +01:00
process.kill(appCreation.pid);
return reject(new Error('Database connection has failed'));
}
});
});
};
const start = () => {
return new Promise((resolve, reject) => {
2018-06-12 17:41:28 +02:00
try {
shell.cd('./testApp');
2018-10-31 17:20:09 +01:00
appStart = shell.exec(`strapi start`, { async: true, silent: true });
appStart.stdout.on('data', (data) => {
2018-06-12 17:41:28 +02:00
if (data.includes('To shut down your server')) {
shell.cd('..');
2018-06-12 17:41:28 +02:00
return resolve();
2018-10-31 17:20:09 +01:00
} else {
console.log(data.trim());
2018-06-12 17:41:28 +02:00
}
});
} catch (e) {
console.log(e)
if (typeof appStart !== 'undefined') {
2018-10-31 17:20:09 +01:00
process.kill(appStart.pid);
}
return reject(e);
2018-06-12 17:41:28 +02:00
}
});
};
const test = () => {
return new Promise(async (resolve) => {
2018-06-12 17:41:28 +02:00
// Run setup tests to generate the app.
await jest({
2018-08-26 17:13:58 +02:00
passWithNoTests: true,
testURL: 'http://localhost/'
2018-06-12 17:41:28 +02:00
}, [process.cwd()]);
2018-10-31 17:20:09 +01:00
const packagesPath = path.resolve(process.cwd(), 'packages');
const packages = fs.readdirSync(packagesPath)
2018-06-12 17:41:28 +02:00
.filter(file => file.indexOf('strapi') !== -1);
// Run tests in every packages.
for (let i in packages) {
await jest({
2018-06-13 15:31:33 +02:00
passWithNoTests: true,
2018-08-26 17:13:58 +02:00
testURL: 'http://localhost/'
2018-10-31 17:20:09 +01:00
}, [path.resolve(packagesPath, packages[i])]);
2018-06-12 17:41:28 +02:00
}
resolve();
});
};
const cypressTest = () => {
return cypress
2018-11-27 14:13:07 +01:00
.run({ spec: './packages/**/cypress/integration/*', browser: 'chrome' });
}
const testProcess = async (database) => {
try {
await clean();
await generate(database);
await start();
await cypressTest();
// await test();
2018-10-31 17:20:09 +01:00
process.kill(appStart.pid);
} catch (e) {
console.error(e.message);
testExitCode = 1;
}
};
await testProcess(databases.mongo);
// await testProcess(databases.postgres);
// await testProcess(databases.mysql);
// process.exit(testExitCode);
};
main();