添加测试

  1. npm install --save-dev ava
  2. npm install --save-dev sinon
  3. npm install --save-dev superkoa
  4. npm install --save-dev zombie
  1. mkdir test
  2. touch test/test.js

将下面代码copy进去

  1. import test from 'ava'
  2. import superkoa from 'superkoa'
  3. test.cb("first test", t => {
  4. superkoa('./app.js')
  5. .get("/")
  6. .expect(200, function (err, res) {
  7. t.ifError(err)
  8. var userId = res.body.id;
  9. t.is(res.text, 'Hello Koa', 'res.text == Hello Koa')
  10. t.end()
  11. });
  12. });

执行测试

  1. your-koa git:(master) ava -v
  2. first test (272ms)
  3. 1 test passed [16:54:55]

就这样完成了简单的测试

测试生命周期是非常重要的,给出模板test2.js

  • test.before([title], implementation)
  • test.after([title], implementation)
  • test.beforeEach([title], implementation)
  • test.afterEach([title], implementation)

举例

  1. test.before.cb((t) => {
  2. setTimeout(() => {
  3. t.end();
  4. }, 2000);
  5. });
  6. test('#save()', t => {
  7. let user = new User({
  8. username: 'i5ting',
  9. password: '0123456789'
  10. });
  11. user.save((err, u) => {
  12. if (err) log(err)
  13. t.is(u.username, 'i5ting');
  14. });
  15. });

更多参考https://github.com/i5ting/ava-practice

至此已有功能

  • livereload
  • mount-routes
  • test