国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

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

2024-05-06 16:37:53
字體:
來源:轉載
供稿:網友

前言

因為最近使用koa2做項目測試開發,所以想整合下travis ci,網上資料也比較少,于是自己就整了個,做個記錄。分享出來供大家參考學習,下面來看看詳細的介紹吧。

方法如下:

先來看下travis.yml的配置

language: node_jsnode_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 knexconst knex = Knex(knexConfig.development);Model.knex(knex);// initial appconst app = new Koa();// initial renderrender(app, { root: path.join(__dirname + '/view'), layout: 'template', viewExt: 'ejs', cache: true, debug: true});app.context.render = co.wrap(app.context.render);// initial staticapp.use(koaLogger()) .use(bodyParser()) .use(router.routes()) .use(koaStatic({ path: '/web', root: __dirname + "/../static" }));module.exports = app;

需要注意的是這里的

module.exports = app;

暴漏出來,再supertest中才可以獨立啟動server測試。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰顺县| 平和县| 萨嘎县| 四川省| 那曲县| 桂平市| 化州市| 望谟县| 鄂伦春自治旗| 翁牛特旗| 全州县| 庆元县| 苏尼特左旗| 蒙山县| 开封县| 邵阳市| 新野县| 通江县| 恩平市| 张家港市| 兴城市| 苗栗市| 奈曼旗| 酒泉市| 汝城县| 锡林郭勒盟| 青海省| 卢湾区| 萨迦县| 南木林县| 东海县| 黄石市| 通山县| 上栗县| 邯郸县| 改则县| 乌海市| 汶川县| 玉门市| 游戏| 馆陶县|