集成测试会清空并删除 users 表,缺少确认数据库确为一次性测试库的保护
原文依据:6 处指南要求每次测试前截断表,并在结束后删除表。一个 API 示例直接复用应用的数据库连接;代码没有检查数据库名、环境或禁止生产连接。另一个示例虽写死为 test_db,但仍执行相同的破坏性查询。
如果应用配置、环境变量或本地凭据误指向共享、开发或生产数据库,运行测试可永久删除全部用户记录或整个 users 表。
该风险有源码支持,但内容是供用户采用的测试示例,并非 Skill 自身已执行的操作。主指南明确要求每次测试前截断表、结束后清理;API 示例复用应用导出的 pool,随后无环境或数据库名校验便 DROP/TRUNCATE users。若测试配置误连到共享库或生产库,会永久删除表或数据。用户可要求作者加入仅允许专用测试数据库的硬性校验,并在运行前核对连接目标与测试账户权限。
Integration tests verify real database operations and HTTP endpoints using `supertest` and a test database instance. Always truncate tables in `beforeEach` and tear down in `afterAll`.For full API integration test examples (supertest + PostgreSQL) and database repository integration tests, see [references/advanced-testing-patterns.md](references/advanced-testing-patterns.md).查看另外 5 个位置
// tests/integration/user.api.test.tsimport request from "supertest";import { app } from "../../src/app";import { pool } from "../../src/config/database";describe("User API Integration Tests", () => { beforeAll(async () => { // Setup test database await pool.query("CREATE TABLE IF NOT EXISTS users (...)"); }); afterAll(async () => { // Cleanup await pool.query("DROP TABLE IF EXISTS users"); await pool.end(); }); beforeEach(async () => { // Clear data before each test await pool.query("TRUNCATE TABLE users CASCADE"); }); beforeAll(async () => { pool = new Pool({ host: "localhost", port: 5432, database: "test_db", user: "test_user", password: "test_password", });import { app } from "../../src/app";import { pool } from "../../src/config/database"; beforeEach(async () => { // Clear data before each test await pool.query("TRUNCATE TABLE users CASCADE"); });