-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into js-137-manager-approves-report
Conflicts: docs/logical_data_model.md frontend/src/components/Navigator/index.js
- Loading branch information
Showing
24 changed files
with
417 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/bash | ||
|
||
echo "Running tests in using test config 'docker-compose.test.yml'" | ||
# Start containers | ||
docker-compose -f 'docker-compose.test.yml' up -d | ||
|
||
# Let postgres initialize | ||
echo "Give postgres a few seconds to start up..." | ||
sleep 5 | ||
|
||
# Migrate and seed db | ||
docker exec test-backend bash -c "yarn db:migrate" | ||
docker exec test-backend bash -c "yarn db:seed;" | ||
|
||
# Test backend | ||
docker exec test-backend bash -c "yarn test:ci" | ||
|
||
# Test frontend | ||
docker exec test-backend bash -c "yarn --cwd frontend run test:ci" | ||
|
||
# Cleanup | ||
docker-compose \ | ||
-f 'docker-compose.test.yml' \ | ||
down --volumes |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
version: "3.5" | ||
services: | ||
test-backend: | ||
build: | ||
context: . | ||
container_name: test-backend | ||
command: yarn server | ||
user: ${CURRENT_USER:-root} | ||
depends_on: | ||
- test-db | ||
environment: | ||
- POSTGRES_HOST=test-db | ||
volumes: | ||
- ".:/app:rw" | ||
networks: | ||
- ttadp-test | ||
test-frontend: | ||
build: | ||
context: . | ||
command: yarn start | ||
user: ${CURRENT_USER:-root} | ||
stdin_open: true | ||
volumes: | ||
- "./frontend:/app:rw" | ||
- "./scripts:/app/scripts" | ||
environment: | ||
- BACKEND_PROXY=http://test-backend:8080 | ||
networks: | ||
- ttadp-test | ||
test-db: | ||
image: postgres:12.4 | ||
container_name: test-db | ||
env_file: .env | ||
networks: | ||
- ttadp-test | ||
# Use non-default network so we don't conflict with the developer environment | ||
networks: | ||
ttadp-test: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Testing | ||
|
||
## Tips and Caveats when writing tests | ||
|
||
|
||
### Handling async/promises | ||
|
||
When writing tests that rely on asynchronous operations, such as writing to the database, take care to make sure that those operations are resolved before any tests that rely on them run. If you need to create database records in a setup function such as `beforeAll`, you will want to make sure all async/promise operations resolve before subsequent tests run. You can make sure multiple await (promise) operations resolve by using [`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) (which takes an iterable of promises). | ||
|
||
Here's how that might look: | ||
|
||
``` | ||
let a = someAsyncFun(); | ||
let b = anotherAsyncFun(); | ||
return Promise.all([a, b]); | ||
``` | ||
|
||
### Creating/deleting database records | ||
|
||
Some tests will require interactions with the database, but at present all tests run using the same instance of the database. That means that any records you create or delete can potentially affect tests elsewhere. On top of that, tests run in parallel, so database operations may run in an unexpected order. That can mean tests may pass various times only to fail due to missing or unexpected records in the database when your tests run. | ||
|
||
To mitigate issues with missing or unexpected records causing failing tests, you can try a few approaches. One approach is to avoid using the database if your test doesn't actually require it. You may be able to use mock models or responses rather than interact with the database. If your test does require the database, you should create the records you need before your tests run and delete the records you created (and no others) when your tests finish. If you `Model.create`, make sure you `Model.destroy()` the records you created. | ||
|
||
When writing tests that create database records, it might also help to use a `try...catch` to catch errors in database transactions and log meaningful output. Sequelize error messages can be vague, and it might help others to see more informative messages. | ||
|
||
## Testing in Docker | ||
|
||
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). | ||
|
||
In the `docker-compose.yml` configuration, the database is set up to persist to a volume, "dbdata", so database records will persist between runs of the 'db' service, unless you remove that volume explicitly (e.g. `docker volume rm` or `docker-compose down --volumes`). | ||
|
||
|
||
### Notes on docker-compose and multiple configurations | ||
|
||
`docker-compose` has a feature for providing multiple `docker-compose.*.yml` files where subsequent files can override settings in previous files, which sounds like it would suit the use case of running docker for local development and for testing. However, the ability to [override configurations](https://docs.docker.com/compose/extends/#adding-and-overriding-configuration) is limited. While experimenting with overrides, it became clear that doing so would require a minimum of three docker-compose.yml files: one "base", one for local development, one for running tests. Trying to compose docker-compose.yml files would be complicated. | ||
|
||
In addition, while experimenting with multiple configuration files, it became clear that docker was unable to differentiate between different versions of the same service. Trying to override the 'db' service for testing would not work as expected: if the local/dev 'db' service had already been created, that one would be used when tests were run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.