這個(gè)示例需要裝兩個(gè)包 koa2 和 supertest 下面已提供參考/包的鏈接
參考:https://www.npmjs.com/package/koa2
app.js 文件編輯如下
const koa = require('koa');
const app = new koa();
const result = new Promise(function(resolve,reject) {
setTimeout(function(){
resolve('Hello World');
},1000)
});
app.use(async(ctx,next)=>{
const start = new Date();
ctx.body = await result;
});
app.listen(3000);
export default app;
參考:https://www.npmjs.com/package/supertest
servertest.js 文件編輯如下
var app = require('./app');
var request = require('supertest').agent(app.listen());
describe('Hello World',function(){
it('should say "Hello World"',function(done){
request
.get('/')
.expect(200)
.expect('Hello World',done);
});
});
可參考的實(shí)例 demo :https://github.com/sunxiaochuan/koatest