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

 更新时间:2017年06月19日 11:28:15   作者:durban  
这篇文章主要给大家介绍了在Node.js环境下Koa2添加travis ci持续集成工具的方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。

前言

因为最近使用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测试。

总结

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

相关文章

  • CentOS 安装NodeJS V8.0.0的方法

    CentOS 安装NodeJS V8.0.0的方法

    这篇文章主要介绍了CentOS 安装NodeJS V8.0.0的方法,需要的朋友可以参考下
    2017-06-06
  • Windows下安装Bun像Node或Deno的现代JS运行时

    Windows下安装Bun像Node或Deno的现代JS运行时

    这篇文章主要为大家介绍了一款像Node或Deno的现代JavaScript运行时的bun在Windows下安装过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • 深入理解Commonjs规范及Node模块实现

    深入理解Commonjs规范及Node模块实现

    本篇文章主要介绍了深入理解Commonjs规范及Node模块实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Nodejs-child_process模块详细介绍

    Nodejs-child_process模块详细介绍

    Node.js的child进程模块允许创建并行任务,提高应用性能,介绍了exec、execFile、spawn、fork等方法,解释了它们的使用场景和优势,通过子进程模块,可以执行外部命令、脚本或创建新的Node.js实例,感兴趣的朋友跟随小编一起看看吧
    2024-09-09
  • nodejs 图片预览和上传的示例代码

    nodejs 图片预览和上传的示例代码

    本篇文章主要介绍了nodejs 图片预览和上传的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 前端如何更好的展示后端返回的十万条数据

    前端如何更好的展示后端返回的十万条数据

    这篇文章主要为大家介绍了前端如何更好的展示后端返回的十万条数据,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2021-11-11
  • node-sass常见报错的问题及解决方法

    node-sass常见报错的问题及解决方法

    在安装node-sass时,经常会遇到下载慢、版本不匹配或python错误等问题,使用淘宝镜像加速下载、通过nvm管理node版本或指定node-sass版本号安装,都能有效解决这些问题,若遇到python相关错误,检查node版本是否合适通常可以解决,感兴趣的朋友一起看看本文吧
    2024-09-09
  • node.js基于dgram数据报模块创建UDP服务器和客户端操作示例

    node.js基于dgram数据报模块创建UDP服务器和客户端操作示例

    这篇文章主要介绍了node.js基于dgram数据报模块创建UDP服务器和客户端操作,结合实例形式分析了node.js使用dgram数据报模块创建UDP服务器和客户端,以及进行UDP广播、组播相关操作技巧,需要的朋友可以参考下
    2020-02-02
  • Node.js中的events事件模块知识点总结

    Node.js中的events事件模块知识点总结

    在本篇文章里小编给大家整理的是一篇关于Node.js中的events事件模块知识点总结内容,有兴趣的朋友们可以跟着学习下。
    2021-12-12
  • NodeJS远程代码执行

    NodeJS远程代码执行

    这篇文章主要介绍了NodeJS远程代码执行方法的相关资料,需要的朋友可以参考下
    2016-08-08

最新评论