如何为 Story 编写交互测试并生成 Figma 链接?

解读

面试官真正想考察的是:

  1. 你是否能把 组件级 Story(CSF 3.0) 与 交互测试(@storybook/testing-library + @storybook/jest) 无缝结合,写出可在 CI 中稳定运行的自动化用例;
  2. 你是否熟悉 Storybook 7 的测试钩子(play 函数) 与 Figma 插件生态(design-addon/figma-addon),并能在 中国网络环境 下给出可落地的“设计-研发对齐”方案;
  3. 你是否理解 Angular 变更检测(OnPush 与 Ivy) 对 DOM 查询时机的影响,避免“测试通过但线上翻车”的坑。

知识点

  • CSF 3.0 的 play 函数:Storybook 官方推荐的交互测试入口,运行在 Canvas 沙箱,可复用 testing-library 查询器。
  • @storybook/addon-designs:国内镜像源可装(cnpm / pnpm --registry=https://registry.npmmirror.com),支持把 Figma 节点嵌入 Story 的 “Design” 面板;参数格式为 { type: 'figma', url: 'https://www.figma.com/file/xxx?node-id=xxx%3Axxx' }。
  • @storybook/testing-library:封装了 screen、userEvent,自动等待 Angular 的 NgZone.onStable,避免手动 fixture.whenStable()。
  • @storybook/jest:提供 expect 断言,与 Jest 语法 100% 对齐,可直接在 play 函数中断言。
  • CI 集成:官方 storybook-test-runner 基于 Playwright,支持在 GitHub Actions / GitLab CI(含 Gitee Go) 中并行跑;国内建议把 Figma 链接改为“可公开访问的只读链接”,否则 CI 会卡在 Figma 登录。
  • Angular 特有问题:Ivy 的 ng-reflect- 调试属性* 只在开发模式存在,切勿用 data-testid 依赖它;推荐在组件内部显式绑定 data-testid 或 role。

答案

  1. 安装依赖(以 pnpm 为例,镜像源已切国内)
    pnpm add -D @storybook/addon-designs @storybook/testing-library @storybook/jest storybook-test-runner

  2. 在 .storybook/main.ts 注册插件
    export default { stories: ['../src/**/*.stories.@(ts|mdx)'], addons: ['@storybook/addon-designs'] }

  3. 编写 CSF 3.0 Story(带交互测试与 Figma 链接)

    import { Meta, StoryObj } from '@storybook/angular';
    import { userEvent, within } from '@storybook/testing-library';
    import { expect } from '@storybook/jest';
    import { ButtonComponent } from './button.component';
    
    const meta: Meta<ButtonComponent> = {
      title: 'Example/Button',
      component: ButtonComponent,
      parameters: {
        design: {
          type: 'figma',
          url: 'https://www.figma.com/file/AbCdEfG123/Button?node-id=12%3A34',
          allowFullscreen: true,
        },
      },
    };
    export default meta;
    
    type Story = StoryObj<ButtonComponent>;
    
    export const Primary: Story = {
      args: { label: '提交', disabled: false },
      play: async ({ canvasElement }) => {
        const canvas = within(canvasElement);
        // 等待 Angular 变更检测完成
        await canvas.findByRole('button', { name: /提交/i });
        // 交互
        await userEvent.click(canvas.getByRole('button'));
        // 断言
        await expect(canvas.getByRole('button')).toHaveClass('ant-btn-loading');
      },
    };
    
  4. 本地验证
    pnpm storybook
    打开 http://localhost:6006,**“Design” 面板应出现 Figma 节点**;点击 “Interactions” 标签可回放 play 函数。

  5. CI 集成(GitHub Actions 片段,国内加速)

    - name: Run Storybook tests
      run: |
        pnpm dlx storybook-test-runner --url http://127.0.0.1:6006 \
          --browsers chromium \
          --maxWorkers 4
    
  6. 交付物
    把 Storybook 构建产物(storybook-static)部署到 阿里云 OSS / 腾讯云 COS,把访问地址与 Figma 链接一起贴到 PR 描述,实现“可交互的 Living Styleguide”。

拓展思考

  • 多主题切换:Angular 项目往往有 “深色/浅色” 两套 Design Token,可在 play 函数里先切换主题再断言,确保颜色 token 与 Figma 一致。
  • 权限收口:Figma 文件若放在企业私有团队,可让设计同学生成“仅限查看”的公开链接,或使用 Figma REST API + 自建代理(部署在阿里云函数计算),把关键帧导出为 SVG 后内嵌 Storybook,彻底摆脱外网限制。
  • 视觉回归:把 storybook-test-runner 与 chromatic(国内可用,但需科学加速)或 自研 pixelmatch 方案 结合,在 play 函数执行后再截图,实现“交互后的像素级比对”。
  • 跨组件链路:对于“按钮 → 弹窗 → 表单提交” 这种多组件链路,可在 Story 里使用 Angular 的 RouterTestingModule 与 ReactiveFormsModule,在 play 函数中完成 端到端数据流断言,让 Storybook 成为 轻量级 Cypress。

把上述思路讲清楚,面试官会认可你不仅“会写测试”,更能 把设计、研发、QA 三端拉通,这才是高级 Angular 候选人的分水岭。