SuperAPI AI 工具集

running-tests

测试与质量

查看项目

更新于 10-16 01:33

技能介绍

用途:在 Nango 单体仓库中运行测试。该仓库使用 Vitest,并为单元、集成、CLI 三类测试分别维护配置,配置选错会静默地跑零个测试。

主要命令(均在仓库根目录执行):单元测试 npm run test:unit,文件模式 *.unit.test.ts;集成测试 npm run test:integration,模式 *.integration.test.ts;CLI 测试 npm run test:cli,模式 *.unit.cli-test.ts;全部测试 npm run test。也可指定文件或在集成测试中按名称匹配,例如 npm run test:unit -- <路径>、npm run test:integration -- -t "should be protected"。

运行前先执行 npm install,尤其在新工作区、切换分支或拉取变更后,缺少依赖会导致看似代码问题的导入错误。

集成测试要求:需要 Docker,全局 setup(tests/setup.ts)通过 testcontainers 启动 PostgreSQL、Elasticsearch、ActiveMQ 和 Redis;测试串行执行(fileParallelism: false、singleFork: true);每条测试超时 20 秒;环境变量 FLAG_AUTH_ROLES_ENABLED=true,时区 UTC。

编写集成测试常用工具位于 packages/server/lib/utils/tests.ts:runServer() 启动 Express 并执行迁移;shouldBeProtected(res) 断言 401;shouldRequireQueryEnv(res) 断言缺少 env 返回 400;authenticateUser(api, user, password) 返回会话 cookie,供需要 res.locals.user 的端点使用;isSuccess / isError 为响应类型守卫。种子数据来自 @nangohq/shared,例如 seeders.seedAccountEnvAndUser() 会创建账号、环境、用户和密钥。

自定义匹配器在 tests/setupFiles.ts 中全局可用:toBeIsoDate()、toBeIsoDateTimezone()、toBeUUID()、toBeSha256()、toBeWithinMs(expected, toleranceMs)。

限制与常见错误:集成测试用错配置会显示 0 个测试,应使用 npm run test:integration;会话端点用 bearer token 会 401,需改用 authenticateUser() 加会话 cookie;在包目录内运行会找不到配置,须从仓库根目录执行;Docker 未运行会连接被拒;缺少 query: { env: 'dev' } 会导致 400 invalid query。以上信息来自公开资料,暂未提供其他收费、平台或效果说明。