如何从mocha BDD测试生成API html文档?

时间:2016-07-05 15:09:43

标签: node.js mocha apiblueprint api-doc serverless-framework

我参与了一个使用Spring Boot java框架的项目,其中人员自动生成API文档。每次运行BDD / Integration样式测试时,都会从mocha测试中创建api蓝色打印文件。然后它运行了generate-html-from-api蓝图。我喜欢这种方法,因为它有两个优点:

1) API docs are always correct and up-to-date 
2) saves time, because no need to write another documentation file (like apidoc). 

是否有人尝试过并且有节点项目的工作示例?我找到了api-doc-test插件,但其文档有限。 ?理想情况下,我想运行:

mocha --recursive

哪会产生api-doc.html并置于test / tmp /下。

我看过swagger,但我真的不想两次指定端点信息,只是在BDD测试中写一次并且同时具有双重结果(测试+文档)真的很棒。

1 个答案:

答案 0 :(得分:0)

https://github.com/stackia/test2doc.js

我正在研究这个项目,它可以从BDD测试生成文档(目前只有API蓝图),正是您所需要的。

测试代码示例:

const doc = require('test2doc')
const request = require('supertest') // We use supertest as the HTTP request library
require('should') // and use should as the assertion library

// For Koa, you should exports app.listen() or app.callback() in your app entry
const app = require('./my-express-app.js')

after(function () {
  doc.emit('api-documentation.apib')
})

doc.group('Products').is(doc => {
  describe('#Products', function () {
    doc.action('Get all products').is(doc => {
      it('should get all products', function () {
        // Write specs towards your API endpoint as you would normally do
        // Just decorate with some utility methods
        return request(app)
          .get(doc.get('/products'))
          .query(doc.query({
            minPrice: doc.val(10, 'Only products of which price >= this value should be returned')
          }))
          .expect(200)
          .then(res => {
            body = doc.resBody(res.body)
            body.desc('List of all products')
              .should.not.be.empty()
            body[0].should.have.properties('id', 'name', 'price')
            body[0].price.desc('Price of this product').should.be.a.Number
          })
      })
    })
  })
})