Node.js环境下Koa2添加travis ci持续集成工具的方法

所属分类: 网络编程 / JavaScript 阅读数: 1264
收藏 0 赞 0 分享

前言

因为最近使用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');

需要将koa实例暴漏出来,不然在做travis ci的集成后,启动了项目,测试的时候依然找不到具体访问地址。

来看下我的index.js

import Knex from 'knex';
import {
 Model
} from 'objection';
import knexConfig from './knexfile';
import config from 'config';
import Koa from 'koa';
import koaLogger from 'koa-logger';
import bodyParser from 'koa-bodyparser';
import render from 'koa-ejs';
import co from 'co';
import koaStatic from "koa2-static"
import router from './router';

const path = require('path');

// initial knex
const knex = Knex(knexConfig.development);
Model.knex(knex);

// initial app
const app = new Koa();

// initial render
render(app, {
 root: path.join(__dirname + '/view'),
 layout: 'template',
 viewExt: 'ejs',
 cache: true,
 debug: true
});
app.context.render = co.wrap(app.context.render);

// initial static

app.use(koaLogger())
 .use(bodyParser())
 .use(router.routes())
 .use(koaStatic({
 path: '/web',
 root: __dirname + "/../static"
 }));

module.exports = app;

需要注意的是这里的

module.exports = app;

暴漏出来,再supertest中才可以独立启动server测试。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

jQuery 行级解析读取XML文件(附源码)

项目中应用jQuery解析读取XML语言配置文件来实现语言的调度。这是jQuery解析读取XML文件功能的测试源码,现拿出来分享。
收藏 0 赞 0 分享

JS 文件本身编码转换 图文教程

JS编码转换,这句话本身就是一句具有二重义的话。通常理解为JS文件里能转换编码的代码,但是,我所碰到的问题并不是这样的,是要解决JS文件本身的编码问题,它是UTF-8编码的还是ANSI编码的?
收藏 0 赞 0 分享

jQuery Ajax之$.get()方法和$.post()方法

load()方法通常用来从Web服务器上获取静态的数据文件,然而这并不能体现Ajax的全部价值。在项目中,如果需要传递一些参数给服务器中的页面,那么可以使用$.get()或者$.post()方法(或者是后面要讲解到的$.ajax方法)。
收藏 0 赞 0 分享

jQuery Ajax之load()方法

jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是laod()、$.get()和$.post()方法,第3层是$.getScript()和$.getJSON()方法。
收藏 0 赞 0 分享

JavaScript 核心参考教程 内置对象

JavaScript 是根据 "ECMAScript"标准制定的网页脚本语言。这个标准由 ECMA 组织发展和维护。ECMA-262 是正式的 JavaScript 标准。
收藏 0 赞 0 分享

JavaScript 核心参考教程 RegExp对象

JavaScript 核心参考教程RegExp对象,学习正则表达式的朋友可以参考下。
收藏 0 赞 0 分享

javascript hashtable实现代码

javascript中没有像c#,java那样的哈希表(hashtable), 然而,javascript中的Array也只有一些类似于'哈希表'的非常简单功能。
收藏 0 赞 0 分享

百度留言本js 大家可以参考下

百度留言本js 大家可以参考下。
收藏 0 赞 0 分享

javascript 判断某年某月有多少天的实现代码 推荐

以前写网页的时候,经常碰到选择日期的问题,其实就是判断某年某月有多少天。
收藏 0 赞 0 分享

让iframe子窗体取父窗体地址栏参数(querystring)

突然用到,记录一下,对地址栏字符串用正则处理最好,有时间研究一下。 主要是思路。
收藏 0 赞 0 分享
查看更多