Skip to content
dsh-invest 文档
Esc
↑↓navigate↵open⌘Jpreview
On this page

测试

用 describeVenue 跑契约检查,用假端口跑数据面,用 e2e 只验真实页面。

插件页:插件与组件状态。

插件页:插件与组件状态。

目标

把「这个 provider 合不合格」从人工审查变成可重复的测试:纯数据约束交给 describeVenue,取数与错误分类用假端口,真实页面行为留给 e2e。

要改哪些文件

tests/conformance.spec.ts   describeVenue(manifest) 逐条跑
tests/history.spec.ts       假 EgressPort:URL、参数、错误分类
tests/client/nav.spec.tsx   jsdom:注册、卸载、重挂
vitest.config.ts            两个 project:host(node) / client(jsdom)

契约

import { describe, it } from 'vitest';
import { describeVenue, type ExistingVenue } from 'dsh-invest/testing';
import { demoManifests } from '../src/shared/manifest.ts';

const existing: ExistingVenue[] = [];
for (const manifest of demoManifests()) {
  const known = [...existing];           // 每次循环快照,否则后一条会看到前一条
  for (const check of describeVenue(manifest, { existing: known })) {
    it(`${manifest.id} · ${check.name}`, check.run);
  }
  existing.push({ id: manifest.id, markets: [] });
}

describeVenue 只做纯数据检查:注册期校验(V1–V7)、marketKey 可重现且不重复、双语 display、caps.markets ⊆ markets、history 周期可翻 Vela、stream 的 connKey / unwatch、detail sections、lake ⊆ history、execution adapter、图标 sanitize、caps 可序列化。不建连接、不打网络、不造定时器。

假端口:

const port = {
  fetchJson: async (url: string) => ({ status: 200, body: fixtureFor(url) }),
  now: () => 1_700_000_000_000,
  warn: () => {},
};

失败与重试

  • 单测不打网络。上游失败、限流、空页都用 fixture 表达;否则 CI 会随上游抖动。
  • 时间要注入。now / 时钟都从参数来,不要 Date.now() 直接进断言。
  • 不要断言日志文案。断言返回值、错误码、注册表内容;日志措辞会变。
  • 渲染只在 tests/client/。scripts/verify.mjs 的替身刻意不装 React 渲染器,在那里写渲染断言只会得到恒真的假绿灯。
  • e2e 只验真实页面:打开、点击、截图;不要拿它替代契约测试。

验证

pnpm --filter dsh-invest-provider-demo exec vitest run tests/conformance.spec.ts
pnpm --filter dsh-invest-provider-demo run check:all

预期:第一条输出 Test Files 1 passed 与 Tests N passed(N 是 describeVenue 的检查数);第二条依次跑 build / typecheck / verify / verify:plugin / test,全部退出码 0。

改过 pnpm-lock.yaml 时,最终判据是 CI 的 pnpm install --frozen-lockfile,不是本机绿。

Was this page helpful?