Node.js環(huán)境下Koa2添加travis ci持續(xù)集成工具的方法
前言
因?yàn)樽罱褂胟oa2做項(xiàng)目測(cè)試開(kāi)發(fā),所以想整合下travis ci,網(wǎng)上資料也比較少,于是自己就整了個(gè),做個(gè)記錄。分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹吧。
方法如下:
先來(lái)看下travis.yml的配置
language: node_js node_js: - "6" before_script: - ./node_modules/.bin/knex migrate:latest --knexfile='./app/knexfile.js' script: - npm run test
因?yàn)槭墙涌跍y(cè)試,所以首先需要做表創(chuàng)建等操作。
測(cè)試的命令:
NODE_ENV=production NODE_CONFIG_DIR='./app/config/' ./node_modules/.bin/mocha --require 'babel-polyfill' --compilers js:babel-register ./app/test/**/*.js
主要是測(cè)試這里,使用了supertest,大概看下是如何調(diào)用的。
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實(shí)例暴漏出來(lái),不然在做travis ci的集成后,啟動(dòng)了項(xiàng)目,測(cè)試的時(shí)候依然找不到具體訪問(wèn)地址。
來(lái)看下我的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;
暴漏出來(lái),再supertest中才可以獨(dú)立啟動(dòng)server測(cè)試。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Node.js檢測(cè)端口(port)是否被占用的簡(jiǎn)單示例
大家有沒(méi)有遇到過(guò)在開(kāi)啟本地服務(wù)時(shí),有這么一種情況:當(dāng)前端口已經(jīng)被另一個(gè)項(xiàng)目使用了,導(dǎo)致服務(wù)開(kāi)啟失敗。那么接下來(lái),我們通過(guò)簡(jiǎn)簡(jiǎn)單單的示例代碼來(lái)檢測(cè)端口是否已經(jīng)被占用。有需要的朋友們可以參考借鑒。2016-09-09詳解nodejs微信公眾號(hào)開(kāi)發(fā)——5.素材管理接口
這篇文章主要介紹了詳解nodejs微信公眾號(hào)開(kāi)發(fā)——5.素材管理接口,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04使用nodejs中httpProxy代理時(shí)候出現(xiàn)404異常的解決方法
下面小編就為大家?guī)?lái)一篇使用nodejs中httpProxy代理時(shí)候出現(xiàn)404異常的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08nodejs搭建本地服務(wù)器并訪問(wèn)文件操作示例
這篇文章主要介紹了nodejs搭建本地服務(wù)器并訪問(wèn)文件操作,結(jié)合實(shí)例形式分析了nodejs搭建本地服務(wù)器操作步驟及文件訪問(wèn)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05node.js中fs文件系統(tǒng)目錄操作與文件信息操作
本篇文章給大家詳細(xì)分析了node.js中fs文件系統(tǒng)目錄操作與文件信息操作的方法以及代碼詳解,需要的讀者可以參考下。2018-02-02Node.js中的HTTP?Server對(duì)象與GET、POST請(qǐng)求
這篇文章介紹了Node.js中的HTTP?Server對(duì)象與GET、POST請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07輕松創(chuàng)建nodejs服務(wù)器(10):處理上傳圖片
這篇文章主要介紹了輕松創(chuàng)建nodejs服務(wù)器(10):處理上傳圖片,本文是系列文章的最后一篇,包含了一個(gè)完整的處理上傳圖片的例子,需要的朋友可以參考下2014-12-12node.js中的fs.readlink方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.readlink方法使用說(shuō)明,本文介紹了fs.readlink方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12詳解nodejs微信公眾號(hào)開(kāi)發(fā)——3.封裝消息響應(yīng)模塊
上一篇文章:nodejs微信公眾號(hào)開(kāi)發(fā)(2)自動(dòng)回復(fù),實(shí)現(xiàn)了簡(jiǎn)單的關(guān)注回復(fù)。采用拼接字符串的形式,并不是很方便,這里我們將其封裝承接口。2017-04-04