fix(e2e): avoid error

This commit is contained in:
Rain 2023-10-06 23:29:45 +08:00
parent 79084ddaa7
commit 2fc81524b0

View File

@ -44,9 +44,15 @@ function checkPort(port) {
/** /**
* *
*/ */
const checkServer = async (duration = 1000) => { const checkServer = async (duration = 1000, max = 30) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let count = 0;
const timer = setInterval(async () => { const timer = setInterval(async () => {
if (count++ > max) {
clearInterval(timer);
return reject(new Error('Server start timeout.'));
}
if (!(await checkPort(process.env.APP_PORT))) { if (!(await checkPort(process.env.APP_PORT))) {
return; return;
} }
@ -60,9 +66,7 @@ const checkServer = async (duration = 1000) => {
} }
}) })
.catch((error) => { .catch((error) => {
clearInterval(timer); console.error('Request error:', error.message);
console.error('Request error:', error);
reject(error);
}); });
}, duration); }, duration);
}); });
@ -72,9 +76,15 @@ const checkServer = async (duration = 1000) => {
* UI * UI
* @param duration * @param duration
*/ */
const checkUI = async (duration = 1000) => { const checkUI = async (duration = 1000, max = 30) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let count = 0;
const timer = setInterval(async () => { const timer = setInterval(async () => {
if (count++ > max) {
clearInterval(timer);
return reject(new Error('UI start timeout.'));
}
axios axios
.get(`http://localhost:${process.env.APP_PORT}/__umi/api/bundle-status`) .get(`http://localhost:${process.env.APP_PORT}/__umi/api/bundle-status`)
.then((response) => { .then((response) => {
@ -84,9 +94,7 @@ const checkUI = async (duration = 1000) => {
} }
}) })
.catch((error) => { .catch((error) => {
clearInterval(timer); console.error('Request error:', error.message);
console.error('Request error:', error);
reject(error);
}); });
}, duration); }, duration);
}); });
@ -110,11 +118,16 @@ export const runNocoBase = async (options?: CommonOptions<any>) => {
dotenv.config({ path: path.resolve(process.cwd(), '.env.e2e') }); dotenv.config({ path: path.resolve(process.cwd(), '.env.e2e') });
const awaitForNocoBase = async () => { const awaitForNocoBase = async () => {
console.log('check server...'); if (process.env.CI) {
await checkServer(); console.log('check server...');
console.log('server is ready, check UI...'); await checkServer();
await checkUI(); } else {
console.log('UI is ready.'); console.log('check server...');
await checkServer();
console.log('server is ready, check UI...');
await checkUI();
console.log('UI is ready.');
}
}; };
if (process.env.CI) { if (process.env.CI) {