前言
因为最近使用koa2做项目测试开发,所以想整合下travis ci,网上资料也比较少,于是自己就整了个,做个记录。分享出来供大家参考学习,下面来看看详细的介绍吧。
方法如下:
先来看下travis.yml的配置
language: node_js
node_js:
- "6"
before_script:
- ./node_modules/.bin/knex migrate:latest --knexfile='./app/knexfile.js'
script:
- npm run test因为是接口测试,所以首先需要做表创建等操作。
测试的命令:
NODE_ENV=production NODE_CONFIG_DIR='./app/config/' ./node_modules/.bin/mocha --require 'babel-polyfill' --compilers js:babel-register ./app/test/**/*.js主要是测试这里,使用了supertest,大概看下是如何调用的。
const request = require('supertest');
const should = require('should');
const index = require('../../index');let app = request(index.listen());
describe('/api/persons', function() {
let personId;
it('POST /api/persons - create person success and respond with 200', function(done) {
app.post('/api/persons')
.send({
'firstName': 'Jennifer',
'lastName': 'Lawrence',
'age': 24
})
.expect(200)
.expect(function(res) {
(res.body.id > 0).should.be.true;
})
.end(function(err, res) {
if (err) {
return done(err);
}
let resJson = JSON.parse(res.text);
personId = resJson.id;
done();
})
});
it('GET /api/persons - fetch persons item', function(done) {
app.get('/api/persons')
.expect(200)
.expect(function(res) {
(res.body.length > 0).should.be.true;
})
.end(function(err, res) {
if (err) {
return done(err);
}
done();
})
});
it('GET /api/persons/:id - fetch a person', function(done) {
app.get(`/api/persons/${personId}`)
.expect(200)
.expect(function(res) {
(res.body.id == personId).should.be.true;
})
.end(function(err, res) {
if (err) {
return done(err);
}
done();
})
});
it('DELETE /api/persons/:id - delete a person', function(done) {
app.delete(`/api/persons/${personId}`)
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
})
});
it('GET /api/persons/:id - fetch a person should 404', function(done) {
app.get(`/api/persons/${personId}`)
.expect(404)
.end(function(err, res) {
if (err) {
return done(err);
}
done();
})
});
});
这里主要注意的是
const index = require('../../index');









