Search code examples
testingherokuintegrationpipeline

Yarn tests on Heroku fail to connect to my back-end code


I am trying to execute tests via a Heroku pipeline with the following app.json:

{
  "environments": {
    "test": {
      "addons": [
        {
          "plan": "mongolab:sandbox"
        }
      ],
      "scripts": {
        "test": "yarn test && ((nohup yarn test:start:api &) && sleep 10 && yarn test:integration)"
      }
    }
  }
}

Locally on my Linux machine the command works but on Heroku I can not reach the API service:

-----> Running test command `yarn test && ((nohup yarn test:start:api &) && sleep 10 && yarn test:integration)`...
yarn run v1.6.0
warning package.json: No license field
$ mocha-webpack --webpack-config ./webpack.config.js --require test/setup.js model/**/*.spec.js api/**/*.spec.js
 WEBPACK  Compiling...
 WEBPACK  Compiled successfully in 792ms
 MOCHA  Testing...
  0 passing (1ms)
 MOCHA  Tests completed successfully
Done in 4.12s.
yarn run v1.6.0
warning package.json: No license field
$ babel-node ./server.js --presets es2015,stage-2
yarn run v1.6.0
warning package.json: No license field
$ mocha-webpack --webpack-config ./webpack.config.js --require test/setup.js model/**/*.integration.js api/**/*.integration.js
 WEBPACK  Compiling...
 WEBPACK  Compiled successfully in 1092ms
 MOCHA  Testing...
  User
Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
(node:570) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
Successfully initialized mongoose-seed
Users collection cleared
Successfully created document [0] of User model
Error: Error: connect ECONNREFUSED 127.0.0.1:8080
...

How can I determine the URI to connect to the API-service itself (nohup yarn test:start:api &)?

The API is built with express-restify-mongoose and as it seems there is no out of the box testing like inject with hapijs (https://hapijs.com/api#-await-serverinjectoptions)

How can I make my tests run on Heroku?


Solution

  • Your tests are trying to connect to 127.0.0.1:8080, but your web application is almost certainly running elsewhere. Heroku provides the PORT environment variable telling your back-end code what port it should use.

    Try updating your tests to connect to whatever that variable holds instead of 8080.