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

gre*_*end 7 javascript testing documentation jestjs

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

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);
  });
});

Run Code Online (Sandbox Code Playgroud)

并且取决于该文件,我希望收到类似此测试文件顶部的评论(摘要)的内容:

// 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);
  });
});
Run Code Online (Sandbox Code Playgroud)

还有一个我可以发布到 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
Run Code Online (Sandbox Code Playgroud)

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

工作流程可以是:

write tests => run generateDocs.js => summary adds at top of file and markdown file creates

在这里创建了一个沙箱:https://codesandbox.io/s/documentation-from-tests-u9n3z