diff --git a/bin/run-tests b/bin/run-tests index 49745897cf..a7c694c4d6 100755 --- a/bin/run-tests +++ b/bin/run-tests @@ -1,24 +1,77 @@ #!/bin/bash -echo "Running tests in using test config 'docker-compose.test.yml'" -# Start containers -docker-compose -f 'docker-compose.test.yml' up -d +declare -a options=(frontend backend) +declare lr=">>>>>>>>" +declare -i exit_code=0 -# Let postgres initialize -echo "Give postgres a few seconds to start up..." -sleep 5 +log() { + echo "$lr $*" +} -# Migrate and seed db -docker exec test-backend bash -c "yarn db:migrate" -docker exec test-backend bash -c "yarn db:seed;" +check_exit() { + if [[ "$1" -ne 0 ]]; then + echo "$lr last docker-compose command failed" + ((exit_code++)) + fi +} -# Test backend -docker exec test-backend bash -c "yarn test:ci" +main() { + local opt="" -# Test frontend -docker exec test-frontend bash -c "yarn --cwd frontend run test:ci" + for o in "${options[@]}"; do + if [[ "${1}" == "$o" ]]; then + opt="$o"; + fi + done -# Cleanup -docker-compose \ - -f 'docker-compose.test.yml' \ - down --volumes + log "Running tests in using test config 'docker-compose.test.yml'" + # Start containers + docker-compose -f 'docker-compose.test.yml' up -d + check_exit "$?" + + # Let postgres initialize + echo + log "Giving postgres a few seconds to start up..." + sleep 5 + + # Migrate and seed db + echo + log "Migrating & seeding db" + docker exec test-backend bash -c "yarn db:migrate" + check_exit "$?" + docker exec test-backend bash -c "yarn db:seed;" + check_exit "$?" + + if [[ "$opt" == "backend" || -z "$opt" ]]; then + # Test backend + echo + log "Running backend tests" + docker exec test-backend bash -c "yarn test:ci" + check_exit "$?" + fi + + if [[ "$opt" == "frontend" || -z "$opt" ]]; then + # Test frontend + echo + log "Running frontend tests" + docker exec test-frontend bash -c "yarn --cwd frontend run test:ci" + check_exit "$?" + fi + + # Cleanup + echo + log "Cleaning up test containers" + docker-compose \ + -f 'docker-compose.test.yml' \ + down --volumes + check_exit "$?" + + if [[ $exit_code -ne 0 ]]; then + echo + log "Errors occurred during script execution" + fi + + exit "$exit_code" +} + +main "$@" diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 222520beba..46d93e3edc 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -17,6 +17,7 @@ services: test-frontend: build: context: . + container_name: test-frontend command: yarn start user: ${CURRENT_USER:-root} stdin_open: true diff --git a/docs/testing.md b/docs/testing.md index 2f5b366cd6..5268d7fe57 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -30,6 +30,10 @@ When writing tests that create database records, it might also help to use a `tr To simplify running tests in Docker, there is a bash script, `./bin/run-tests` that will run the appropriate commands to start `test-` variations of the services used in tests. You should be able to run tests using that command while your development Docker environment is running. The script uses a separate `docker-compose.test.yml` which does not create a user-accessible network and cleans up after itself once tests have run. +This script is written such that it will log errors, but won't exit if a docker command fails. It will count the number of errors and the number of errors will be the exit code (`$?`) for the script. So if three docker commands fail, the exit code would be 3. + +By default, `./bin/run-tests` will run both backend and frontend tests. If you want to run only one set of tests, supply 'frontend' or 'backend' as a parameter. So to run only the backend tests, you'd run `./bin/run-tests backend`. + ### Running tests in your development Docker environment When running tests in Docker, be aware that there are tests that will modify/delete database records. For tests to run, the 'db' service needs to exist and `db:migrate` and `db:seed` need to have been run (to create the tables and populate certain records).