Testing a REST API which depends on MongoDB backend, and using Cypress - fails to start API

I have a config file which contains a REST API testing image using Cypress and MongoDB which is required as a backend via an env var MONGODB_URI.

If I run cypress npm test command locally, it works as expected. On Circle CI it fails, and I think the API image is not seeing the Environment Variable MONGODB_URI as it can’t find the database, thereby not spinning up the API, and Cypress is not able to run the tests.

I’ve attached my config.yaml any help is appreciated and I am happy to pay for support as I need to get this infra up and running…

This is my config.yml

Any guidance is much appreciated!

version: 2.1
orbs:
  cypress: cypress-io/cypress@1
# using service containers on CircleCI
# https://github.com/cypress-io/circleci-orb/blob/master/docs/recipes.md#service-containers
# https://circleci.com/docs/2.0/databases/
executors:
  with-mongo:
    docker:
      - image: cypress/base:14.16.0
        environment:
          MONGODB_URI: mongodb://$MONGO_INITDB_ROOT_USERNAME:$MONGO_INITDB_ROOT_PASSWORD@localhost:27017/?authSource=admin

      - image: mongo:4.4.5
        environment:
          MONGO_INITDB_ROOT_USERNAME: $MONGO_INITDB_ROOT_USERNAME
          MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
workflows:
  build:
    jobs:
      - cypress/run:
          executor: with-mongo
          start: npm test
          # no need to save the workspace after this job
          no-workspace: true

Here is the image of the build error

I have configured the environment variables required by my API on the project as well

Name	Value

MONGODB_URI	xxxxdzzz

MONGO_INITDB_DATABASE	xxxxzzxx

MONGO_INITDB_ROOT_PASSWORD	xxxxrd0x

MONGO_INITDB_ROOT_USERNAME	xxxxxx

NODE_ENV	xxxxzzzx


1 Like

Hey thanks for dropping this here… I have pinged some internal folks and will try to get some quick eyes on it

1 Like

I’ve even disabled authentication when spinning up Mongo container, and it just somehow shows a gray circle on the far left, instead of a green checkmark. What am I missing?

I had a look at your sample config, and got the mongo service image working with a minimal Node.js sample.
This makes me think there’s something wrong with how you’re connecting to it in your Cypress test.

The grey icon appears when service images terminate - it would show in the logs if you open the step up whether there has been any connections to it.

For example here’s log from the mongodb service container - showing connections from Node.js client - presumably yours won’t show it if there haven’t been any connections established:

{"t":{"$date":"2022-02-02T14:21:22.680+00:00"},"s":"I",  "c":"NETWORK",  "id":23016,   "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
{"t":{"$date":"2022-02-02T14:21:25.185+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:44700","connectionId":1,"connectionCount":1}}
{"t":{"$date":"2022-02-02T14:21:25.190+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn1","msg":"client metadata","attr":{"remote":"127.0.0.1:44700","client":"conn1","doc":{"driver":{"name":"nodejs","version":"4.3.1"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"5.11.0-1022-aws"},"platform":"Node.js v14.16.0, LE (unified)|Node.js v14.16.0, LE (unified)"}}}
{"t":{"$date":"2022-02-02T14:21:25.195+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:44702","connectionId":2,"connectionCount":2}}
{"t":{"$date":"2022-02-02T14:21:25.196+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn2","msg":"client metadata","attr":{"remote":"127.0.0.1:44702","client":"conn2","doc":{"driver":{"name":"nodejs","version":"4.3.1"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"5.11.0-1022-aws"},"platform":"Node.js v14.16.0, LE (unified)|Node.js v14.16.0, LE (unified)"}}}
{"t":{"$date":"2022-02-02T14:21:25.216+00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn2","msg":"Successful authentication","attr":{"mechanism":"SCRAM-SHA-256","principalName":"$MONGO_INITDB_ROOT_USERNAME","authenticationDatabase":"admin","client":"127.0.0.1:44702"}}
{"t":{"$date":"2022-02-02T14:21:25.219+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn1","msg":"Connection ended","attr":{"remote":"127.0.0.1:44700","connectionId":1,"connectionCount":1}}
{"t":{"$date":"2022-02-02T14:21:25.228+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:44702","connectionId":2,"connectionCount":0}}

Also - the very minimal test I used - run with npm install && npm run test.
If this connection fails it throws an error otherwise the tests passes.

const assert = require('assert')
const {MongoClient} = require('mongodb');

describe("Connection test", function() {

  describe("Mongodb can open connection", function() {

    it("Connects and disconnects successfully", async function() {
      let openAndCloseConnection = async function() {
        let url = process.env.MONGODB_URI
    
        console.log(url)
        const client = new MongoClient(url)
        await client.connect()  
        await client.close()
      }
      await assert.doesNotThrow(openAndCloseConnection)
    })
  })
1 Like