Unit Tests with Mongo

Using this repository, we’ll help you get up to speed with basic functionality such as: compiling, testing and building Docker images.

This project uses Node.js, Mongo to build an application which will eventually become a distributable Docker image.

Looking around

In the root of this repository, you’ll find a file named codefresh.yml, this is our build descriptor and it describes the different steps that comprise our process. Let’s quickly review the contents of this file:

codefresh.yml

version: '1.0'
steps:
  build_step:
    type: build
    image_name: codefreshio/example_nodejs_mongo
    dockerfile: Dockerfile
    tag: ${{CF_BRANCH}}

  unit_test:
    type: composition
    working_directory: ${{build_step}}
    composition:
      version: '2'
      services:
        mongo:
          image: mongo
    composition_candidates:
      test:
        image: ${{build_step}}
        links:
          - mongo
        command: bash -c "/src/test-script.sh"
        environment:
          - MONGO_PORT=27017

In this test script, we wait for mongo is ready, then we can run the tests

script.sh

#!/usr/bin/env bash
wait_for_db() {
  nslookup mongo
  if ! nc -z mongo 27017; then
    echo "Waiting for db..."
    sleep 2
    wait_for_db
  fi
}

wait_for_db

export MONGO_PORT=27017

cd /src
npm install
npm install -g mocha
npm test
Example

Just head over to the example repository in Github.

Expected result

Codefresh unit test Mongo