电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 2053 人浏览分享

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

[复制链接]
2053 0

这篇文章主要给大家介绍了在Node.js环境下Koa2添加travis ci持续集成工具的方法,文中介绍的非常详细,对大家具有一定的参考学习价值

最近使用koa2做项目测试开发,所以想整合下travis ci,网上资料也比较少,于是自己就整了个,做个记录。分享出来供大家参考学习,下面来看看详细的介绍吧。

方法如下:

先来看下travis.yml的配置

  1. language: node_js
  2. node_js:
  3. - "6"
  4. before_script:
  5. - ./node_modules/.bin/knex migrate:latest --knexfile='./app/knexfile.js'
  6. script:
  7. - npm run test
复制代码

因为是接口测试,所以首先需要做表创建等操作。

测试的命令:

  1. NODE_ENV=production NODE_CONFIG_DIR='./app/config/' ./node_modules/.bin/mocha --require 'babel-polyfill'
  2. --compilers js:babel-register ./app/test/**/*.js
复制代码

主要是测试这里,使用了supertest,大概看下是如何调用的。

  1. const request = require('supertest');
  2. const should = require('should');
  3. const index = require('../../index');
  4. let app = request(index.listen());
  5. describe('/api/persons', function() {
  6. let personId;
  7. it('POST /api/persons - create person success and respond with 200', function(done) {
  8. app.post('/api/persons')
  9.   .send({
  10.   'firstName': 'Jennifer',
  11.   'lastName': 'Lawrence',
  12.   'age': 24
  13.   })
  14.   .expect(200)
  15.   .expect(function(res) {
  16.   (res.body.id > 0).should.be.true;
  17.   })
  18.   .end(function(err, res) {
  19.   if (err) {
  20.    return done(err);
  21.   }
  22.   let resJson = JSON.parse(res.text);
  23.   personId = resJson.id;
  24.   done();
  25.   })
  26. });

  27. it('GET /api/persons - fetch persons item', function(done) {
  28. app.get('/api/persons')
  29.   .expect(200)
  30.   .expect(function(res) {
  31.   (res.body.length > 0).should.be.true;
  32.   })
  33.   .end(function(err, res) {
  34.   if (err) {
  35.    return done(err);
  36.   }
  37.   done();
  38.   })
  39. });
  40. it('GET /api/persons/:id - fetch a person', function(done) {
  41. app.get(`/api/persons/${personId}`)
  42.   .expect(200)
  43.   .expect(function(res) {
  44.   (res.body.id == personId).should.be.true;
  45.   })
  46.   .end(function(err, res) {
  47.   if (err) {
  48.    return done(err);
  49.   }
  50.   done();
  51.   })
  52. });
  53. it('DELETE /api/persons/:id - delete a person', function(done) {
  54. app.delete(`/api/persons/${personId}`)
  55.   .expect(200)
  56.   .end(function(err, res) {
  57.   if (err) {
  58.    return done(err);
  59.   }
  60.   done();
  61.   })
  62. });
  63. it('GET /api/persons/:id - fetch a person should 404', function(done) {
  64. app.get(`/api/persons/${personId}`)
  65.   .expect(404)
  66.   .end(function(err, res) {
  67.   if (err) {
  68.    return done(err);
  69.   }
  70.   done();
  71.   })
  72. });
  73. });
复制代码

这里主要注意的是

  1. const index = require('../../index');
复制代码

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

  1. import Knex from 'knex';
  2. import {
  3. Model
  4. } from 'objection';
  5. import knexConfig from './knexfile';
  6. import config from 'config';
  7. import Koa from 'koa';
  8. import koaLogger from 'koa-logger';
  9. import bodyParser from 'koa-bodyparser';
  10. import render from 'koa-ejs';
  11. import co from 'co';
  12. import koaStatic from "koa2-static"
  13. import router from './router';

  14. const path = require('path');

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

  18. // initial app
  19. const app = new Koa();

  20. // initial render
  21. render(app, {
  22. root: path.join(__dirname + '/view'),
  23. layout: 'template',
  24. viewExt: 'ejs',
  25. cache: true,
  26. debug: true
  27. });
  28. app.context.render = co.wrap(app.context.render);

  29. // initial static

  30. app.use(koaLogger())
  31. .use(bodyParser())
  32. .use(router.routes())
  33. .use(koaStatic({
  34. path: '/web',
  35. root: __dirname + "/../static"
  36. }));

  37. module.exports = app;
复制代码

需要注意的是这里的

  1. module.exports = app;
复制代码

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

您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

关注

0

粉丝

60

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.