gpt4 book ai didi

node.js - 在 Travis 中运行 Node 应用程序

转载 作者:搜寻专家 更新时间:2023-10-31 22:26:46 25 4
gpt4 key购买 nike

我目前使用 Node js 构建服务器端应用程序。为了测试它,我使用 Travis,它 runs npm test by default .

现在我还想测试依赖关系是否正确,因此在 Travis 中使用

启动应用程序
nodejs app.js

如何在 Travis 中运行此任务?

最佳答案

您可以像期望的那样在 unix shell 上运行任何任务:

language: node_js
node_js:
- "5"
before_script:
- npm install
script:
- node app.js
- npm test

不过,npm install 命令已经涵盖了您的目的。如果此操作失败并且随后您的 npm test 也失败,则构建将不会成功。

对于需要运行实际服务器的更复杂的示例,例如在 API 端到端测试中,我将使用 docker-compose 代替。但这在这里太多了。

travis.yml

language: node_js
sudo: required
services:
- docker
cache:
directories:
- node_modules
node_js:
- 5
before_install:
- npm install -g node-gyp
before_script:
- npm install
- npm install -g standard
- docker-compose build
- docker-compose up -d
- sleep 3
script:
- npm test
after_script:
- docker-compose kill

docker-compose.yml

api1:
build: .
dockerfile: ./Dockerfile
ports:
- 3955
links:
- mongo
- redis
environment:
- REDIS_HOST=redis
- MONGO_HOST=mongo
- IS_TEST=true
command: "node app.js"

api2:
build: .
dockerfile: ./Dockerfile
ports:
- 3955
links:
- mongo
- redis
environment:
- REDIS_HOST=redis
- MONGO_HOST=mongo
- IS_TEST=true
command: "node app.js"

mongo:
image: mongo
ports:
- "27017:27017"
command: "--smallfiles --logpath=/dev/null"

redis:
image: redis
ports:
- "6379:6379"

haproxy:
image: haproxy:1.5
volumes:
- ./cluster:/usr/local/etc/haproxy/
links:
- "api1"
- "api2"
ports:
- 80:80
- 70:70
expose:
- "80"
- "70"

关于node.js - 在 Travis 中运行 Node 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36197855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com