在travis的后台运行节点服务器

时间:2017-03-28 23:57:44

标签: node.js bash mocha travis-ci chai

我正在尝试对travis进行单元测试,这需要一个位于单独目录中的节点服务器来运行。所以我需要在运行chai-mocha测试之前让服务器在后台运行。但是我一直在努力工作......这是我的.yml文件

language: node_js
node_js:
  - "6.10.0"
services: mongodb
env:
  - TEST_DIR=UserService/server
  - TEST_DIR=ApplicantService
  - TEST_DIR=ta-corrd-service
before_script:
  - sleep 15
script:
  - cd UserService/server && npm install nodemon && npm install && npm start& cd $TEST_DIR && npm install && npm test

我需要进入userservice / server目录,然后执行npm start,然后在每个test_dir中执行npm测试。

错误我上了travis

> proj-awol@0.0.0 test /home/travis/build/csc302-2017-spring/proj-Awol/ta-corrd-service
> mocha --recursive
  course
(node:1780) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
    1) "before all" hook
  2) "after all" hook
  0 passing (68ms)
  2 failing
  1) course "before all" hook:
     Uncaught Error: connect ECONNREFUSED 127.0.0.1:3002
      at Object.exports._errnoException (util.js:1022:11)
      at exports._exceptionWithHostPort (util.js:1045:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
  2) course "after all" hook:
     Uncaught Error: socket hang up
      at createHangUpError (_http_client.js:254:15)
      at Socket.socketCloseListener (_http_client.js:286:23)
      at TCP._handle.close [as _onclose] (net.js:498:12)
npm ERR! Test failed.  See above for more details.
The command "cd UserService/server && npm install nodemon && npm install && npm start& cd $TEST_DIR && npm install && npm test" exited with 1.
Done. Your build exited with 1.

我真的不知道我做错了什么。当我在我的机器上执行命令时,它可以工作......

编辑:只是更多信息,如果节点服务器没有运行,我的本地计算机上也会出现错误消息“ERROR:connect ECONNREFUSED”。所以我的猜测是travis没有正确启动节点服务器。但我不确定我的剧本有什么问题。

1 个答案:

答案 0 :(得分:1)

我很抱歉,但你所做的是让你很难受。您应该将所有测试都放在一个目录中。我无法想出一个会迫使您将测试保存在不同目录中的原因。

无论您在测试中想要什么,都可以使用import轻松导入。

如果你能解决这个问题,要在后台从不同的目录运行服务器,你可以使用带有以下命令的bash脚本

cd path/to/directory
npm start &

然后在.travis.yml文件中

script: 
 - ./your_script.sh
 - sleep 25
 - npm test