【问题标题】:Running a node server in the background on travis在 travis 上后台运行节点服务器
【发布时间】:2017-06-30 00:42:40
【问题描述】:

我正在尝试对 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.

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

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

【问题讨论】:

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


    【解决方案1】:

    我很抱歉,但你的所作所为让你很难受。您应该将所有测试放在一个目录中。我想不出有什么理由会迫使您将测试保存在不同的目录中。

    您在测试中想要的任何内容都可以使用import 轻松导入。

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

    cd path/to/directory
    npm start &
    

    然后在你的 .travis.yml 文件中

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

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 2020-07-24
      • 2020-04-10
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      相关资源
      最近更新 更多