javascript - 从测试中自动生成文档(javascript、jest)

标签 javascript testing jestjs documentation

我想生成依赖于测试的文档。例如,我有一个包含测试的文件:

describe("sum", () => {
  it("sums 1 and 2", () => {
    expect(sum(1, 2)).toEqual(3);
  });
  it("sums 3 and 4", () => {
    expect(sum(3, 4)).toEqual(7);
  });
});

describe("multiplication", () => {
  it("multiply 10 and 20", () => {
    expect(multiplication(10, 20)).toEqual(200);
  });
  it("multiply 30 and 40", () => {
    expect(multiplication(30, 40)).toEqual(1200);
  });
});


并且取决于该文件,我希望在此测试文件(摘要)顶部收到类似评论:
// Index test cases

// sum
// - [x] sums 1 and 2
// - [x] sums 3 and 4

// multiplication
// - [x] multiply 10 and 20
// - [x] multiply 30 and 40

describe("sum", () => {
  it("sums 1 and 2", () => {
    expect(sum(1, 2)).toEqual(3);
  });
  it("sums 3 and 4", () => {
    expect(sum(3, 4)).toEqual(7);
  });
});

describe("multiplication", () => {
  it("multiply 10 and 20", () => {
    expect(multiplication(10, 20)).toEqual(200);
  });
  it("multiply 30 and 40", () => {
    expect(multiplication(30, 40)).toEqual(1200);
  });
});

还有一个我可以发布到 GitHub Wiki 的 Markdown 文件:
# Index test cases

## sum
 - [x] sums 1 and 2
 - [x] sums 3 and 4

## multiplication
 - [x] multiply 10 and 20
 - [x] multiply 30 and 40

也许有最佳实践来做这些事情或准备使用包?
也许我应该使用 JSDoc?
主要是创建“摘要”而不是阅读数百行测试。

工作流程可以是:
write tests => run generateDocs.js => summary adds at top of file and markdown file creates
在这里创建了一个沙箱:https://codesandbox.io/s/documentation-from-tests-u9n3z

最佳答案

你可以从一个 babel 插件开始,例如 this或创建自己的 babel 插件/代码 mod。

关于javascript - 从测试中自动生成文档(javascript、jest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61369234/

相关文章:

javascript - 预加载视频文件 - HTML5 播放

javascript - Angular 和 TypeScript 循环遍历对象

reactjs - 使用 RequireJS 依赖项测试 es6 模块时 Jest 中的 "Define is not defined"

javascript - 如何在测试时正确模拟 useSelector Hook 以返回正确的值?

typescript - NestJS TypeORM 模拟存储库的数据源

javascript - 提取网页的调色板

javascript - Heroku 通过 JS 文件缩小破坏了我的 Rails + Angular.js 应用程序

php - 测试数组中的任何结果是否在特定节点中具有值

silverlight - 您使用什么来测试 Silverlight 应用程序?

testing - NSubstitute 模拟 NLog 记录器失败