When we run our tests, we want to make sure that our backend API is running using the NODE_ENV environment variable set to test. At the moment, we are doing this manually. However, it's more ideal to add it as part of our test script. Just as we did for our Swagger UI, we can use Git submodules to include the Hobnob API repository in the client's repository without duplicating the code:
git submodule add git@github.com:d4nyll/hobnob.git api
Now, to make life easier for later, add the following NPM scripts to package.json:
"api:init": "git submodule update --init",
"api:install": "yarn install --cwd api",
"api:serve": "yarn --cwd api run build && dotenv -e api/.env.example node api/dist/index.js",
"api:update": "git submodule update --init --remote",
api:init will download the Hobnob API repository using the commit hash that's been stored. api:install usesĀ --cwd to change directory into the api directory before running yarn install. api:serve first runs the build script from our API repository, loads the environment variables, and then runs the API server. api:update will download but also update the API repository to the latest commit in the same branch.
Lastly, run the NPM scripts inside scripts/e2e.test.sh:
...
export NODE_ENV=test
yarn run api:init > /dev/null 2>&1 &
yarn run api:install > /dev/null 2>&1 &
yarn run api:serve > /dev/null 2>&1 &
yarn run serve > /dev/null 2>&1 &
...