title: Deploy Express server and test GraphQL API via CircleCI
body: <p>I wish to test that my Express server launches properly and that my endpoint retrieves the correct information. I wish to do this in CI so that I can monitor when my application is no longer working, if I make any changes in the future.</p>
  
  <p>It's a very simple application, my server looks like this:</p>
  
  <p><strong><code>server.js</code></strong></p>
  
  <pre><code>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: () =&gt; ({
          hello: {
              type: GraphQLString,
              resolve: () =&gt; "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, () =&gt; {
      console.log('Server running on port: 1337');
  });
  </code></pre>
  
  <p><strong><code>basicTest.test.js</code></strong>:</p>
  
  <pre><code>const chai = require('chai');
  
  const expect = chai.expect;
  const url = `http://localhost:1337`;
  const request = require('supertest')(url);
  
  describe('GraphQL', () =&gt; {
      it('Response returns hello world', (done) =&gt; {
          request.post('/api')
              .send({ query: ' { hello }' })
              .expect(200)
              .end((err, res) =&gt; {
                  if (err) return done(err);
                  expect(res.body.data.hello).to.contain('Hello world!');
                  done();
              });
      });
  });
  </code></pre>
  
  <p>My <code>.circleci/config.yml</code> looks like this:</p>
  
  <pre><code># 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 &amp;
            npm test
  </code></pre>
  
  <p>and <strong><code>package.json</code></strong>:</p>
  
  <pre><code>{
      "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"
      }
  }
  </code></pre>
  
  <p>I tried adding <code>&amp;</code> to the end of my <code>npm start</code> command but that just skips the entire process as far as I can see. The issue I'm getting is <code>'Error: ECONNREFUSED: Connection refused'</code> when the test gets executed. </p>
  
  <p>Do I need to host the server elsewhere? I'm unsure what I'm supposed to do. I've never really tested the back end in CI before.</p>
  
  <p><strong><em>EDIT:</em></strong></p>
  
  <p>My commands are as follows:</p>
  
  <pre><code>- run: 
      npm start &amp;
  - run:
      sleep 5
  - run: 
      npm test
  </code></pre>
  
  <p>And my output in CI is as follows:</p>
  
  <pre><code>--------------------------------
  ***npm start &amp;***
  
  #!/bin/bash -eo pipefail
  npm start &amp;
  
  --------------------------------
  ***sleep 5***
  
  #!/bin/bash -eo pipefail
  sleep 5
  
  --------------------------------
  ***npm test***
  
  #!/bin/bash -eo pipefail
  npm test
  
  &gt; graphql-express-server@1.0.0 test /home/circleci/repo
  &gt; 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
  
  --------------------------------
  </code></pre>
  
  <p>As you can see, the message <code>Server running on port: 1337</code> does not show up in my <code>npm start &amp;</code> step console section.</p>
  
  <p>Any suggestions? Am I doing something wrong?</p>