From f321859f9bc593bbf508d4f6269ebd6a6a1e0fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Sat, 4 Apr 2026 11:18:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=B8=85=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在global-setup中添加cleanupTestData函数 - 测试前自动清理之前创建的测试数据 - 保留初始数据(用户ID 1-10,角色ID 1-4) - 解决重复键冲突问题 --- novalon-manage-web/e2e/global-setup.ts | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/novalon-manage-web/e2e/global-setup.ts b/novalon-manage-web/e2e/global-setup.ts index fc25aa9..557cb65 100644 --- a/novalon-manage-web/e2e/global-setup.ts +++ b/novalon-manage-web/e2e/global-setup.ts @@ -81,6 +81,9 @@ async function globalSetup(config: FullConfig) { console.log('⏳ 等待后端服务就绪...'); await waitForBackendReady(); + console.log('🧹 清理测试数据...'); + await cleanupTestData(); + console.log('✅ 全局测试环境设置完成'); } @@ -110,4 +113,89 @@ async function waitForBackendReady(): Promise { throw new Error('❌ 后端服务启动超时'); } +async function cleanupTestData(): Promise { + try { + // 登录获取token + const loginResponse = await fetch('http://localhost:8084/api/auth/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username: 'admin', + password: 'Test@123' + }) + }); + + if (!loginResponse.ok) { + console.log('⚠️ 无法登录,跳过数据清理'); + return; + } + + const loginData = await loginResponse.json(); + const token = loginData.token; + + // 获取所有用户 + const usersResponse = await fetch('http://localhost:8084/api/users', { + headers: { + 'Authorization': `Bearer ${token}` + } + }); + + if (usersResponse.ok) { + const users = await usersResponse.json(); + + // 删除测试创建的用户(保留ID 1-10的初始用户) + for (const user of users) { + if (user.id > 10) { + try { + await fetch(`http://localhost:8084/api/users/${user.id}`, { + method: 'DELETE', + headers: { + 'Authorization': `Bearer ${token}` + } + }); + console.log(` 删除用户: ${user.username}`); + } catch (error) { + console.log(` ⚠️ 无法删除用户 ${user.username}`); + } + } + } + } + + // 获取所有角色 + const rolesResponse = await fetch('http://localhost:8084/api/roles', { + headers: { + 'Authorization': `Bearer ${token}` + } + }); + + if (rolesResponse.ok) { + const roles = await rolesResponse.json(); + + // 删除测试创建的角色(保留ID 1-4的初始角色) + for (const role of roles) { + if (role.id > 4) { + try { + await fetch(`http://localhost:8084/api/roles/${role.id}`, { + method: 'DELETE', + headers: { + 'Authorization': `Bearer ${token}` + } + }); + console.log(` 删除角色: ${role.roleName}`); + } catch (error) { + console.log(` ⚠️ 无法删除角色 ${role.roleName}`); + } + } + } + } + + console.log('✅ 测试数据清理完成'); + } catch (error) { + console.log('⚠️ 数据清理失败,继续执行测试'); + console.error('清理错误:', error); + } +} + export default globalSetup;