Testing REST api built using Node.js(Express) on Travis CI

喜欢而已 提交于 2019-12-13 01:26:25

问题


I want to test the rest api service i created using Express framework and MongoDb on Travis CI. The test works locally on my machine but fails on Travis CI. I'm guessing the problem is with my .travis.yml and package.json.

Here is how i run my test locally:

// opens mongodb database on port 27017
mongod 

// reads the db and returns documents depending on the request
node express.js 

// test CRUD operations on the app
mocha express.test.js 

How can i do the same on travis CI? Here is my app https://github.com/mujuni88/Express-RestApi-Tutorial.

travis.yml:

language: node_js
node_js:
 - "0.10"
 - "0.9"
services: mongodb
script: 
 - "./node_modules/mocha/bin/mocha *.test.js"

package.json:

{
    "name":"express-restapi",
    "author":"Joe Buza",
    "description":"Express RestApi",
    "version": "0.0.1",
    "private": true,
    "keywords":["angular", "mocha", "mongodb", "express"],
    "license":"BSD",
    "dependencies": {
            "express":"3.4.4",
            "mongoskin":"0.6.0"
    },
    "devDependencies":{
        "mocha":"1.13.0",
        "expect.js":"0.2.0",
        "superagent":"0.15.7"
    },
    "scripts":{
        "start":"node express.js"
    }

}

回答1:


The answer is simple - Travis will not start up your server for you, if you not specify this in scripts steps. Look at my repository (it's a bit complicated, but hey, it does testing).

You can add require of express.js into express.test.js and start up server from there. After all tests is completed do not forget to tear it down.



来源:https://stackoverflow.com/questions/19719697/testing-rest-api-built-using-node-jsexpress-on-travis-ci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!