?????????????У???????????????????? utils.js ?е? test ???????÷????????????????????????????????????????????????????????? test ?????????????????????????????е???ν?????£?
????agent?????? API ??????
????file????????????????????
????group????????????????
????title???????????
????method???????????
????params?????????????????
????headers: ??????????е??????
????expect??supertest ??expect??
????callback??supertest ?????? end ??????????
????utils.js???????£?
// utils.js
const path = require('path');
const fs = require('fs');
const mdStr = {};
exports.test = function(obj){
if (!mdStr[obj.group]) {
mdStr[obj.group] = '';
mdStr[obj.group] += '## ' + obj.group + ' ';
}
const fields = {};
mdStr[obj.group] += `###${ obj.title }`${ obj.method }`${ obj.url } #### ???? `;
mdStr[obj.group] += ' ?????? | ???? | ?????? | ??? -----|-----|-----|----- ';
Object.keys(obj.params).forEach(function(param){
const paramVal = obj.params[param];
fields[param] = paramVal['value'];
mdStr[obj.group] += `${ param }|${ paramVal['type'] }|${ paramVal['required'] ?'??':'??'}|${ paramVal['desc'] } `;
});
mdStr[obj.group] += ' #### ?????? ???????: ';
mdStr[obj.group] += '```json ' + JSON.stringify(fields?? null?? 2) + ' ``` ';
mdStr[obj.group] += ' ??????: ';
if (obj.url.indexOf(':') > -1) {
obj.url = obj.url.replace(/:w*/g?? function(word){
return fields[word.substr(1)];
});
}
obj.agent[obj.method](obj.url)
.set(obj.header || {})
.query(fields)
.send(fields)
.expect(obj.expect)
.end(function(err?? res){
mdStr[obj.group] += '```json ' + JSON.stringify(res.body?? null?? 2) + ' ``` ';
mdStr[obj.group] += ' ';
if (process.env['GEN_DOC'] > 0) {
fs.writeFileSync(path.resolve(__dirname?? './docs/'?? obj.file + '.md')?? mdStr[obj.group]);
}
obj.callback(err?? res);
});
}
?????????????????????? docs ???????? npm run test:doc ???????? docs ???????????????????е??????????????????????? npm test ????????????package.json???????£?
????"scripts": {
????"test": "export NODE_ENV='test' && mocha"??
????"test:doc": "export NODE_ENV='test' && export GEN_DOC=1 && mocha"
????}
??????????????? API ???????????????? utils ?? test???????????д?????????
?????????????????????????????? test ??????????????? sign: true ???????????? test ??????????????ж???????????????
??????????????????????£?