chore(e2e): make stability (#2751)

This commit is contained in:
被雨水过滤的空气-Rain 2023-10-06 18:59:59 +08:00 committed by GitHub
parent 68009d3d32
commit 70b61c60c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 35 deletions

View File

@ -94,7 +94,8 @@
"ts-jest": "^29.1.1", "ts-jest": "^29.1.1",
"typescript": "5.1.3", "typescript": "5.1.3",
"vite": "^4.4.9", "vite": "^4.4.9",
"vitest": "^0.34.3" "vitest": "^0.34.3",
"axios": "^0.26.1"
}, },
"volta": { "volta": {
"node": "18.14.2", "node": "18.14.2",

View File

@ -20,19 +20,13 @@ const runCodegenSync = () => {
}; };
const run = async () => { const run = async () => {
const { kill } = await runNocoBase(); const { kill, awaitForNocoBase } = await runNocoBase();
await awaitForNocoBase();
if (kill) {
// 等待服务成功启动
setTimeout(() => {
console.log('Starting codegen...'); console.log('Starting codegen...');
runCodegenSync(); runCodegenSync();
kill?.('SIGKILL'); kill?.('SIGKILL');
}, 5000);
} else {
console.log('Starting codegen...');
runCodegenSync();
}
}; };
void run(); run();

View File

@ -1,3 +1,3 @@
import { runNocoBase } from './utils'; import { runNocoBase } from './utils';
void runNocoBase(); runNocoBase();

View File

@ -1,21 +1,15 @@
import { runCommand, runNocoBase } from './utils'; import { runCommand, runNocoBase } from './utils';
const run = async () => { const run = async () => {
const { kill } = await runNocoBase({ const { kill, awaitForNocoBase } = await runNocoBase({
stdio: 'ignore', // 不输出服务的日志,避免干扰测试的日志 stdio: 'ignore', // 不输出服务的日志,避免干扰测试的日志
}); });
if (kill) { await awaitForNocoBase();
// 等待服务成功启动
setTimeout(() => {
console.log('Start running tests...'); console.log('Start running tests...');
void runCommand('npx', ['playwright', 'test', ...process.argv.slice(2)]); runCommand('npx', ['playwright', 'test', ...process.argv.slice(2)]);
kill?.('SIGKILL'); kill?.('SIGKILL');
}, 5000);
} else {
console.log('Start running tests...');
void runCommand('npx', ['playwright', 'test', ...process.argv.slice(2)]);
}
}; };
void run(); run();

View File

@ -1,3 +1,4 @@
import axios from 'axios';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import type { CommonOptions } from 'execa'; import type { CommonOptions } from 'execa';
import execa from 'execa'; import execa from 'execa';
@ -40,6 +41,57 @@ function checkPort(port) {
}); });
} }
/**
*
*/
const checkServer = async (duration = 1000) => {
return new Promise((resolve, reject) => {
const timer = setInterval(async () => {
if (!(await checkPort(process.env.APP_PORT))) {
return;
}
axios
.get(`http://localhost:${process.env.APP_PORT}/api/__health_check`)
.then((response) => {
if (response.status === 200) {
clearInterval(timer);
resolve(true);
}
})
.catch((error) => {
clearInterval(timer);
console.error('Request error:', error);
reject(error);
});
}, duration);
});
};
/**
* UI
* @param duration
*/
const checkUI = async (duration = 1000) => {
return new Promise((resolve, reject) => {
const timer = setInterval(async () => {
axios
.get(`http://localhost:${process.env.APP_PORT}/__umi/api/bundle-status`)
.then((response) => {
if (response.data.bundleStatus.done) {
clearInterval(timer);
resolve(true);
}
})
.catch((error) => {
clearInterval(timer);
console.error('Request error:', error);
reject(error);
});
}, duration);
});
};
export const runNocoBase = async (options?: CommonOptions<any>) => { export const runNocoBase = async (options?: CommonOptions<any>) => {
// 用于存放 playwright 自动生成的相关的文件 // 用于存放 playwright 自动生成的相关的文件
if (!fs.existsSync('playwright')) { if (!fs.existsSync('playwright')) {
@ -57,25 +109,33 @@ 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 () => {
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) {
console.log('yarn nocobase install'); console.log('yarn nocobase install');
await runCommand('yarn', ['nocobase', 'install'], options); await runCommand('yarn', ['nocobase', 'install'], options);
console.log(`yarn start -d -p ${process.env.APP_PORT}`); console.log(`yarn start -d -p ${process.env.APP_PORT}`);
await runCommand('yarn', ['start', '-d', `-p ${process.env.APP_PORT}`], options); await runCommand('yarn', ['start', '-d', `-p ${process.env.APP_PORT}`], options);
return {}; return { awaitForNocoBase };
}
if (await checkPort(process.env.APP_PORT)) {
console.log('Server is running, skip starting server.');
return {};
} }
// 加上 -f 会清空数据库 // 加上 -f 会清空数据库
console.log('yarn nocobase install -f'); console.log('yarn nocobase install -f');
await runCommand('yarn', ['nocobase', 'install', '-f'], options); await runCommand('yarn', ['nocobase', 'install', '-f'], options);
if (await checkPort(process.env.APP_PORT)) {
console.log('Server is running, skip starting server.');
return { awaitForNocoBase };
}
console.log('starting server...'); console.log('starting server...');
const { cancel, kill } = runCommand('yarn', ['dev', `-p ${process.env.APP_PORT}`, ...process.argv.slice(2)], options); const { cancel, kill } = runCommand('yarn', ['dev', `-p ${process.env.APP_PORT}`, ...process.argv.slice(2)], options);
return { cancel, kill }; return { cancel, kill, awaitForNocoBase };
}; };