部署 Express 服务器并通过 CircleCI 测试 GraphQL API
Deploy Express server and test GraphQL API via CircleCI
我想测试我的 Express 服务器是否正常启动以及我的端点是否检索到正确的信息。我希望在 CI 中执行此操作,以便我可以监控我的应用程序何时不再工作,如果我将来进行任何更改。
这是一个非常简单的应用程序,我的服务器是这样的:
server.js
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { GraphQLObjectType, GraphQLString, GraphQLSchema } from 'graphql';
import cors from 'cors';
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
hello: {
type: GraphQLString,
resolve: () => "Hello world!"
}
})
});
const schema = new GraphQLSchema({ query: QueryRoot });
const app = express();
app.use(cors()); // enable cors
app.use('/api', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(1337, () => {
console.log('Server running on port: 1337');
});
basicTest.test.js
:
const chai = require('chai');
const expect = chai.expect;
const url = `http://localhost:1337`;
const request = require('supertest')(url);
describe('GraphQL', () => {
it('Response returns hello world', (done) => {
request.post('/api')
.send({ query: ' { hello }' })
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body.data.hello).to.contain('Hello world!');
done();
});
});
});
我的 .circleci/config.yml
看起来像这样:
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:10.16.0
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run:
npm start &
npm test
和package.json
:
{
"name": "graphql-express-server",
"repository": {
"url": "myURL"
},
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm server.js",
"test": "mocha test/*.test.js"
},
"author": "AuthorName",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"esm": "^3.2.25",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^14.4.2",
"nodemon": "^1.19.1",
"supertest": "^4.0.2"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.0"
}
}
我尝试将 &
添加到 npm start
命令的末尾,但据我所知,这只是跳过了整个过程。我遇到的问题是 'Error: ECONNREFUSED: Connection refused'
执行测试时。
我需要在别处托管服务器吗?我不确定我应该做什么。我以前从未真正测试过 CI 中的后端。
编辑:
我的命令如下:
- run:
npm start &
- run:
sleep 5
- run:
npm test
而我在CI中的输出如下:
--------------------------------
***npm start &***
#!/bin/bash -eo pipefail
npm start &
--------------------------------
***sleep 5***
#!/bin/bash -eo pipefail
sleep 5
--------------------------------
***npm test***
#!/bin/bash -eo pipefail
npm test
> graphql-express-server@1.0.0 test /home/circleci/repo
> mocha test/*.test.js
GraphQL
1) Response returns hello world
0 passing (14ms)
1 failing
1) GraphQL
Response returns hello world:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:165:15)
at localAssert (node_modules/supertest/lib/test.js:131:12)
at /home/circleci/repo/node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:647:10)
at Socket.socketErrorListener (_http_client.js:392:9)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! Test failed. See above for more details.
Exited with code 1
--------------------------------
如您所见,消息 Server running on port: 1337
没有出现在我的 npm start &
步骤控制台部分。
有什么建议吗?我做错了什么吗?
npm start &
不会等待服务器启动,您确定在您的测试发出请求时您的服务器正在侦听端口吗? IE。 CI 上的控制台中有 'Server running on port: 1337'
吗?如果没有,那么在开始测试之前尝试 运行 sleep 3
我想测试我的 Express 服务器是否正常启动以及我的端点是否检索到正确的信息。我希望在 CI 中执行此操作,以便我可以监控我的应用程序何时不再工作,如果我将来进行任何更改。
这是一个非常简单的应用程序,我的服务器是这样的:
server.js
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { GraphQLObjectType, GraphQLString, GraphQLSchema } from 'graphql';
import cors from 'cors';
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
hello: {
type: GraphQLString,
resolve: () => "Hello world!"
}
})
});
const schema = new GraphQLSchema({ query: QueryRoot });
const app = express();
app.use(cors()); // enable cors
app.use('/api', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(1337, () => {
console.log('Server running on port: 1337');
});
basicTest.test.js
:
const chai = require('chai');
const expect = chai.expect;
const url = `http://localhost:1337`;
const request = require('supertest')(url);
describe('GraphQL', () => {
it('Response returns hello world', (done) => {
request.post('/api')
.send({ query: ' { hello }' })
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body.data.hello).to.contain('Hello world!');
done();
});
});
});
我的 .circleci/config.yml
看起来像这样:
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:10.16.0
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run:
npm start &
npm test
和package.json
:
{
"name": "graphql-express-server",
"repository": {
"url": "myURL"
},
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm server.js",
"test": "mocha test/*.test.js"
},
"author": "AuthorName",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"esm": "^3.2.25",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^14.4.2",
"nodemon": "^1.19.1",
"supertest": "^4.0.2"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.0"
}
}
我尝试将 &
添加到 npm start
命令的末尾,但据我所知,这只是跳过了整个过程。我遇到的问题是 'Error: ECONNREFUSED: Connection refused'
执行测试时。
我需要在别处托管服务器吗?我不确定我应该做什么。我以前从未真正测试过 CI 中的后端。
编辑:
我的命令如下:
- run:
npm start &
- run:
sleep 5
- run:
npm test
而我在CI中的输出如下:
--------------------------------
***npm start &***
#!/bin/bash -eo pipefail
npm start &
--------------------------------
***sleep 5***
#!/bin/bash -eo pipefail
sleep 5
--------------------------------
***npm test***
#!/bin/bash -eo pipefail
npm test
> graphql-express-server@1.0.0 test /home/circleci/repo
> mocha test/*.test.js
GraphQL
1) Response returns hello world
0 passing (14ms)
1 failing
1) GraphQL
Response returns hello world:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:165:15)
at localAssert (node_modules/supertest/lib/test.js:131:12)
at /home/circleci/repo/node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:647:10)
at Socket.socketErrorListener (_http_client.js:392:9)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! Test failed. See above for more details.
Exited with code 1
--------------------------------
如您所见,消息 Server running on port: 1337
没有出现在我的 npm start &
步骤控制台部分。
有什么建议吗?我做错了什么吗?
npm start &
不会等待服务器启动,您确定在您的测试发出请求时您的服务器正在侦听端口吗? IE。 CI 上的控制台中有 'Server running on port: 1337'
吗?如果没有,那么在开始测试之前尝试 运行 sleep 3