Commit fe86a559 authored by Alex Quispe's avatar Alex Quispe
Browse files

Se adicionó la función para personalizar los templates de los tests.

parent ab967927
......@@ -122,6 +122,9 @@ const body = await ApiGen.get('/api/v1/users').generate()
app
├─ documentation
| ├─ build
| ├─ template
| │ ├─ test-block.js
| │ └─ test-body.js
| ├─ src
| │ └─ api-v1-users.js
| ├─ scaffold.js
......@@ -168,13 +171,43 @@ Se requieren los paquetes: `ava` y `supertest` para ejecutar los tests.
const ApiGen = require('./../../')
const path = require('path')
ApiGen.TEST_PATH = path.resolve(__dirname, '../test')
ApiGen.TEST_PATH = path.resolve(__dirname, '../test')
ApiGen.TEMPLATE_PATH = path.resolve(__dirname, 'template')
const app = require(process.ced())
const app = require(process.cwd())
ApiGen.scaffoldTest(app)
```
### Templates para generar los tests
Templates por defecto: Se configuran con la variable `ApidocGenerate.TEMPLATE_PATH`
#### Archivo `template/test-block.js`
```js
test.serial.cb('[GROUP] [METHOD] PATH', t => {
request(app).METHOD('PATH')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
})
// <!-- [TEST DEFINITION] --!> //
```
#### Archivo `template/test-body.js`
```js
const test = require('ava')
const request = require('supertest')
const app = require(process.cwd())
// <!-- [TEST DEFINITION] --!> //
```
### Archivo `documentation/generate.js`
Este fichero se encarga de generar la documentación con soporte para ApidocJS y Swagger.
......
......@@ -283,6 +283,7 @@ ApidocGenerator.REDIRECT_PATH = '/apidoc'
ApidocGenerator.DOC_SERVER_PORT = 5000
ApidocGenerator.DESCRIPTION = ''
ApidocGenerator.HELP_TYPE = ''
ApidocGenerator.TEMPLATE_PATH = path.resolve(__dirname, '../template')
function _getPropertiesExtra(route) {
if (ApidocGenerator.HELP_TYPE === 'YAML') {
......@@ -414,6 +415,7 @@ async function _buildApidocJS () {
const apidocConfig = {
title : 'Apidoc',
name : 'Documentación',
description : ApidocGenerator.DESCRIPTION,
version : '1.0.0',
url : `${ApidocGenerator.API_URL}`,
sampleUrl : `${ApidocGenerator.API_URL}`,
......@@ -549,7 +551,10 @@ function _crearContenido (groupKey, route) {
function _crearContenidoTest (groupKey, route) {
// Archivo .test.js
let content = util.readFile(path.resolve(__dirname, '../template/test.js'))
let TEST_BODY_PATH = path.resolve(ApidocGenerator.TEMPLATE_PATH, 'test-body.js')
if (!util.isFile(TEST_BODY_PATH)) { TEST_BODY_PATH = path.resolve(__dirname, '../template/test-body.js') }
let content = util.readFile(TEST_BODY_PATH)
let filePath = path.resolve(ApidocGenerator.TEST_PATH,`${groupKey}.test.js`)
if (!util.isFile(filePath)) {
util.writeFile(filePath, content)
......@@ -558,13 +563,15 @@ function _crearContenidoTest (groupKey, route) {
const ROUTE_FLAG = '// <!-- [TEST DEFINITION] --!> //'
content = util.readFile(filePath)
if (!content.includes(`test.serial.cb('[${route.group}] [${route.method}] ${route.path}', t => {`)) {
const TEST_BLOCK_PATH = path.resolve(__dirname, '../template/test-block.js')
let testBlock = util.readFile(TEST_BLOCK_PATH)
testBlock = _.replace(testBlock, /GROUP/g, route.group)
testBlock = _.replace(testBlock, /METHOD/g, route.method)
testBlock = _.replace(testBlock, /PATH/g, route.path)
content = content.replace(ROUTE_FLAG, testBlock)
let TEST_BLOCK_PATH = path.resolve(ApidocGenerator.TEMPLATE_PATH, 'test-block.js')
if (!util.isFile(TEST_BLOCK_PATH)) { TEST_BLOCK_PATH = path.resolve(__dirname, '../template/test-block.js') }
let newTestBlock = util.readFile(TEST_BLOCK_PATH)
newTestBlock = _.replace(newTestBlock, /GROUP/g, route.group)
newTestBlock = _.replace(newTestBlock, /METHOD/g, route.method)
newTestBlock = _.replace(newTestBlock, /PATH/g, route.path)
const ROUTE_KEY = newTestBlock.split('\n')[0]
if (!content.includes(ROUTE_KEY)) {
content = content.replace(ROUTE_FLAG, newTestBlock)
console.log(` [${groupKey}] - [${route.method}] ${route.path} \u2713`)
}
content = content.trim() + '\n'
......
const ApiGen = require('./../../')
const path = require('path')
ApiGen.TEST_PATH = path.resolve(__dirname, '../test')
ApiGen.TEST_PATH = path.resolve(__dirname, '../test')
ApiGen.TEMPLATE_PATH = path.resolve(__dirname, 'template')
const app = require(process.cwd())
......
test.serial.cb('[GROUP] [METHOD] PATH', t => {
t.pass()
t.end()
// request(app).METHOD('PATH')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
// <!-- [TEST DEFINITION] --!> //
......@@ -4,13 +4,15 @@ const request = require('supertest')
const app = require(process.cwd())
test.serial.cb('[Api Custom] [get] /api/custom/other/route', t => {
request(app).get('/api/custom/other/route')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).get('/api/custom/other/route')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
// <!-- [TEST DEFINITION] --!> //
......@@ -4,43 +4,51 @@ const request = require('supertest')
const app = require(process.cwd())
test.serial.cb('[Api V1 Users] [get] /api/v1/users/', t => {
request(app).get('/api/v1/users/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).get('/api/v1/users/')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
test.serial.cb('[Api V1 Users] [get] /api/v1/users/:id', t => {
request(app).get('/api/v1/users/:id')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).get('/api/v1/users/:id')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
test.serial.cb('[Api V1 Users] [post] /api/v1/users/', t => {
request(app).post('/api/v1/users/')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).post('/api/v1/users/')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
test.serial.cb('[Api V1 Users] [post] /api/v1/users/bulk', t => {
request(app).post('/api/v1/users/bulk')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).post('/api/v1/users/bulk')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
// <!-- [TEST DEFINITION] --!> //
......@@ -4,13 +4,15 @@ const request = require('supertest')
const app = require(process.cwd())
test.serial.cb('[Default] [get] /ruta/sin/grupo', t => {
request(app).get('/ruta/sin/grupo')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
t.pass()
t.end()
})
t.pass()
t.end()
// request(app).get('/ruta/sin/grupo')
// .set('Accept', 'application/json')
// .expect('Content-Type', /json/)
// .end((err, res) => {
// t.pass()
// t.end()
// })
})
// <!-- [TEST DEFINITION] --!> //
const test = require('ava')
const request = require('supertest')
const app = require(process.cwd())
// <!-- [TEST DEFINITION] --!> //
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment