#!/bin/sh
# script/frontend-test: run the frontend test suite on the host: the
# unit tests in test/unit/, through the test script in package.json,
# then the production build, which fails on broken code. make test runs
# the same tests in the frontend-test stage of Dockerfile, and the image
# build runs the production build in its frontend-build stage. The tests
# print a dot each; if any fails, they run again with every test listed,
# and the script fails even if that run passes. NODE_OPTIONS chooses the
# reporter because yarn adds its arguments after the test files, where
# node would take a reporter option for one more file.
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"

main() {
    cd "$ROOT"
    NODE_OPTIONS=--test-reporter=dot timeout 90 yarn --silent run test || {
        echo "--- Rerunning with every test listed for details ---"
        NODE_OPTIONS=--test-reporter=spec timeout 90 yarn --silent run test
        exit 1
    }
    timeout 30 yarn build
}

main "$@"
