Run HTTP server in background during tests

The way I run tests locally is normally npm start and then, once the server is running, in a different terminal tab I run npm test. This is because my test scripts make multiple HTTP calls to the web server started in npm start.

How can I make this work with CircleCI?

2 Likes

Wouldn’t just adding an “&” after “npm start” do the job?

1 Like

You can use something like forever as well to make sure that it stays up. Note that each command in circle.yml runs in a separate shell.

No, this won’t work because I need to wait for the port to be opened / the server to be started before running the tests. I could do a sleep 10 etc but this seems hacky

What about following it with a curl request using --connect-timeout and --max-time? That way your build script would stall (I think) until your server is up. Otherwise, wrap that call in a loop?

1 Like

I would do something like what @retorquere mentioned.

Did you ever get this figured out? We use a similar workflow, but I can’t seem to get my node server to start in the background, and still get my tests to hit the local urls

Similar issue, when I try to run my node server in the background I see: Step was canceled

And then I cannot connect to it with curl

I setup separate run commands to get a graphql server running and listening so I could execute tests against it. https://circleci.com/docs/2.0/configuration-reference/#run explained how to override the shell to ignore a failed exit code.

          # get server up and running in the background
          - run:
              command: yarn test-server
              background: true
          # wait for the server to start responding. We expect Bad Request 400 once it starts listening.
          # so override the shell and have the last command be the : { null } command to force exit code 0.
          - run:
              shell: /bin/sh
              command: |
                wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 -t 10 http://localhost:3032/graphql
                :
          - run: 
              command: yarn test
7 Likes